Vote
1
How to find the full URL of the page in PHP in a platform independent and configuration independent way
Angsuman Chakraborty
June 3rd, 2005
Without much ado, I present the script.
It handles http and https URL's and should work across platforms and configurations (like using htaccess for clean url's etc.).
To understand more about the issues involved read - Understanding $_SERVER[’PHP_SELF’], $PHP_SELF, $_SERVER[’REQUEST_URI’] and $_SERVER[’SCRIPT_NAME’] in PHP and when to use what
$_SERVER['FULL_URL'] = 'http';
$script_name = ";
if(isset($_SERVER['REQUEST_URI'])) {
$script_name = $_SERVER['REQUEST_URI'];
} else {
$script_name = $_SERVER['PHP_SELF'];
if($_SERVER['QUERY_STRING']>' ') {
$script_name .= '?'.$_SERVER['QUERY_STRING'];
}
}
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on') {
$_SERVER['FULL_URL'] .= 's';
}
$_SERVER['FULL_URL'] .= '://';
if($_SERVER['SERVER_PORT']!='80′) {
$_SERVER['FULL_URL'] .=
$_SERVER['HTTP_HOST'].':'.$_SERVER['SERVER_PORT'].$script_name;
} else {
$_SERVER['FULL_URL'] .= $_SERVER['HTTP_HOST'].$script_name;
}
Filed under Headline News, How To, PHP, Web, Web Services |
|
RSS 2.0 |
Trackback this Article
|
Email this Article
You may also like to read |





































June 6th, 2005 at 8:56 am
The generated url will work, but the port for https is 443. And the IIS may not set $_SERVER['HTTP_HOST']:
‘ ‘) {
$script_name .= ‘?’.$_SERVER['QUERY_STRING'];
}
}
if (isset($_SERVER['HTTP_HOST'])) {
$_SERVER['FULL_URL'] .= $_SERVER['HTTP_HOST'].$port.$script_name;
} else {
$_SERVER['FULL_URL'] .= $_SERVER['SERVER_NAME'].$port.$script_name;
}
?>
June 6th, 2005 at 8:58 am
The uploaded code-snipped was broken:
$port = ”;
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']==’on’) {
$_SERVER['FULL_URL'] = ‘https://’;
if($_SERVER['SERVER_PORT']!=’443′) {
$port = ‘:’ . $_SERVER['SERVER_PORT'];
}
} else {
$_SERVER['FULL_URL'] = ‘http://’;
if($_SERVER['SERVER_PORT']!=’80′) {
$port = ‘:’ . $_SERVER['SERVER_PORT'];
}
}
if(isset($_SERVER['REQUEST_URI'])) {
$script_name = $_SERVER['REQUEST_URI'];
} else {
$script_name = $_SERVER['PHP_SELF'];
if($_SERVER['QUERY_STRING']>’ ‘) {
$script_name .= ‘?’.$_SERVER['QUERY_STRING'];
}
}
if (isset($_SERVER['HTTP_HOST'])) {
$_SERVER['FULL_URL'] .= $_SERVER['HTTP_HOST'].$port.$script_name;
} else {
$_SERVER['FULL_URL'] .= $_SERVER['SERVER_NAME'].$port.$script_name;
}
April 18th, 2006 at 4:58 pm
I found this script quite useful
. TNX!
August 31st, 2006 at 5:09 am
[...] Configuration [...]
September 7th, 2006 at 7:26 am
[...] [...]
August 22nd, 2008 at 12:22 pm
This script will NOT take in to account any redirects. They will show up in $_SERVER['REDIRECT_URL'] and $_SERVER['REDIRECT_QUERY_STRING'] but they are not bullet proof. It depends on how the redirect is made.