简体   繁体   中英

PHP get address as in browser address bar

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.

您可以通过查看$_SERVER['HTTP_HOST']来区分www.mysite.commysite.com之间的区别,但是浏览器将始终自动将http://添加到URL(某些浏览器将http://隐藏为不必要的信息),因此无法知道他们实际输入的内容。

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM