Example to explain the use of Session in PHP website development

Source: Internet
Author: User
Tags function prototype functions variables php session php website

PHP session refers to the user from entering the site to the closure of the site during the period of a mechanism, it provides all web pages are common use of the public variable storage mechanism. So what is the use of the session? When shopping on the Internet, everyone has used a shopping cart, you can always add the goods you buy to the shopping cart, and finally go to the cashier checkout. Throughout the process, the shopping cart has been playing the role of temporary storage of selected goods, using it to track the user's activities on the site, this is the role of the session.
The session's invention fills the limits of the HTTP protocol, which is considered a stateless protocol, and the server loses contact with the browser when it completes its response on the service side. The invention of the session allows a user to save his information when switching between multiple pages.
In the PHP3 version does not provide a direct session function, we can only use other methods to achieve, such as using Phplib. If PHP4 compared with PHP3, its greatest improvement is to provide a session.
Session Basics
To use the session, you need to PHP4.1 the above version, and you need to set the Register_globle=off in PHP.ini to Register_globle=on. In addition, Session.cookie_path =/This line is not easy to change.

To station edit Note: This article is obviously a bit old, but, the following is also very good, in the Register_globle=off, also can use, and, the following session_register also can not use, directly with $_session[xxx]= " xxxxxxx "; $HTTP _session_vars this variable is not necessary.
The session in PHP is by default a cookie used by the client. When the client's cookie is disabled, it is automatically passed through the query_string.

There are 11 functions in the PHP processing session, and we'll talk about several functions we'll use.
1, Session_Start
function function: Start a session or return a session that already exists.
Function prototype: Boolean session_start (void);
Return Value: Boolean value
Function Description: This function has no parameters and the return value is true. It is best to place this function first, and there is no output before it, otherwise it will alarm, such as: Warning:cannot send session cache Limiter-headers already sent (output started At/us R/local/apache/htdocs/cga/member/1.php:2) in/usr/local/apache/htdocs/cga/member/1.php on line 3
2, Session_register
function function: Register a new variable as session variable
Function prototype: Boolean Session_register (string name);
Return value: Boolean value.
Function Description: This function is to add a variable in the global variable to the current session, the parameter name is the name of the variable you want to join, and success returns the logical value TRUE. You can use the form of $_session[name] or $http_session_vars[name] to take a value or assign a value.
3, session_is_registered
function function: Checks whether a variable is registered as a session variable.
Function prototype: Boobean session_is_registered (string name);
Return Value: Boolean value
Function Description: This function checks whether the specified variable is registered in the current session, and the parameter name is the variable name to check. Success returns the logical value TRUE.
4, Session_unregister
function function: Deletes a registered variable.
Function prototype: Boolean Session_session_unregister (string name);
Return Value: Boolean value
Function Description: This function deletes the variables in the global variable in the current session. The parameter name is the name of the variable you want to delete, and success returns TRUE.
5, Session_destroy
function function: Ends the current session and empties all resources in the session.
Function Prototype: Boolean session destroy (void);
Return value: Boolean value.
Function Description: This function ends the current session, this function has no arguments, and the return value is True

The functions described above will be used, but there are also some functions about the session:
6, Session_encode
function function: sesssion information encoding
Function prototype: string session_encode (void);
return value: String
Function Description: The returned string contains the names and values of the variables in the global variable in the form of: A|s:12: "It is a test"; C|s:4: "Lala"; A is the variable name s:12 represents the value of variable a "it is a test length is a semicolon between 12 variables"; Separated.
7, Session_decode
function function: sesssion information decoding
Function prototype: Boolean Session_decode (String data)
Return Value: Boolean value
Function Description: This function can decode session information, success returns the logical value TRUE



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.