sphinx Posted January 19, 2015 Share Posted January 19, 2015 Hi, I'm using an example below regarding a whole URL. I'm looking for some conditional PHP that will display certain content depending on if the URL contains the word 'liz' if not, display else. $host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; if($host == 'liz-4.website.com') { echo $this->__('The domain contains the word liz.'); } else { echo $this->__('The domain does not contain the word liz.'); } Link to comment https://forums.phpfreaks.com/topic/294069-php-if-url-equals-this-then-perform-action/ Share on other sites More sharing options...
ginerjm Posted January 19, 2015 Share Posted January 19, 2015 You have the if statement, but what is this "this" that you use? Why "this"? Link to comment https://forums.phpfreaks.com/topic/294069-php-if-url-equals-this-then-perform-action/#findComment-1503466 Share on other sites More sharing options...
requinix Posted January 19, 2015 Share Posted January 19, 2015 Checking if the host string is another string is not the same as checking if the string contains another string. strpos And you should be using HTTP_HOST instead of SERVER_NAME. The former is the actual hostname used in the URL while the second is the name configured in the server. They don't necessarily match (though they often do). Link to comment https://forums.phpfreaks.com/topic/294069-php-if-url-equals-this-then-perform-action/#findComment-1503468 Share on other sites More sharing options...
QuickOldCar Posted January 20, 2015 Share Posted January 20, 2015 If are looking for the first 3 characters for liz this could work. if(substr($_SERVER['HTTP_HOST'], 0, 3) == "liz"){ echo "yes"; } else { echo "no"; } (this would be better if you did a check if was not your servers actual name) example: if($_SERVER['HTTP_HOST'] != "website.com"){ $is_subdomain = true; } if(isset($is_subdomain) && substr($_SERVER['HTTP_HOST'], 0, 3) == "liz"){ echo "yes"; } else { echo "no"; } below will look only within the subdomain an exact subdomain $explode_host = explode(".",$_SERVER['HTTP_HOST']); $subdomain = $explode_host[0]; if($subdomain == "liz-4"){ echo "yes"; } else { echo "no"; } anywhere it exists within subdomain as requinix mentioned $explode_host = explode(".",$_SERVER['HTTP_HOST']); $subdomain = $explode_host[0]; $pos = strpos($subdomain, "liz"); if ($pos !== false){ echo "yes"; } else { echo "no"; } Link to comment https://forums.phpfreaks.com/topic/294069-php-if-url-equals-this-then-perform-action/#findComment-1503495 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.