开发者

PHP: return current URL

开发者 https://www.devze.com 2023-03-03 05:59 出处:网络
With php, I wan开发者_C百科t to return the current url of the page I am currently on. for example, if this script is run on http://www.google.com, I want to echo out \'google\' sans http://

With php, I wan开发者_C百科t to return the current url of the page I am currently on.

for example, if this script is run on http://www.google.com, I want to echo out 'google' sans http://

OR

if this script is run on http://173.244.195.179, I want to echo out '173.244.195.179' sans http://

I've looked at $_SERVER but haven't been able to get it to work. Suggestions?


$domain = $_SERVER['HTTP_HOST'];
$ar = explode('.', $domain);

echo $ar[0];

Maybe?

EDIT: (Supports subdomains)

function domain()
{
    $ends = array('net','com','info','org');

    $domain = $_SERVER['HTTP_HOST'];
    $ar = explode('.', $domain);

    $result = '';

    $i = 0;
    $found = false;
    for($i; $i<sizeof($ar); $i++)
    {
        $j = 0;
        for($j; $j<sizeof($ends); $j++)
        {
            if($ends[$j] == $ar[$i]) $found = true;
        }

        if($found) break;
        $result .= $ar[$i] . '.';
    }

    return substr($result, 0, strlen($result)-1);
}

echo domain();

I'm going to put my money on that there's a way simpler or inbuilt way of doing this.

0

精彩评论

暂无评论...
验证码 换一张
取 消