Page 1 of 1
determining full url to page
Posted: Thu Sep 09, 2004 9:17 am
by newmember
let's say my site located at:
http://www.webhost.com/~me/main.php
there is a line inside main.php: header("Location: full_url");
I want to specify dynamically complete url in this header...
$_SERVER['script_name'] gives only relative path...
how do i determine the http://..... part ?
thanks
Posted: Thu Sep 09, 2004 9:30 am
by CoderGoblin
$_SERVER["SERVER_NAME"] gives you the server name part.
I don't know off hand how to determine the www part (should be automatic).
Why do you need the header("Location: full_url") when the relative path should send it correctly ?
As an aside the phpinfo() command is great to look up $_SERVER variables etc.
Have a phpinfo page consististing of
and take a look.
Posted: Thu Sep 09, 2004 9:43 am
by newmember
Why do you need the header("Location: full_url") when the relative path should send it correctly ?
i read that specs reqiure full url... so there are maybe UA's that doesn't understand relative path...
i did printed $_SERVER variable so that why i'm asking because i don't see there anything that would hold complete url...
php manual also says that:
If the script is running on a virtual host, this will be the value defined for that virtual host.
so i'm not sure whether it's good to use $_SERVER['SERVER_NAME']
in my case $_SERVER['SERVER_NAME'] contains ip and not dns name... is it always the case?
so i'm not sure if i can use this variable because of "virtual host" thing...
Posted: Thu Sep 09, 2004 10:05 am
by newmember
i just tried to give a relative url:
header('Location: /en/mainen.php');
and i got:
The requested URL /en/mainen.php was not found on this server.
Posted: Thu Sep 09, 2004 12:42 pm
by feyd
$_SERVER['HTTP_HOST']
if $_SERVER['HTTPS'] is set, then you are running under https, otherwise, it's an http request.
Posted: Thu Sep 09, 2004 1:21 pm
by newmember
ok thanks
i already solved this:
header('Location: http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/en/mainen.php');
i don't know if it is flexable enough (maybe i'll have to change it when i'll put it on real webhost) but for now it works...
Posted: Thu Sep 09, 2004 2:13 pm
by timvw
Code: Select all
function geturl()
{
$ports = array('https' => 443, 'http' => 80);
$prefix = empty($_SERVER['HTTPS']) ? 'http' : 'https';
$url = $prefix;
$url .= $_SERVER['SERVER_PORT'] != $ports[$prefix] ? ':' . $_SERVER['SERVER_PORT'] : '';
$url .= '://';
$url .= $_SERVER['HTTP_HOST'];
$url .= $_SERVER['REQUEST_URI'];
return $url;
}
Posted: Thu Sep 09, 2004 3:58 pm
by evilmonkey
Wow, nice function.

Posted: Thu Sep 09, 2004 4:02 pm
by feyd
needs to swap a few lines though

Posted: Thu Sep 09, 2004 6:24 pm
by newmember
thanks for that piece of art:)