PHP predefined variables (ii)

Source: Internet
Author: User
Tags file size file system file upload functions header variables string version
Session variable ($_session): The data produced by the PHP session function is stored in the $_session variable in the form of a super global variable.
1. Introduction to Session
Sessions are also called session periods, which are small pieces of files stored on the server to store information about users. Session identities are used to differentiate between sessions, each with a unique identifier. You can also do a cookie-like work using session. The session automatically encode and decode the values to be set, so sessions can support storing arbitrary data types, including arrays and objects.
2, using the Session_save_path () function, set the session file to save the directory, such as:
Session_save_path ("D:/phpnow/apache2/temp");
Use the Session_save_path () function to get the directory saved by the current session
Echo Session_save_path ().
";
Note: After the installation of PHP, because the save path of the session is not set, if you use the session directly in the script, there will be errors caused by the path does not exist. Using the Session_save_path () function, you can modify the save path of the session file, but only for the current script. If you have set up a valid session file to save the directory in the PHP configuration file, you can not use this function in the script, at which point all PHP programs share a directory to store the session file.
3, register the session variable

Because the $_session is an array, you can add new variables to the session by using the array's method of adding cells. Such as:

Session_Start ()//open session, before use, must first open

Add the session variable by using the method of adding cells to the array

$_session["username"] = "phpstuer";//Storage string

$SESSIONARR = Array ("1", "2", "3");
$_session["arr"] = $SESSIONARR; Storing arrays

Note: You can also register the session variable with the Session_register function, but this function has been deprecated since PHP5.3, so here is no longer an example.

4, access to session variables
Because the session variable is stored in the $_session global variable in the form of a cell, the session variable can be accessed by using the method of accessing the array. After the session variable is registered, it needs to be initialized before it can be used, which is different from the cookie. The function that initializes the session is Session_Start (). Such as:
Use the Session_Start () function to open the session
Session_Start ();
Traversing $_session arrays
foreach ($_session as $key => $value) {
echo "$key => $value
";
}
echo "Access session variable individually:";
echo $_session["username"]. "
";
echo "Access session variable individually:";
echo $_session["arr"][2];

Note: When using the Session_Start () function, add code to the top of the page to prevent error messages from appearing .

5. Delete Session variable
After you use the session variable, you need to delete the registered session to reduce resource usage for the server.
Session_Start ();
Before deleting:
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 for the current page
Session_unset ();
Use the Session_destroy () function to delete the $_session variable for the current page and delete the session file
Session_destroy ();

After deletion:
echo "

";
Print_r ($_session);
echo "
";

The REQUEST variable ($_request) is a super global variable, and $_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 the $_request is convenient to use, the data provided by it is of some danger. For example, when a program uses $_request to read the $_cookie value, the attentive visitor can gain certain access by providing a $_get variable to simulate the $_cookie value.
Use instance: slightly.

A server variable ($_server) is an array created by a network server that includes header information, paths, script locations, and so on. The information provided by different Web servers varies, and the following examples are provided as criteria by the Apache server.
echo "

";
Print_r ($_server);
echo "
";
Explained as follows:
echo Displays the relative path and filename of the script file: \ "" "$_server[" php_self "." \"
";
echo "Displays the CGI scripting specification used by the server: \" ". $_server[" Gateway_interface "]." \"
";
echo Displays the IP address of the server on which the script is currently running: \ "". $_server["Server_addr"]. " \"
";
echo Displays the name of the currently running script server: \ "". $_server["SERVER_NAME"]. " \"
";
echo "Displays the currently running Script server identity: \" ". $_server[" Server_software "]." \"
";
echo Displays the name and version of the communication Protocol for the request page: \ "". $_server["Server_protocol"]. " \"
";
echo "shows the request method for accessing the page: \" ". $_server[" Request_method "]." \"
";
echo "Displays the script start run time: \" ". $_server[" Request_time "]." \"
";
Echo shows the string after the URL question mark: \ "". $_server["Query_string"]. " \"
";
echo "Displays the document root of the currently running script: \" ". $_server[" Document_root "]."
";
echo "Displays the header information for the current ACCEPT request: \" ". $_server[" Http_accept "]." \"
";
ECHO Displays the character information for the current request: \ "". $_server["Http_accept_charset"]. " \"
";
echo Displays the Accept-encoding header information for the current request: \ "". $_server["Http_accept_encoding"]. " \"
";
echo Displays the Accept-language header information for the current request: \ "". $_server["Http_accept_language"]. " \"
";
echo Displays the CONNECTION header information for the current request: \ "". $_server["Http_connection"]. " \"
";
echo Displays the HOST header information for the current request: \ "". $_server["Http_host"]. " \"
";
echo Displays the URL address of the previous page of the current page: \ "". $_server["Http_referer"]. " \"
";
echo "Displays the header information for the user-agent of the current request: \" ". $_server[" Http_user_agent "]." \"
";
Echo shows whether the script can be accessed through the HTTPS protocol: \ "". $_server["https"]. " \"
";
echo "Displays the IP address of the user browsing the current page: \" ". $_server[" REMOTE_ADDR "]." \"
";
ECHO Displays the host name of the user browsing the current page: \ "". $_server["Remote_host"]. " \"
";
echo Displays the port used by the user to connect to the server: \ "". $_server["Remote_port"]. " \"
";
echo Displays the absolute pathname of the currently executing script: \ "". $_server["Script_filename"]. " \"
";
echo Displays the Server_admin parameter settings in the Apache configuration file: \ "". $_server["Server_admin"]. " \"
";
echo Displays the port used by the network server, by default \ "80\": \ "". $_server["Server_port"]. " \"
";
echo "Displays the server version and the virtual host name string: \" ". $_server[" Server_signature "]." \"
";
echo "shows 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 "Displays the Uri:\" "$_server[" "Request_uri" to the current page. " \"
";
The environment variable ($_ENV) is a predefined array that records information such as the system path.
echo "
";
Print_r ($_server);
echo "
";
An array member that accesses an environment variable individually can be implemented by means of the "$_env[' member variable name '", such as:
echo "Server operating system is:". $_env["OS". "
";
HTTP file upload variable ($_files): HTML form generated file upload variables, in the form of an array of file upload details, the array members include uploaded file name: name; file type: type; temporary file name: tmp_name; Error message code : error; File size: size. Here's how to use HTML forms to generate file upload variables,
To make a form produce a file variable, you meet three criteria:
1, the HTML form to use post way to pass data.
2, the form's "enctype" parameter to be set to "Multipart/form-data".
3, the form contains a file selection box.
Through the $_files variable to obtain uploaded file related information, you can cooperate with other file functions to achieve file upload.
Upload files and $_files usage examples: to be sorted out
Nine, $GLOBALS variables in the form of an array, all the defined global variables are recorded, and the variable name is the index of the array. That is, $globals records all the global variables, and then, of course, can access the information in the global variable. The following methods are used to access other predefined variables using $globals, such as:
Session_Start ();
$_session["school"] = "university";
Echo $_session["School"]. "
"; Export University
Use $globals to display the current operating system version
echo $GLOBALS ["_env"] ["OS"]. "
";
accessing session values using $globals
echo $GLOBALS ["_session"] ["school"]. "
"; Export University

echo "

";
Print_r ($GLOBALS);
echo "
";





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.