Вопрос

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

Это было полезно?

Решение

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

Другие советы

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

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;

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']));
$G_SYSTEMID = strstr($_SERVER['SERVER_NAME'], 'storeboard.com') ? 1 : 2;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top