Home  >  Article  >  Backend Development  >  Teach you how to play $_SERVER

Teach you how to play $_SERVER

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-08-02 14:27:462184browse

In the previous article, we learned about what super global variables are, and about $GLOBALS. If necessary, please read "The editor will teach you $GLOBALS in variables in PHP". This time we introduce $_SERVER to you, you can refer to it if necessary.

In order for us to understand this array, let's look at a little chestnut.

<?php 
echo $_SERVER[&#39;PHP_SELF&#39;];
echo "<br>";
echo $_SERVER[&#39;SERVER_NAME&#39;];
echo "<br>";
echo $_SERVER[&#39;HTTP_HOST&#39;];
echo "<br>";
echo $_SERVER[&#39;HTTP_REFERER&#39;];
echo "<br>";
echo $_SERVER[&#39;HTTP_USER_AGENT&#39;];
echo "<br>";
echo $_SERVER[&#39;SCRIPT_NAME&#39;];
?>

The result is

/try/demo_source/demo_global_server.php
www.runoob.com
www.runoob.com
https://www.runoob.com/try/showphp.php?filename=demo_global_server
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36
/try/demo_source/demo_global_server.php

Let’s take a rough look at the results first. It seems that they are all the same address. Then we can boldly guess that this array is an array that stores information. Let’s Let's go verify it.

$_SERVER is an array, including header information (header), path (path), and script locations (script locations) and other information. The items in this array are created by the web server. There is no guarantee that every server will serve all items; servers may ignore some items or serve items not listed here.

Let’s talk about the important elements in all $_SERVER variables.

  • $_SERVER['PHP_SELF'], the file name of the currently executing script, related to the document root. For example, using $_SERVER['PHP_SELF'] in a script at http://example.com/test.php/foo.bar will result in /test .php/foo.bar. The __FILE__ constant contains the full path and filename of the current (i.e. containing) file. Starting with PHP version 4.3.0, this variable will contain the script name if PHP is running in command line mode. This variable is not available in previous versions.

  • $_SERVER['GATEWAY_INTERFACE'], the version of the CGI specification used by the server; for example, "CGI/1.1".

  • $_SERVER['SERVER_ADDR'], the IP address of the server where the script is currently running.

  • $_SERVER['SERVER_NAME'], the host name of the server where the script is currently running. If the script is running on a virtual host, the name is determined by the value set for that virtual host.

  • $_SERVER['SERVER_SOFTWARE'], server identification string, given in the header information when responding to the request. (For example: Apache/2.2.24)

  • $_SERVER['SERVER_PROTOCOL'], the name and version of the communication protocol when requesting the page. For example, "HTTP/1.0".

  • $_SERVER['REQUEST_METHOD'], the request method used to access the page; for example, "GET", "HEAD", "POST", "PUT".

  • $_SERVER['REQUEST_TIME'], the timestamp when the request started. Available since PHP 5.1.0. (For example: 1377687496)

  • $_SERVER['QUERY_STRING'], query string (query string), if any, page access is performed through it.

  • $_SERVER['HTTP_ACCEPT'], the content of the Accept: item in the current request header, if it exists.

  • $_SERVER['HTTP_ACCEPT_CHARSET'], the content of the Accept-Charset: item in the current request header, if it exists. For example: "iso-8859-1,*,utf-8".

  • $_SERVER['HTTP_HOST'], the content of the Host: item in the current request header, if it exists.

  • $_SERVER['HTTP_REFERER'], directs the user agent to the address of the previous page of the current page (if one exists). Determined by user agent settings. Not all user agents will set this item, and some also provide the function of modifying HTTP_REFERER. In short, the value is not trustworthy. )

  • $_SERVER['HTTPS'], is set to a non-empty value if the script is accessed via the HTTPS protocol.

  • $_SERVER['REMOTE_ADDR'], the IP address of the user browsing the current page.

  • $_SERVER['REMOTE_HOST'], the host name of the user browsing the current page. DNS reverse resolution does not depend on the user's REMOTE_ADDR.

  • $_SERVER['REMOTE_PORT'], the port number used on the user's machine to connect to the web server.

  • $_SERVER['SCRIPT_FILENAME'], the absolute path of the currently executing script.

  • $_SERVER['SERVER_ADMIN'], this value specifies the SERVER_ADMIN parameter in the Apache server configuration file. If the script is running on a virtual host, this value is that of that virtual host.

  • $_SERVER['SERVER_PORT'], the port used by the web server. The default value is "80". If using SSL secure connection, this value is the HTTP port set by the user.

  • $_SERVER['SERVER_SIGNATURE'], a string containing the server version and virtual host name.

  • $_SERVER['PATH_TRANSLATED'], the base path of the file system (not the document root directory) where the current script is located. This is the result after the server has been imaged from a virtual to real path.

  • $_SERVER['SCRIPT_NAME'], contains the path of the current script. This is useful when the page needs to point to itself. __FILE__ Constant contains the full path and file name of the current script (i.e. include file).

  • $_SERVER['SCRIPT_URI'], URI is used to specify the page to be accessed. For example "/index.html".

That’s all. If you want to know anything else, you can click this. → →php video tutorial

The above is the detailed content of Teach you how to play $_SERVER. For more information, please follow other related articles on the PHP Chinese website!

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