Question

With php, I want 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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top