Jump to content

PHP if URL equals this then perform action


sphinx

Recommended Posts

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.');
}

 

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).

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";
    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.