-4

I have been struggling with this for a week or two now, and I just cant seem to resolve it. Here is my Script:

<?php
$Domain = $_SERVER['SERVER_NAME'];
$paryDomain = explode(".",$Domain);
$Array = count($paryDomain);
$RootDomain = "";
$G_SYSTEMID = "";

if ($Array == "1")
{
    $RootDomain = $paryDomain[0].'.'.$paryDomain[1];
}    
elseif ($Array == "2")
{
    $RootDomain = $paryDomain[1].'.'.$paryDomain[2];
}

if ($RootDomain == "storeboard.com")
{
    $G_SYSTEMID = 1;
}
elseif ($RootDomain == "dcwn.org")
{
    $G_SYSTEMID = 2;
}

echo $G_SYSTEMID;
echo "------------";

?>

Why do I get no result at the end either 1 or 2..?

Any help would be greatly appreciated.

Neojakey

Boaz
  • 19,892
  • 8
  • 62
  • 70
neojakey
  • 1,633
  • 4
  • 24
  • 36
  • 1
    I would say the obvious cause is that $RootDomain is neither of the strings you compare it too... Maybe you can echo it and see what the actual value is...? – MrHug Feb 22 '13 at 21:47
  • Start echoing out variables at various points throughout the logic and see where they start not being what you're expecting. – ceejayoz Feb 22 '13 at 21:47
  • 15
    "What am I doing wrong" <- not using an appropriate title. – James Coyle Feb 22 '13 at 21:47
  • 1
    Maybe you should check as INTEGER not as STRING... `$Array == "1"` is string due to `$Array == 1` is INTEGER. If you `count($var);` you will get result as INTEGER. – Marin Sagovac Feb 22 '13 at 21:48
  • Yes do the test as an integer and dump the variables. – COil Feb 22 '13 at 21:49
  • 1
    Use var_dump($paryDomain) to see the content of your explode operation, that could help. Other thing, try using strcmp to compare strings http://php.net/manual/en/function.strcmp.php – legrandviking Feb 22 '13 at 21:49
  • Some one is on a down voting frenzy today lol – Sir Feb 22 '13 at 21:57
  • Many thanks for all your help, and my apologies for the inappropriate title.. – neojakey Feb 22 '13 at 22:23

5 Answers5

2
if ($Array == 1)
{
    $RootDomain = $paryDomain[0]; 
}    
elseif ($Array == 2)
{
    $RootDomain = $paryDomain[0].'.'.$paryDomain[1];
}    
elseif ($Array == 3)
{
    $RootDomain = $paryDomain[1].'.'.$paryDomain[2];
} else {
    //to debug the possible cause 
    $G_SYSTEMID = 'Array length was equal to '.$Array;
    $RootDomain = false;
}

You had your array count not matching the pointers you were using this should help. You were also checking for strings instead of integers...

Sir
  • 8,135
  • 17
  • 83
  • 146
  • I made this change an still got no output from the script.. Any other suggestions..? – neojakey Feb 22 '13 at 21:54
  • @neojakey see edit :) you had to use count function to count array length and you had the array pointer numbers incorrect :) arrays start at `0` – Sir Feb 22 '13 at 21:54
  • @neojakey third edit ive added an extra if to take into account all options – Sir Feb 22 '13 at 22:00
  • +1 Dave that is true, I also explained it in detail –  Feb 22 '13 at 22:06
1
$Array = count($paryDomain);
if ($Array == "1")
{
    $RootDomain = $paryDomain[0].'.'.$paryDomain[1];
} 

if $Array contains one element then $paryDomain[1] is not exist, or the array must contain only one element.

for whose that says about $Array that is integer:

$Array = 1;
var_dump(($Array == "1"));
var_dump(($Array == "2"));

bool(true) 

bool(false)

WHAT IS WRONGS IS: Your server name if any ((www.)?example.com):

if ($Array == "1")
{
    $RootDomain = $paryDomain[0].'.'.$paryDomain[1];
}    
elseif ($Array == "2")
{
    $RootDomain = $paryDomain[1].'.'.$paryDomain[2];
}

Because $paryDomain = explode(".",$Domain); at least contains two element, then, $Array is never equals to one, only in one cases, when SERVER_NAME is one word!

Solution:

if ($Array == "2")
{
    $RootDomain = $paryDomain[0].'.'.$paryDomain[1];
}    
elseif ($Array == "3")
{
    $RootDomain = $paryDomain[1].'.'.$paryDomain[2];
}else{

}

if ($RootDomain == "storeboard.com")
{
    $G_SYSTEMID = 1;
}
elseif ($RootDomain == "dcwn.org")
{
    $G_SYSTEMID = 2;
}else{
//in case when none of above cases true
    $G_SYSTEMID = 100;
} 
1

Just wanted to show you an easier way to do this. It will support domains with unlimited sub-domains as it grabs the last two parts always:

if (strpos($_SERVER['SERVER_NAME'], ".") !== false) {
    preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z]+)$/i', $_SERVER['SERVER_NAME'], $item);
    $RootDomain = $item['domain'];
} else {
    $RootDomain = $_SERVER['SERVER_NAME'];
}

switch ($RootDomain)
{
    case 'storeboard.com':
        $G_SYSTEMID = 1;
        break;
    case 'dcwn.org':
        $G_SYSTEMID = 2;
        break;
    default:
        $G_SYSTEMID = '';
        break;
}

echo $G_SYSTEMID;
kittycat
  • 14,983
  • 9
  • 55
  • 80
  • 1
    @Baba should `\.[a-z\.]{2,6})` be changed to `\.[a-z]+)`? I think the period is a typo right? Also this would limit the TLD to 2-6 chars, but ICANN has opened up the [purchase](http://www.techspot.com/news/47031-icann-selling-custom-top-level-domains-for-185000.html) of TLDs so we can have domains like mysite.somefancytld in the future. – kittycat Feb 26 '13 at 13:11
  • `ICANN selling custom top-level domains for $185,000` wow .. thanks for the information .... you can change it – Baba Feb 26 '13 at 13:12
0

First echo your $Domain

echo $_SERVER['SERVER_NAME'];

Then print_r your $paryDomain

print_r($paryDomain);

And if these are correct, echo your count()

echo count(explode(".",$_SERVER['SERVER_NAME']));
Stef Geysels
  • 1,023
  • 11
  • 27
-1
$G_SYSTEMID = strstr($_SERVER['SERVER_NAME'], 'storeboard.com') ? 1 : 2;
kittycat
  • 14,983
  • 9
  • 55
  • 80
slattman
  • 98
  • 6