PHP pre-defined variable (2) _ PHP Tutorial

Source: Internet
Author: User
Tags http file upload php session
PHP pre-defined variable (2 ). PHP pre-defined variables (II) IV. Session variables ($ _ SESSION): the data generated by the php session function is in the form of super global variables, stored in the $ _ SESSION variable. 1. Session introduction PHP predefined variables (2)
4. Session variable ($ _ SESSION): the data generated by the php session function is stored in the $ _ SESSION variable in the form of a super global variable.
1. Session introduction
SESSION is also called a SESSION period. it is a small file stored on the server and is used to store user information. Sessions are differentiated by SESSION ids. each SESSION has a unique identifier. SESSION can also be used to do the same work as COOKIE. The session automatically performs encode and decode on the value to be set. Therefore, the session can store any data type, including arrays and objects.
2. use the session_save_path () function to set the session file storage directory, for example:
Session_save_path ("D:/phpnow/Apache2/temp ");
// Use the session_save_path () function to obtain the directory saved by the current session
Echo session_save_path ()."
";
Note: After PHP is installed, the SESSION storage path is not set. If SESSION is directly used in the script, an error occurs because the path does not exist. You can use the session_save_path () function to modify the save path of the SESSION file, but only the current script. If a valid SESSION file storage directory has been set in the PHP configuration file, you can skip this function in the script. at this time, all PHP programs share a directory to store SESSION files.
3. Register SESSION variables

Because $ _ SESSION is an array, you can add new variables to the SESSION by adding cells to the array. For example:

Session_start (); // enable SESSION. you must enable SESSION before using it.

// Use an array to add a unit and add a SESSION variable

$ _ SESSION ["username"] = "phpstuer"; // store strings

$ SessionArr = array ("1", "2", "3 ");
$ _ SESSION ["arr"] = $ sessionArr; // storage array

NOTE: You can also use the session_register function to register the SESSION variable. However, this function has been deprecated since PHP5.3, so we will not give an example here.

4. access SESSION variables
Because the SESSION variables are stored in the $ _ SESSION global variables in the form of units, you can access the SESSION variables by using the method of accessing the array. SESSION variables must be initialized before they can be used. this is different from COOKIE. The SESSION initialization function is session_start (). For example:
// Use the session_start () function to enable the SESSION
Session_start ();
// Traverse the $ _ SESSION array
Foreach ($ _ SESSION as $ key => $ value ){
Echo "$ key => $ value
";
}
Echo "separately accessing SESSION variables :";
Echo $ _ SESSION ["username"]."
";
Echo "separately accessing SESSION variables :";
Echo $ _ SESSION ["arr"] [2];

Note: When using the session_start () function, add code at the top of the page to prevent error messages. .

5. delete SESSION variables
After using the SESSION variable, you need to delete the registered SESSION to reduce the use of server resources.
Session_start ();
// Before deletion:
Echo"

";
print_r($_SESSION);
echo "
";
// Use the unset () function to delete a $ _ SESSION variable.
Unset ($ _ SESSION ["username"]);
// Use the session_unset () function to delete the $ _ SESSION variable on the current page.
// Session_unset ();
// Use the session_destroy () function to delete the $ _ SESSION variable on the current page and delete the SESSION file.
// Session_destroy ();

// After deletion:
Echo"

";
print_r($_SESSION);
echo "
";

5. the Request variable ($ _ REQUEST) is a super global variable. $ _ REQUEST stores data including $ _ POST, $ _ GET, and $ _ COOKIE variables. By accessing $ _ REQUEST, you can also implement the functions of $ _ POST, $ _ GET, and $ _ COOKIE variables.
Although $ _ REQUEST is easy to use, the data provided by it is dangerous. For example, when a program uses $ _ REQUEST to read the $ _ COOKIE value, the visitor can simulate the $ _ COOKIE value by providing a $ _ GET variable to obtain certain access permissions.
Instance used: omitted.

6. the SERVER variable ($ _ SERVER) is an array created by the network SERVER. its content includes header information, path, and script location. Different network servers provide different information. the following example uses the information provided by the Apache server as the standard.
Echo"

