Teach you how to play $_SERVER
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['PHP_SELF']; echo "<br>"; echo $_SERVER['SERVER_NAME']; echo "<br>"; echo $_SERVER['HTTP_HOST']; echo "<br>"; echo $_SERVER['HTTP_REFERER']; echo "<br>"; echo $_SERVER['HTTP_USER_AGENT']; echo "<br>"; echo $_SERVER['SCRIPT_NAME']; ?>
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 athttp://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 modifyingHTTP_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 theApache
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
