#测试网址: http://localhost/blog/testurl.php?id=5
Get domain name or host address
echo $_server[' Http_host ']. " <br> "; #localhost
Get web Address
echo $_server[' php_self ']. " <br> "; #/blog/testurl.php
Get URL parameters
echo $_server["Query_string"]. " <br> "; #id =5
Get user Agent
echo $_server[' Http_referer ']. " <br> ";
Get the full URL
Echo ' http://' $_server[' http_host '].$_server[' Request_uri '];
Echo ' http://'. $_server[' http_host '].$_server[' php_self ']. $_server[' query_string '];
#http://localhost/blog/testurl.php?id=5
Full URL with port number
Echo ' http://'. $_server[' server_name ', '. '. $_server["Server_port"].$_server["Request_uri"];
#http://localhost:80/blog/testurl.php?id=5
Fetch path only
$url = ' http://' $_server[' server_name '].$_server["Request_uri"];
echo dirname ($url);
#http://localhost/blog
The difference between server_name and http_host in PHP $_server
Same point:
The same information is output when the following three conditions are met.
1. Server is 80 port
2. ServerName is set correctly in Apache Conf
3. http/1.1 Protocol Specification
Different points:
1. The usual situation:
_server["Http_host"] under the http/1.1 protocol specification, the information is output based on the HTTP request of the client.
_server["SERVER_NAME"] by default, the ServerName value in the Apache configuration file httpd.conf is output directly.
2. When the server is not 80 ports:
_server["Http_host"] outputs the port number, for example: mimiz.cn:8080
_server["SERVER_NAME"] will output servername value directly
So in this case, it can be understood as: Http_host = Server_name:server_port
3. When servername in profile httpd.conf is inconsistent with the domain name requested by http/1.0:
The httpd.conf configuration is as follows:
<virtualhost *>
ServerName mimiz.cn
Serveralias www.mimiz.cn
</virtualhost>
Client Access domain name www.mimiz.cn
_server["Http_host"] Output www.mimiz.cn
_server["SERVER_NAME"] Output mimiz.cn
Therefore, in the actual procedure, should try to use _server["Http_host", more insurance and reliable.
Get the full URL of the current page in PHP & the difference between server_name and http_host in PHP $_server