1. Overview
PHP environment variables mainly include:
$ Globals []: stores all global variables in the current script. The key is the variable name, and the value is the variable value.
$ _ Server []: array of current web server Variables
$ _ Get []: stores the data in the form submitted using the get method.
$ _ Post []: stores data in the form submitted using the POST method
$ _ Cookie []: Get or set the array of variables stored in cookies in the user's browser
$ _ FILES []: stores the data submitted to the current script by the uploaded file.
$ _ Env []: stores the current web environment variable
$ _ Request []: stores all request arrays in the submission form, including $ _ Get, $ _ post, $ _ cookie, and $ _ Session.
$ _ Session []: an array of session variables for storing the current script
2. $ globals []
Globals is an array composed of defined global variables. The variable name is the index of the array. For example:
$ENTER = "<br />";$var1 = 5; $var2 = 10; print $GLOBALS['var1'] . $ENTER ;print $GLOBALS['var2'] . $ENTER ;
Globals and global are similar, but there are some differences, refer to: http://www.neatstudio.com/show-644-1.shtml
3. $ _ server []
_ Server is an array containing information such as header, path, and script locations. For example:
$ENTER = "<br />";print "SERVER_ADDR:" . $_SERVER['SERVER_ADDR'] . $ENTER;
Reference: http://www.php.net/manual/zh/reserved.variables.server.php
4. $ _ Get and _ post
$ _ Get and _ post are used to receive request data and implement Input
$ _ Get content is transmitted to the array of variables of the current script through URL parameters.
$ _ Post is the variable name and value sent by the http post method.
Get is the most primitive Request Method in HTTP. a GET request is sent when you click a hyperlink in a webpage or enter a URL in the address bar. In a GET request, the data is sent after the URL suffix, like: http: // 192.168.21.20./ test1.php? Name_get = zxm & age_get = 23
The main purpose of the POST method is to pass data. It uploads the data after all request headers, so that no matter how much data is uploaded.
Eg:
HTML:
<form action="test1.php" method="get">Name: <input type="text" name="name_get" />Age: <input type="text" name="age_get" /><input type="submit" /></form><form action="test1.php" method="post">Name: <input type="text" name="name_post" />Age: <input type="text" name="age_post" /><input type="submit" /></form>
PHP:
//_GETecho "name:" . $_GET["name_get"] . $ENTER ;echo "age:" . $_GET["age_get"] . $ENTER;//_POSTecho "name:" . $_POST["name_post"] . $ENTER ;echo "age:" . $_POST["age_post"] . $ENTER;?>
5. $ _ file
With $ _ files, we can upload files from the client's computer to a remote server.
Upload File Form:
<form action="test1.php" method="post"enctype="multipart/form-data"><label for="file">Filename:</label><input type="file" name="file" id="file" /><br /><input type="submit" name="submit" value="Submit" /></form>
PHP:
//_FILEecho "Error: " . $_FILES["file"]["error"] . "<br />";echo "Upload: " . $_FILES["file"]["name"] . "<br />";echo "Type: " . $_FILES["file"]["type"] . "<br />";echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";echo "Stored in: " . $_FILES["file"]["tmp_name"];
6. _ cookie []
Cookie is a mechanism for storing data in a remote browser to track and identify users. It is often used to identify users. When the same computer requests a page through a browser, it also sends a cookie. Through $ _ cookie [], we can retrieve the cookie.
Set COOKIE:
<?php setcookie("user", "zxm"); ?>
Read COOKIE:
//cookieif (isset($_COOKIE['user'])) echo "Welcome " . $_COOKIE['user'] . "!<br />";else echo "Welcome guest!<br />";
7. _ session []
_ Session [] stores information about user sessions.
Eg:
<?phpsession_start();$_SESSION['id'] = "100";?>
PHP:
//sessionif(isset($_SESSION['id'])) echo "session id: " . $_SESSION['id'] . $ENTER;else echo "without session" . $ENTER;?>
7. _ env []
In PHP, $ _ env stores some system environment variables, which have different values due to different environments.
8. _ request []
$ _ Request contains all the content of $ _ Get, $ _ post, and $ _ cookie.
$ _ Get, $ _ post, and $ _ Cookie all have a copy in $ _ request. Changing the value of $ _ request does not affect $ _ get, and vice versa.
Priority: $ _ Get <$ _ post <$ _ cookie
Eg:
//requestecho "request: " . $_REQUEST['name_get'] . "!<br />";?>
8. Test the PHP program:
<?phpsetcookie("user", "zxm");session_start();$_SESSION['id'] = "100";?>