Home  >  Article  >  Backend Development  >  Usage of QUERY_STRING, REQUEST_URI in php $_SERVER

Usage of QUERY_STRING, REQUEST_URI in php $_SERVER

高洛峰
高洛峰Original
2016-11-29 15:37:202068browse

The $_SERVER function is often used in the process of writing programs. Sometimes not knowing much about variables can cause big misunderstandings. Today I found a few small examples to illustrate the usage of four commonly used variables.

$_SERVER stores the current server information, there are several values ​​​​as follows:

$_SERVER["QUERY_STRING"];

$_SERVER["REQUEST_URI"];

$_SERVER["SCRIPT_NAME"],

$_SERVER ["PHP_SELF"];

It is often easy to confuse. The following is a detailed explanation of the differences between the QUERY_STRING, REQUEST_URI, SCRIPT_NAME and PHP_SELF variables in the $_SERVER function through examples. Master the relationship between the four to facilitate the correct acquisition of the required values ​​​​in practical applications. , for reference.

1, $_SERVER["QUERY_STRING"]

Description: The string of the query

2, $_SERVER["REQUEST_URI"]

Description: The URI required to access this page

3. $_SERVER["SCRIPT_NAME"]

Description: Contains the path of the current script

4. $_SERVER["PHP_SELF"]

Description: The file name of the currently executing script

Example 1, the code is as follows:

$_SERVER["QUERY_STRING"] = ""

$_SERVER["REQUEST_URI"] = "/"

$_SERVER["SCRIPT_NAME"] = "/index.php"

$_SERVER["PHP_SELF"] = " /index.php"

Example 2, the code is as follows:

$_SERVER["QUERY_STRING"] = "tags/upload"

$_SERVER["REQUEST_URI"] = "/?tags/upload"

$_SERVER[ "SCRIPT_NAME"] = "/index.php"

$_SERVER["PHP_SELF"] = "/index.php"

Example 3, the code is as follows:

$_SERVER["QUERY_STRING"] = "tags/upload/ 2"

$_SERVER["REQUEST_URI"] = "/index.php?tags/upload/2"

$_SERVER["SCRIPT_NAME"] = "/index.php"

$_SERVER["PHP_SELF"] = "/index.php"

$_SERVER["QUERY_STRING"] obtains the query statement. As can be seen from the example, what is obtained is the value behind ?

$_SERVER["REQUEST_URI"] obtains the value behind http://www.phpfensi.com The value, including /

$_SERVER["SCRIPT_NAME"] Get the path of the current script, such as: index.php

$_SERVER["PHP_SELF"] The file name of the currently executing script

To summarize: for QUERY_STRING, REQUEST_URI , SCRIPT_NAME and PHP_SELF, an in-depth understanding will help us correctly call these four values ​​​​in the $_SERVER function. We will use examples to explain the differences between the four variables QUERY_STRING, REQUEST_URI, SCRIPT_NAME and PHP_SELF in the $_SERVER function.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn