PHP obtains the functions and parameters of the current URL.

Source: Internet
Author: User
Tags http authentication url example
[PHP] predefined variable (1) $ _ server (Appendix: getting the current URL example) For example, I need to obtain the current URL address.

$ Url_this ="Http: // ". $ _ Server['HTTP _ host']. $ _ server ['php _ Self '];
Echo $ url_this;

Then:Http: // localhost/Lu. php


Server variable: $ _ Server

Note: PhP 4.1.0 and later versions are used. In earlier versions, $ http_server_vars is used.

$ _ Server is an array containing headers, paths, and script locations. The object of the array is created by the Web server. It is not guaranteed that all servers can generate all information. The server may ignore some information or generate new information not listed below. This means that a large number of these variables are described in CGI 1.1 specification, so you should study it carefully.

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. For more information, see the security section using register globals. These independent global variables are not automatic global variables.

You may find that some $ _ server elements listed below are not available. Note: If you run php In the command line mode, the elements listed below are hardly valid (or have no practical significance ).


The file name of the script being executed, which is related to document root. For exampleHttp:// $ _ server ['php _ Self '] in the script to get the result of/test. php/Foo. Bar.

If PHP is run as a command line, the variable is invalid.

Parameters passed to the script. When the script runs in the command line mode, the argv variable is passedProgramC-style command line parameters. When the get method is called, the variable contains the requested data.

Contains the number of command line parameters passed to the Program (if it is in command line mode ).

The CGI Specification Version Used by the server. For example, "cgi/1.1 ".

'Server _ name'
Name of the server host where the script is currently running. If the script runs on a VM, the name is determined by the value set by that VM.

'Server _ soft'
The string identified by the server, which is in the header of the Response Request.

The name and version of the communication protocol on the request page. For example, "HTTP/1.0 ".

The request method used to access the page. For example, "get", "head", "Post", and "put ".

Query string.

The document root directory where the script is currently running. Defined in the server configuration file.

The accept of the current request: the content in the header.

Accept-charset of the current request: Content in the header. Example: "iso-8859-1, *, UTF-8 ".

Accept-encoding of the current request: Content in the header. For example, "gzip ".

The accept-language of the current request: the content in the header. For example, "en ".

Connection of the current request: Content in the header. For example, "keep-alive ".

Host of the current request: Content in the header.

The URL of the previous page that is linked to the current page. Not all user proxies (browsers) will set this variable, and some can also manually modify http_referer. Therefore, this variable is not always true.

User_agent of the current request: Content in the header. This string indicates the information of the User Agent accessing the page. A typical example is Mozilla/4.5 [En] (X11; U; Linux 2.2.9 i586 ). You can also use get_browser () to obtain this information.

Browsing the IP address of the user on the current page.

'Remote _ host'
The host name of the user browsing the current page. Reverse Domain name resolution is based on the user's remote_addr.

Note: You must configure the Web server to create this variable. For example, Apache needs to include hostnamelookups on in httpd. conf. See gethostbyaddr ().

The port used by the user to connect to the server.

The absolute path name of the currently executed script.

This value specifies the server_admin parameter in the Apache server configuration file. If the script runs on a VM, this value is the value of that VM.

The port used by the server. The default value is "80 ". If you use an SSL secure connection, this value is the http port you set.

A string containing the server version and virtual host name.

The basic path of the file system (not the document root directory) where the current script is located. This is the result of a virtual image to a real path on the server.

The path that contains the current script. This is useful when the page needs to point to itself.

The URI required to access this page. For example, "/index.html ".

When PHP runs in the Apache module mode and uses the HTTP authentication function, this variable is the user name entered by the user.

When PHP runs in the Apache module mode and is using the HTTP authentication function, this variable is the password entered by the user.

When PHP runs in the Apache module mode and uses the HTTP authentication function, this variable is the authentication type.

========================================================== ========================================================== ==========

PHP retrieves the functions and server variables of the current URL path:
Code As follows:
<? PHP
Echo $ _ server ['document _ root']. "<br>"; // obtain the root variable of the server document.
Echo $ _ server ['php _ Self ']. "<br>"; // get the absolute path variable of the file server that executes the code
Echo _ file _. "<br>"; // obtain the absolute path variable of the file system.
Echo dirname (_ file _); // function used to obtain the path of the folder where the file is located
// Server function
$ _ Server ["http_referer"] = http: // localhost/lianxi/
$ _ Server ["http_accept_language"] = ZH-CN
$ _ Server ["http_accept_encoding"] = gzip, deflate
$ _ Server ["http_user_agent"] = Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;. Net CLR 1.1.4322;. Net CLR 2.0.50727)
$ _ Server ["http_host"] = localhost
$ _ Server ["http_connection"] = keep-alive
$ _ Server ["path"] = c: \ windows \ system32; C: \ WINDOWS; c: \ windows \ system32 \ WBEM; C: \ Program Files \ common files \ Adobe \ AGL; C: \ Program Files \ mysql \ MySQL Server 5.0 \ bin; C: \ PHP \ ext
$ _ Server ["systemroot"] = C: \ WINDOWS
$ _ Server ["comspec"] = c: \ windows \ system32 \ cmd.exe
$ _ Server ["pathext"] =. com;. EXE;. BAT;. CMD;. vbs;. VBE;. js;. JSE;. WSF;. wsh
$ _ Server ["WINDIR"] = C: \ WINDOWS
$ _ Server ["server_signature"] =
Apache/2.0.55 (win32) PHP/5.1.1 server at localhost port 80 \ which server is used
$ _ Server ["server_software"] = Apache/2.0.55 (win32) PHP/5.1.1
$ _ Server ["SERVER_NAME"] = localhost \ Server Name
$ _ Server ["server_addr"] =
$ _ Server ["server_port"] = 80 \ Server Port
$ _ Server ["remote_addr"] =
$ _ Server ["document_root"] = D:/lianxi \ main directory of the website
$ _ Server ["server_admin"] = \ mailbox set when Apache is installed
$ _ Server ["script_filename"] = D:/lianxi/servervalues. php \ absolute path of the current webpage,
$ _ Server ["remote_port"] = 1076 \ remote port
$ _ Server ["gateway_interface"] = cgi/1.1
$ _ Server ["server_protocol"] = http/1.1
$ _ Server ["request_method"] = get
$ _ Server ["QUERY_STRING"] =\\ get? Content after no.
$ _ Server ["request_uri"] = example:/lianxi/servervalues. php? A = 1 & amp; B = 2
$ _ Server ["script_name"] = example:/lianxi/servervalues. php
$ _ Server ["php_self"] =/lianxi/servervalues. php \ returns the relative path of the current webpage.
$ _ Server ["request_time"] = 1179190013 \ The unit of running time is 100,000 in milliseconds
$ _ Server ["argv"] = Array
$ _ Server ["argc"] = 0
1, $ _ server ["QUERY_STRING"]
Description: query string
2, $ _ server ["request_uri"]
Description: The URI required to access this page
3, $ _ server ["script_name"]
Description: path containing the current script
4, $ _ server ["php_self"]
Description: File Name of the script being executed
1, the home page directly)
$ _ Server ["QUERY_STRING"] = ""
$ _ Server ["request_uri"] = "/"
$ _ Server ["script_name"] = "/index. php"
$ _ Server ["php_self"] = "/index. php"
2, /? P = 222 (with query)
$ _ Server ["QUERY_STRING"] = "P = 222 ″
$ _ Server ["request_uri"] = "/? P = 222 ″
$ _ Server ["script_name"] = "/index. php"
$ _ Server ["php_self"] = "/index. php"
3, P = 222 & Q = biuuu
$ _ Server ["QUERY_STRING"] = "P = 222 & Q = biuuu"
$ _ Server ["request_uri"] = "/index. php? P = 222 & Q = biuuu"
$ _ Server ["script_name"] = "/index. php"
$ _ Server ["php_self"] = "/index. php"
$ _ Server ["QUERY_STRING"]: Obtain the query statement. Which of the following statements can be obtained in the instance? Value after
$ _ Server ["request_uri"] gets the value after the, including/
$ _ Server ["script_name"] obtains the path of the current script, for example, index. php.
$ _ Server ["php_self"] File Name of the script currently being executed

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.

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.