2

Possible Duplicate:
getting current URL
PHP and invoking url?

Say somebody is looking for

http://subdomain.domainname.com/somedirectory/somefile.htm

What $_SERVER variable contain http://subdomain.domainname.com/somedirectory/somefile.htm

Community
  • 1
  • 1
user4951
  • 32,206
  • 53
  • 172
  • 282

4 Answers4

18
// Get HTTP/HTTPS (the possible values for this vary from server to server)
$myUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] && !in_array(strtolower($_SERVER['HTTPS']),array('off','no'))) ? 'https' : 'http';
// Get domain portion
$myUrl .= '://'.$_SERVER['HTTP_HOST'];
// Get path to script
$myUrl .= $_SERVER['REQUEST_URI'];
// Add path info, if any
if (!empty($_SERVER['PATH_INFO'])) $myUrl .= $_SERVER['PATH_INFO'];
// Add query string, if any (some servers include a ?, some don't)
if (!empty($_SERVER['QUERY_STRING'])) $myUrl .= '?'.ltrim($_SERVER['REQUEST_URI'],'?');

echo $myUrl;

...is my most resilient routine for this.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • 1
    +1 Without testing, this looks like the most complete answer. – Michael Mior Dec 05 '11 at 17:26
  • `$_SERVER['REQUEST_URI']` includes the path info and query string if present, so this is doubling those up for me. Removing the `PATH_INFO` and `QUERY_STRING` lines fixes it (YMMV, server configs may be different). – Paul Calcraft Dec 05 '17 at 17:32
6

You might like to try:

echo '<pre>';
print_r($_SERVER);
echo '</pre>';

A combination of $_SERVER['HTTP_HOST'] and $_SERVER['SCRIPT_NAME'] or $_SERVER['PHP_SELF'] should be what you are looking for.

Ian Jamieson
  • 4,376
  • 2
  • 35
  • 55
5

You need to build it yourself;

echo $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

You can also use $_SERVER['HTTPS'] to detect if HTTP or HTTPS.

I recommend using $_SERVER['REQUEST_URI'] as this is exactly what the user is looking for, before any rewrites or anything else, and includes GET variables

Tak
  • 11,428
  • 5
  • 29
  • 48
1

There is no full url contain in $_SERVER. But you can use this code:

$url = "http://" . $_SERVER['HTTP_HOST'] . "/" . $_SERVER['PHP_SELF'] . "/" . $_SERVER['QUERYSTRING'] . "/"; 
virushuo
  • 660
  • 3
  • 5