How to find the full URL of the page in PHP in a platform independent and configuration independent way

By Angsuman Chakraborty, Gaea News Network
Friday, June 3, 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

Tags:
Discussion

Md. Irshad
April 6, 2010: 12:21 am

You can find full url from this coding —->
$url = parse_url($_SERVER['REQUEST_URI']);
print $url['fragment'];


Md Irshad
April 5, 2010: 7:44 am

You can find URL throug the js…

document.write (document.location.href);


Md Irshad
April 5, 2010: 7:42 am

You can find the full url from the javascript and after that you can convert in PHP. Script is

var aa = document.location.href;

May 8, 2009: 10:37 am

hii thanks for the above info,
but its not satisfying my requirement

what i need to trace is not ust url with query strin but i also need to trace #href

i.e. if my url is
https://jigishthakar.com/?p=2#cid

then till
https://jigishthakar.com/?p=2 ur code works fine but now how to trace #cid ?

October 15, 2008: 12:25 pm

Thank you for sharing.

August 22, 2008: 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.

August 31, 2006: 5:09 am

[...] Configuration [...]


oscar
April 18, 2006: 4:58 pm

I found this script quite useful :) . TNX!


Nils
June 6, 2005: 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'] = ‘https://’;
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;
}


Nils
June 6, 2005: 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;
}
?>

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :