Detailed explanation of the usage of 9 predefined superglobal variables in PHP

怪我咯
Release: 2023-03-13 20:02:01
Original
1907 people have browsed it

Many predefined variables in PHP are "superglobal", which means they are available throughout the entire scope of a script. They can be accessed within a function or method without executing global $variable;.

These superglobal variables are:

  • $GLOBALS

  • ##$_SERVER

  • $_REQUEST

  • $_POST

  • $_GET

  • $_FILES

  • $_ENV

  • $_COOKIE

  • $_SESSION

This article mainly introduces PHP9 super global variables


1, $_SERVER

$_SERVER super global variable contains the variables created by the web server Information, which provides information about server and client configurations and the current request environment. Depending on the server, the variable value and number of variables in $_SERVER will be different, but generally you can find the variables defined in the CGI1.1 specification. These include:

$_SERVER['HTTP_REFERER']; The URL of the page that leads the user to the current location;


$_SERVER['REMOTE_ADDR']; Customer IP address;


$_SERVER['REQUEST_URI']; The path part of the URL. If the URL is [url]http://www.example.com/blog/apache/index.html[/url], then the URI is /blog/apache/index.html.


$_SERVER['HTTP_USER_AGENT']; The customer's user agent generally provides information about the operating system and browser.

2, $_GET

$_GET super global variable contains information about the parameters passed using the GET method. If the request URL is [url]http://www.example.com /index.html?cat=apache&id=157[/url], you can use the $_GET super global variable to access the following variables:

$ _GET['cat'] = "apache";

$_GET['id'] = "157";

By default, to access variables passed through the GET method, $_GET Super global variables are the only way.

3. $_POST

$_POST super global variable contains information about the parameters passed by the POST method.

The code is as follows:

<form caction="subscribe.php" method="post">
  <p> Email address : <br>
    <input type="text" name="email" size="20" maxlength="so" value="">
  </p>
  <p> Password : <br>
    <input type="password" name="pswd" size="20" maxlength="15" value="">
  </p>
  <p>
    <input type="submit" name="subscribe" value="subscribe!">
  </p>
</form>
Copy after login

Through the script subscribe.php, you can use the following

POST variable :

$_POST[&#39;email&#39;] = " jason@example.com ";
$_POST[&#39;pswd&#39;] = "rainyday";
$_POST[&#39;subscribe&#39;] = "subscribe!";
Copy after login

Like $_GET, by default, the $_POST super global variable is the only way to access POST variables.

4, $_COOKIE

$_COOKIE super global variable stores the information passed to the script through the HTTP cookie. These cookies are generally set by a previously executed PHP script through the

PHP function setcookie(). For example, suppose you use setcookie() to store a cookie named example.com with a value of ab2213. This value can be obtained later by calling $_COOKIE['example.com'].

5, $_FILES

$_FILES super global variable contains information about the data uploaded to the server through the POST method. This super global variable is different from other variables. It is a

two-dimensional array, containing 5 elements. The first subscript indicates the File upload element name of the form; the second subscript is one of five predefined subscripts, which describe a certain attribute of the uploaded file:

$_FILES[&#39;upload-name&#39;][&#39;name&#39;]; 从客户端向服务器上传文件的文件名;
$_FILES[&#39;upload-name&#39;][&#39;type&#39;]; 上传文件的MIME类型,这个变量是否赋值取决于浏览器的功能。
$_FILES[&#39;upload-name&#39;][&#39;size&#39;]; 上传文件的大小(以字节为单位);
$_FILES[&#39;upload-name&#39;][&#39;tmp_name&#39;]; 上传之后,将此文件移到最终位置之前赋予的临时名。
$_FILES[&#39;upload-name&#39;][&#39;error&#39;]; 上传状态码。尽管这个变量的名为 error ,但实际上在成功的情况下也会填写这个变量。它有五个可能的值:
UPLOAD_ERR_OK 文件成功上传
UPLOAD_ERR_INI_SIZE 文件大小超出了 upload_max_filesize 指令所指定的最大值。
UPLOAD_ERR_FORM_SIZE 文件大小超出了MAX_FILE_SIZE 隐藏表单域参数(可选)指定的最大值。
UPLOAD_ERR_PARTIAL 文件只上传了一部分
UPLOAD_ERR_NO_FILE 上传表单中没有指定文件
Copy after login

6, $_ENV

$_ENV super global variable provides information about the server environment where PHP parses. Variables in this array include:

$_ENV['HOSTNAME'] Server's host name

$_ENV['SHELL'] System shell

7, The $_REQUEST

$_REQUEST super global variable is an all-rounder that records variables passed to the script through various methods, especially GET, POST and COOKIE. The order of these variables does not depend on the order in which they appear in the send script, but on the order specified by the variables_order configuration directive. It is recommended to use this super variable sparingly because it is not safe enough.

8, $_SESSION

$_SESSION super global variable contains information related to all sessions. Registering session information gives you the convenience of being able to reference it throughout your site without having to explicitly pass the data via GET or POST.

9, $GLOBALS

$GLOBALS The super global variable array can be considered a superset of super global variables, including all variables in the global scope. Execute the following code to view all variables in $GLOBALS.

print &#39;<pre class="brush:php;toolbar:false">&#39;;  
 
print_r ($GLOBALS);  
 
print &#39;
';
Copy after login

The above is the detailed content of Detailed explanation of the usage of 9 predefined superglobal variables in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template