PHP code used to obtain the URL address of the current page

Source: Internet
Author: User
Tags php code server array

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:
$ _ SERVER ['server _ name']:
$ _ SERVER ['server _ port']: 80
$ _ SERVER ['request _ URI ']:/Test. php
$ _ SERVER ['document _ root']:/home/var/www/
Therefore, the complete URL path is:

The code is as follows: Copy code

<? Php
Echo 'http: // '. $ _ SERVER ['server _ name']. $ _ SERVER ['request _ URI'];


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 ).


$ _ 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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.