using root_domain but can't get to next step

I have a website with four versions depending on the device accessing it. Remember when duplicating the content of websites was a huge no-no a few years back? I’ve begun using PHP includes to keep them all alike when I make changes. To complicate things further, two of each version of the website are on two different domains. I know that was frowned upon in those old ‘scramble for search position’ days, but this is a special case IMHO. It’s actually easy to copy the new include file to both domains and they all say the same thing when I make a change. I need a PHP script to determine what domain name the surfer came from, and then echo a letter or a blank depending on which one. I already have a script that uses print root_domain and echo in a few places from a give-away script I adapted. Unfortunately I’m so inept I can’t get to the next step to just echo a letter for one domain or blank for the other.

Here’s my best guess:

[php]function GetRootDomain ()
{
$URL = strtolower($_SERVER[“HTTP_HOST”]);

$URL = preg_replace('/www\./', '', $URL);

$URL = parse_url(strtoupper($URL));

if (!empty($URL["host"]))   {
    return $URL["host"];
} else {
    return $URL["path"];
}

}

//good above

$root_domain = GetRootDomain();

if ($root_domain=“ex.com”)

    {   
    echo "t" ;
    }

else
{
echo “x” ;
}[/php]

The comment in the middle marks a working script. When I use this script I get the first echo output every time. Any help would be appreciated.

This was one of my most common mistakes too, you need 2 ='s to do a string comparison and 3 ='s if you’re doing a string and type comparison.

I program in in a lot of different languages and this one always gets me when I switch back to PHP.

Change:

[php]if ($root_domain=“ex.com”)[/php]

To:

[php]if ($root_domain == “ex.com”)[/php]

Thanks Topcoder, but now I just get the second echo in either domain. If I’m in ex.com I still get an “x”.

Do you really own ex.com or is that just some example your trying to explain things with?

I used the following code below to mock up a sample of what you are trying to do and it works for me, test it on you server.

[php]<?php
$root_domain = “ex.com”;

if ($root_domain==“ex.com”)
{
echo “t” ;
}

else
{
echo “x” ;
}

$root_domain = “no.com”;

if ($root_domain==“ex.com”)
{
echo “t” ;
}
else
{
echo “x” ;
}
?>[/php]

I don’t own ex.com. I wish I did as it would probably sell better than the ones I own and try to sell ;D Your code isn’t working for me. I want root_domain to be determined, and the same script working on both domains. I’m actually only trying to get a letter if on one domain, and nothing if I’m on the second, so my best guess for code should be:
[php]function GetRootDomain ()
{
$URL = strtolower($_SERVER[“HTTP_HOST”]);

$URL = preg_replace('/www\./', '', $URL);

$URL = parse_url($URL);

if (!empty($URL["host"]))   {
    return $URL["host"];
} else {
    return $URL["path"];
}

}

//good above

$root_domain = GetRootDomain();

if ($root_domain=“ex.com”)

    {   
    echo "t" ;
    }

else
{
echo “” ;
}[/php]
As you can see, I’ve taken out the second echo, and also the unnecessary call for an uppercase first letter at parse_url. I could probably take out the second echo altogether, if I could get the output right. I’m getting a two letter output with your latest suggestion, and no variation when on either domain.

What do you mean it’s not working?

Did you use my code as is and not retro fit it into your code?

Or did you try to retro fit it and that made it not work?

The single = is not how you do a comparison in php, you can read all about that in the manual. .

http://php.net/manual/en/language.operators.comparison.php

and

If I'm in ex.com I still get an "x".

You said you don’t own ex.com, so how are you in ex.com? Host file redirect?

I’ve mostly tested on WAMP on my own computers. The only thing I’ve changed of the suggested code you sent is replacing ex.com with my actual WAMP folder name. I simply used ex.com because I first thought to use “example.com” and shortened it. When I looked up IF ELSE at w3schools here http://w3schools.com/php/php_if_else.asp they started with one < sign for example of a date<“20”, and being a NOOB I cjanged that to an equal sign .

When your suggested second equal sign just outputted the second echo instead of the first, I reverted it, but looking at another w3school page for this reply here http://www.w3schools.in/php-tutorial/if-else/ I see it’s needed.

Thanks for putting up with me so far.

I think you should

[php]echo ‘root domain’ . $root_domain[/php]

Right before your comparison if statement. I have a feeling, it’s not what you expect on your wamp server.

That (my latest script with another equal sign…I’m learning!) put out “root domainex.comt” When I tried it on my other domain I’ll call ow.com I get “root domainow.com” so the letter disappears in WAMP like it should. Actually, I also had to add a semi-colon to your suggestion to get rid of an error message. I then tested on one of “my” hosts but “ex.com” doesn’t get the echo letter.

With WAMP I need just the letter t or nothing and none of the text your suggestion is showing and I might be able to work it out. Just because you’ll probably laugh, in the spirit of experimentation I replaced your suggestion of:[php]echo ‘root domain’ . $root_domain[/php]

with…
[php]echo “”;[/php] and that worked for WAMP, but at the host, ex.com still didn’t get a letter.

OK! Thanks. I found a typo on my host. When I used echo “”; I’ve got it working the way I need it on my host too!

Sponsor our Newsletter | Privacy Policy | Terms of Service