";
print_r($_SERVER);
echo "
";
// The description is as follows:
Echo "show the relative path and file name of the script file: \" ". $ _ SERVER [" PHP_SELF "]." \"
";
Echo "display CGI script specifications used by the SERVER: \" ". $ _ SERVER [" GATEWAY_INTERFACE "]." \"
";
Echo "displays the IP address of the SERVER where the script is currently running: \" ". $ _ SERVER [" SERVER_ADDR "]." \"
";
Echo "display the name of the currently running script SERVER: \" ". $ _ SERVER [" SERVER_NAME "]." \"
";
Echo "display the current running script server id: \" ". $ _ SERVER [" SERVER_SOFTWARE "]." \"
";
Echo "display the name and version of the communication protocol on the request page: \" ". $ _ SERVER [" SERVER_PROTOCOL "]." \"
";
Echo "request method for access page display: \" ". $ _ SERVER [" REQUEST_METHOD "]." \"
";
Echo "show script start Running time: \" ". $ _ SERVER [" REQUEST_TIME "]." \"
";
Echo "string after the URL question mark is displayed: \" ". $ _ SERVER [" QUERY_STRING "]." \"
";
Echo "shows the root directory of the file currently running the script: \" ". $ _ SERVER [" DOCUMENT_ROOT "]."
";
Echo "display the current Accept request header information: \" ". $ _ SERVER [" HTTP_ACCEPT "]." \"
";
Echo "display the character information of the current request: \" ". $ _ SERVER [" HTTP_ACCEPT_CHARSET "]." \"
";
Echo "displays the Accept-Encoding header information of the current request: \" ". $ _ SERVER [" HTTP_ACCEPT_ENCODING "]." \"
";
Echo "the Accept-Language header of the current request is displayed: \" ". $ _ SERVER [" HTTP_ACCEPT_LANGUAGE "]." \"
";
Echo "display the Connection header information of the current request: \" ". $ _ SERVER [" HTTP_CONNECTION "]." \"
";
Echo "displays the Host header information of the current request: \" ". $ _ SERVER [" HTTP_HOST "]." \"
";
Echo "shows the URL of the previous page: \" ". $ _ SERVER [" HTTP_REFERER "]." \"
";
Echo "display the current request's User-Agent header information: \" ". $ _ SERVER [" HTTP_USER_AGENT "]." \"
";
Echo "show whether the script can be accessed through HTTPS: \" ". $ _ SERVER [" HTTPS "]." \"
";
Echo "display the IP address of the user browsing the current page: \" ". $ _ SERVER [" REMOTE_ADDR "]." \"
";
Echo "show the host name of the user browsing the current page: \" ". $ _ SERVER [" REMOTE_HOST "]." \"
";
Echo "display the port used by the user to connect to the SERVER: \" ". $ _ SERVER [" REMOTE_PORT "]." \"
";
Echo "displays the absolute path name of the currently executed script: \" ". $ _ SERVER [" SCRIPT_FILENAME "]." \"
";
Echo "shows SERVER_ADMIN parameter settings in the Apache configuration file: \" ". $ _ SERVER [" SERVER_ADMIN "]." \"
";
Echo "displays the port used by the network SERVER. the default value is \" 80 \ ": \" ". $ _ SERVER [" SERVER_PORT "]." \"
";
Echo "display the SERVER version and virtual host name string: \" ". $ _ SERVER [" SERVER_SIGNATURE "]." \"
";
Echo "display the basic path of the script in the file system: \" ". $ _ SERVER [" PATH_TRANSLATED "]." \"
";
Echo "shows the path of the current script: \" ". $ _ SERVER [" SCRIPT_NAME "]." \"
";
Echo "shows the URI used to access the current page: \" ". $ _ SERVER [" REQUEST_URI "]." \"
";
7. environment variables ($ _ ENV) are predefined arrays that record system paths and other information.
Echo"
";
print_r($_SERVER);
echo "
";
// The array members that access the environment variables separately can be implemented by "$ _ ENV ['Member variable name']", for example:
Echo "server OS:". $ _ ENV ["OS"]."
";
8. HTTP file Upload variable ($ _ FILES): the file upload variable generated by an HTML form records the detailed information of the uploaded file in an array, its array members include the uploaded File name: name; file type: type; temporary File name: tmp_name; error code: error; file size: size. The following describes how to use HTML forms to generate file Upload variables,
To make the form generate file variables, three conditions must be met:
1. Use POST to transmit data to HTML forms.
2. set the "enctype" parameter of the form to "multipart/form-data ".
3. the form contains a file selection box.
After obtaining information about the uploaded file through the $ _ FILES variable, you can use other file functions to upload FILES.
Example of how to upload FILES and $ _ FILES: to be sorted
9. The $ GLOBALS variable records all defined global variables in the form of an array, and the variable name is the index of this array. Even if $ GLOBALS records all global variables, you can naturally access the information in global variables. The following describes how to use $ GLOBALS to access other predefined variables, for example:
Session_start ();
$ _ SESSION ["school"] = "";
Echo $ _ SESSION ["school"]."
"; // Output University
// Use $ GLOBALS to display the current operating system version
Echo $ GLOBALS ["_ ENV"] ["OS"]."
";
// Use $ GLOBALS to access the SESSION value
Echo $ GLOBALS ["_ SESSION"] ["school"]."
"; // Output University

Echo"

";
print_r($GLOBALS);
echo "
";



Round (II) 4. Session variable ($ _ SESSION): data generated by the php session function is stored in the $ _ SESSION variable in the form of ultra-global variables. 1. Session introduction...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.