In php, the method to obtain the url address of the current page is very simple. Next I will summarize the examples of getting the complete url address using the relevant functions.
The predefined variable $ _ SERVER of PHP is an array containing headers, path information, and script location. The entity of the array is created by the web SERVER.
Using the $ _ SERVER array, we can obtain the complete URL path and actual path, so that we can use them as we like.
$ _ SERVER ['server _ name'] // NAME of the host on which the script is currently running.
$ _ SERVER [SERVER_PORT '] // port used by the user to connect to the SERVER.
$ _ SERVER ['request _ URI '] // The request url except the Host Name of the currently running script.
$ _ SERVER ['document _ root'] // ROOT directory of the DOCUMENT where the script is currently running.
Example URL: http://xsooo.com/Test/Test.php
$ _ SERVER ['server _ name']: xsooo.com
$ _ SERVER ['server _ port']: 80
$ _ SERVER ['request _ URI ']:/Test. php
$ _ SERVER ['document _ root']:/home/var/www/xsooo.com/htdocs
Therefore, the complete URL path is:
The Code is as follows: |
Copy code |
<? Php Echo 'HTTP: // '. $ _ SERVER ['server _ name']. $ _ SERVER ['request _ URI']; ?> |
Note:
1. The default http PORT is 80. Therefore, $ _ SERVER ['server _ port'] is not included in the complete URL path. Of course, this is a special case!
2. someone will use $ _ SERVER ['remote _ host'] to obtain the HOST name, but it means that the HOST name of the user is browsing the current page (this variable must be created by the Web SERVER ).
Extension:
$ _ SERVER ['remote _ host ']
Modify the HostnameLookups In the httpd. conf configuration file of the apache server
Here I want to summarize some information about the path where the URL and file in the address bar belong to me:
With $ _ SERVER [], we can accomplish many things: it is an array containing headers, path information, and script positions. The array entity is created by the web SERVER.
This is a "superglobal", or it can be described as an automatic global variable. This only means that it is valid in all scripts. You do not need to use global $ _ SERVER; to access a function or method, just like using $ HTTP_SERVER_VARS.
$ HTTP_SERVER_VARS contains the same information, but it is not an automatic global variable. (Note: $ HTTP_SERVER_VARS and $ _ SERVER are different variables. PHP processes them differently .) If the register_globals command is set, these variables are also available in all scripts; that is, the $ _ SERVER and $ HTTP_SERVER_VARS arrays are separated.
$ _ SERVER ['HTTP _ host'] retrieves the HOST of the current request: header content
$ _ SERVER ['php _ SELF '], which is probably the most frequently used one. It returns the name of the currently called page, if it is http: // localhost/test/2005/test. php,/test/2005/test will be returned. php
$ _ SERVER ['script _ name'] returns the path containing the current SCRIPT. This is useful when the page needs to point to itself.
$ _ SERVER ['script _ filename'] It returns the absolute path of the current file.
$ _ SERVER ['request _ URI '] returns the URI required to access this page, including "/"
Of course there are many path functions:
Dirname (), returns the directory section in the path information, which is prefixed "/"
Basename () returns the basic file name in the path. You can also set a suffix to control the output.
Realpath (), returns the path information of the absolutely normalized path