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

 

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.