Reputation: 1267
I'm looking for a way to get a page address exactly how it is displayed in the address bar of the browser.
My site is basically a PHP script and my goal is to determine if the users use http://mysite.com or http://www.mysite.com or just mysite.com or www.mysite.com to access the site. I hope to use this information to redirect the link so it would be in a single format. This is required for some third party tools I'm using.
so what I'm askin is if it's possible to get the url of a site, in PHP exactly how the browser is requesting it.
Upvotes: 0
Views: 3549
Reputation: 23597
The first two lines of an HTTP request look like:
GET /index.php
Host: www.mysite.com
The first line specifies the local resource (usually a file in your web directory), and the second specifies what hostname the user entered to find your site (this is especially useful for servers running multiple virtual web hosts). PHP allows you to extract both of these:
$_SERVER['HTTP_HOST']
$_SERVER['PHP_SELF']
Technically, $_SERVER['PHP_SELF']
specifies the filepath of the current PHP script relative to the root directory of this web host, but that should, in practice, be the same as the resource listed on the first line of the HTTP request.
As the other responses have mentioned, there's no way to tell whether the user typed http://
or not.
Upvotes: 2
Reputation: 2676
You cannot determine whether or not the user typed http:// or left it off directly from PHP. PHP will only be able to tell the final domain name ($_SERVER['HTTP_HOST']
). The other functionality is handled in the browser.
Upvotes: 0
Reputation: 324750
You can tell the difference between www.mysite.com
and mysite.com
by looking at $_SERVER['HTTP_HOST']
, however the browser will always automatically add http://
to a URL (and some browsers hide it from the users as unnecessary information) so there's no way to know what they actually typed in.
Upvotes: 3