Home >Backend Development >PHP Tutorial >Detailed introduction to PHP's $_SERVER (code example)

Detailed introduction to PHP's $_SERVER (code example)

不言
不言forward
2019-01-09 10:07:464960browse

This article brings you a detailed introduction (code example) about PHP's $_SERVER. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you

$_SERVER is a predefined super global variable in PHP. The so-called "super global variables" can be used in all scopes of the script. $_SERVER saves information about headers, paths and script locations. I often forget it at work, so I’ll record it here to deepen my impression. The test was conducted under Windows, the environment is Apache/2.4.23 (Win32) PHP/5.6.27-nts, the access domain name is http://www.example.com/index...., and the file directory is E: /WWW/example/.

Detailed explanation of the main content

  • $_SERVER["SCRIPT_NAME"] => "/index.php", current script path

  • $_SERVER["REQUEST_URI"] => "/index.php?id=1", the visited page URI, containing the query string

  • $_SERVER["QUERY_STRING"] => "id=1", query string, does not exist as " "

  • ##$_SERVER["REQUEST_METHOD"] => "GET ", request method, such as "POST", "PUT", etc.

  • $_SERVER["SERVER_PROTOCOL"] => "HTTP/1.1", the name and version of the communication protocol

  • $_SERVER["GATEWAY_INTERFACE"] => "CGI/1.1", the version of the CGI specification used by the server

  • $_SERVER["REMOTE_PORT "] => "60599", the port used by users to connect to the server

  • $_SERVER["SCRIPT_FILENAME"] => "E:/WWW/example/index.php", The absolute path of the current script

  • $_SERVER["DOCUMENT_ROOT"] => "E:/WWW/example/", the absolute path of the current script document root directory

  • $_SERVER["REMOTE_ADDR"] => "127.0.0.1", the user's IP address

  • $_SERVER["SERVER_PORT"] => " 80", the port used by the server

  • $_SERVER["SERVER_ADDR"] => "127.0.0.1", the IP address of the server

  • $_SERVER["SERVER_NAME"] => "www.example.com", the host name of the server, Note:

    If the script is run in a virtual host, the name is determined by the value set by that virtual host. In Apache 2, UseCanonicalName = On and ServerName must be set. Otherwise the value will be provided by the client and may be forged. This value should not be relied upon in environments where the context has security requirements.

  • ##$_SERVER["SERVER_SOFTWARE"] => "Apache/2.4.23 (Win32) OpenSSL/1.0.2j mod_fcgid/2.3.9", Server in the response header The content
  • $_SERVER["SERVER_SIGNATURE"] => "", a string containing the server version and virtual host name
  • $_SERVER["HTTP_HOST"] => "www.example.com", the content of the Host item in the request header
  • $_SERVER["HTTP_CONNECTION"] => "keep- alive", the content of the Connection item in the request header
  • ##$_SERVER["HTTP_PRAGMA"] => "no-cache", the content of the Pragma item in the request header
  • $_SERVER["HTTP_CACHE_CONTROL"] => "no-cache", the content of the Cache-Control item in the request header
  • ##$_SERVER["HTTP_UPGRADE_INSECURE_REQUESTS" ] => "1", the content of the Upgrade-Insecure-Requests item in the request header

  • $_SERVER["HTTP_USER_AGENT"] => "Mozilla/5.0 (Windows NT 10.0; Win64; ] => "text/html,application/xhtml xml,application/xml;q=0.9,image/webp,image/apng,

    /
  • ;q=0.8", the content of the Accept item in the request header
  • ##$_SERVER["HTTP_ACCEPT_ENCODING"] => "gzip, deflate", the content of the Accept-Encoding item in the request header
  • #$ _SERVER["HTTP_ACCEPT_LANGUAGE"] => "zh-CN,zh;q=0.8", the content of the Accept-Language item in the request header

  • $_SERVER["PHP_SELF"] = > "/index.php", the file name of the currently executing script

  • $_SERVER["REQUEST_TIME_FLOAT"] => 1510112348.8084, the timestamp of the start of the request, microsecond level accuracy

  • $_SERVER["REQUEST_TIME"] => 1510112348, the timestamp of the start of the request

  • Instance

  • The above is the main content of

    $_SERVER

    in the test. There will be some changes according to different environment configurations. In actual work, $_SERVER has many functions. Here are only two simple examples, obtaining the current request URL and simple anti-leeching.
<?php
// $_SERVER['HTTPS']当前是否为HTTPS协议
if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
    $url = 'https://';
} else {
    $url = 'http://';
}

if ($_SERVER['SERVER_PORT'] == 80) {
    $url .= $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
} else {
    $url .= $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['SERVER_PORT'];
}
rrree

The above is the detailed content of Detailed introduction to PHP's $_SERVER (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete
Previous article:What is PerlNext article:What is Perl