I'm attempting to show a div tag based on the root url of my site i.e. if the url is dev.foo.com display div开发者_StackOverflow if else www.foo.com don't display etc. I've come up with this
<?php if (substr($_SERVER['REQUEST_URI'], 0, 5) !== 'dev.') {
echo '<div class="dev-site-stuff">THIS IS THE DEV SITE!!</div>';
}
?>
But it isn't working and I can't see a reason - any thoughts greatly appreciated!
you should use $_SERVER['SERVER_NAME']
If you have any other doubts just do a print_r($_SERVER);
to actually see how the array is composed
$url = explode('.', $_SERVER['HTTP_HOST'], 2);
/* you might want pretty variable that exactly gets the subdomain.
* $subdomain = $url[0] != "www" ? $url[0] : "";
* if($subdomain == "dev") {
* // dev site...
* }else if($subdomain == "") {
* // www site...
* }
*/
if($url[0] == "dev") {
echo '<div class="dev-site-stuff">THIS IS THE DEV SITE!!</div>';
}
substr, strpos etc. are not always effective in this cases. It's nice?
EDIT: By the way read comments of your question.
As well as @yes123 answer above you are also comparing a substring of length 5 against a string of length 4 so it will never be true.
You con use strrpos like this
if (strrpos($_SERVER['SERVER_NAME'], 'dev.') > 0) {
Hope this helps.
精彩评论