Get the full URL of the current page in PHP & the difference between server_name and http_host in PHP $_server

Source: Internet
Author: User

#测试网址: 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.