Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What $_SERVER variable provide full URL [duplicate]

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

like image 900
user4951 Avatar asked Jun 30 '26 08:06

user4951


1 Answers

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

like image 167
DaveRandom Avatar answered Jul 02 '26 22:07

DaveRandom