Home > Article > Backend Development > Detailed introduction to PHP's $_SERVER (code example)
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 " "
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["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,
/#$ _SERVER["HTTP_ACCEPT_LANGUAGE"] => "zh-CN,zh;q=0.8", the content of the Accept-Language item in the request header
Instance
$_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!