PHP pre-defined variables (ii) _php tutorial

Source: Internet
Author: User
Tags http file upload

PHP pre-defined variables (ii)


Session variable ($_session): The data generated by the session function of PHP are stored in the $_session variable in the way of super global variables.
1. Session Introduction
The session is also known as the conversation period, which is a small piece of file stored on the server to store information about the user. Session identifiers are used to differentiate between sessions, and each session has a unique identity. You can also use the session to do a cookie-like job. The session automatically encode and decode the values to be set, so the session can support storing arbitrary data types, including arrays and objects.
2, use the Session_save_path () function, set the session file save 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 completion of PHP installation, because the session save path is not set, if you use the session directly in the script, there will be errors due to 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 a valid session file to save the directory in the PHP configuration file, you can not use this function in the script, and all PHP programs share a directory to store the session file.
3. Register Session variable

Since $_session is an array, you can add a new variable to the session by using an array of cells. Such as:

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

Add a session variable by adding a cell to the array

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

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

Note: It is also possible to register session variables with the Session_register function, but this function has been deprecated since PHP5.3, so this is no longer an example.

4. Access Session Variables
Since the session variable is stored in the $_session global variable in the form of a cell, the session variable can be accessed 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 the $_session array
foreach ($_session as $key = = $value) {
echo "$key and $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 the error message from appearing .

5. Delete Session Variables
After the session variable is used, the session that has already been registered needs to be deleted to reduce the resource usage of the server.
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 of the current page
Session_unset ();
Use the Session_destroy () function to delete the $_session variable of 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. The functions of $_post, $_get, $_cookie variables can also be implemented by accessing $_request.
Although the $_request is more convenient to use, the data provided is of some danger. For example, when a program uses $_request to read the $_cookie value, the intentional visitor can simulate the $_cookie value by providing a $_get variable, thus obtaining certain access rights.
Usage Example: slightly.

The 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 standard by the Apache server.
echo "

";
Print_r ($_server);
echo "
";
The explanations are as follows:
echo "Displays the relative path and file name of the script file: \" ". $_server[" Php_self "]." \"
";
echo "Displays the CGI script specification 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 "Displays the currently running Script server name: \" ". $_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 requested page: \" ". $_server[" Server_protocol "]." \"
";
echo "Displays the request method for accessing the page: \" ". $_server[" Request_method "]." \"
";
echo "Show script start run time: \" ". $_server[" Request_time "]." \"
";
echo "Displays 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 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 if 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 "shows the port used by the user when connecting to the server: \" ". $_server[" Remote_port "]." \"
";
echo "Displays the absolute path name of the currently executing script: \" ". $_server[" Script_filename "]." \"
";
echo "Displays the Server_admin parameter settings in the Apache configuration file: \" ". $_server[" Server_admin "]." \"
";
echo "shows the port used by the network server, which defaults to \" 80\ ": \" ". $_server[" Server_port "]." \"
";
echo "Displays the server version and the virtual hostname of the string: \" ". $_server[" Server_signature "]." \"
";
echo "shows the basic path of the script in the file system: \" ". $_server[" path_translated "]." \"
";
echo "Displays the path of the current script: \" ". $_server[" Script_name "]." \"
";
echo "shows access to the current page uri:\" ". $_server[" Request_uri "]." \"
";
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:". $_env["OS"]. "
";
HTTP file upload variable ($_files): The file upload variable generated by the HTML form, the details of the uploaded file are recorded as an array, and its array members include the uploaded filename: name; file type: type; temporary file name: tmp_name; Error message code : error; File size: size. Here's how to generate a file upload variable using an HTML form,
To cause a form to produce a file variable, three conditions are met:
1, the HTML form to use the Post method to pass the data.
2. The "enctype" parameter of the form should be set to "Multipart/form-data".
3. The form contains a file selection box.
After obtaining the information about the uploaded file through the $_files variable, the file upload can be implemented with other file functions.
Examples of uploading files and $_files: to be sorted
Nine, $GLOBALS variables in the form of an array, all defined global variables are recorded, and the variable name is the index of this array. Now that the $globals records all the global variables, it is also possible to access the information in the global variables as a matter of course. Here are ways 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 "
";



http://www.bkjia.com/PHPjc/905598.html www.bkjia.com true http://www.bkjia.com/PHPjc/905598.html techarticle PHP predefined variables (ii) Four, session variables ($_session): The data generated by the session function of PHP are stored in the $_session variable in the form of super-global variables. 1. Introduction of the session ...

  • Related Article

    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.