PHPSession usage and Sessions

Source: Internet
Author: User
PHPSession usage and Sessions. The usage of PHPSession is the same as that of Sessions. in php Tutorial 5, the registry configuration options of all phpsessions are applicable to PHP Session usage and Sessions input into the instance during programming.


The cause of the session header message being sent is the same as that of the cookie.
In php Tutorial 5, the registry configuration options of all php sessions are configurable during programming. In general, we do not need to modify the configuration. for more information about php session registry configuration options, see Session session processing functions in the manual.
During session data storage, the $ _ SESSION array is serialized for storage. Therefore, due to serialization problems, the values of special characters may be encoded using the base64_encode function, base64_decode is used for decoding when reading data.


The following is a simple script. you should start a PHP session at the beginning of your PHP code.

Session_start (); // start up your PHP session!
?>

This small piece of code registers sessions with server users, allowing you to begin saving user information and assigning a UID (unique recognition of user sessions ).

Store session variables
When you want to store SESSION user data, use the $ _ SESSION associated array. You two store and retrieve session data. In the previous PHP version, there were other methods to perform this storage operation, but it has been updated. this is the correct method.

Session_start ();
$ _ SESSION ['View'] = 1; // store session data
Echo "Pageviews =". $ _ SESSION ['View']; // retrieve data
?>
View a simple shopping cart instance

Session_start ();
If (isset ($ _ SESSION ['View'])
$ _ SESSION ['View'] = $ _ SESSION ['View'] + 1;
Else
$ _ SESSION ['View'] = 1;

Echo "views =". $ _ SESSION ['View'];
?>

Session_start ();
If (isset ($ _ SESSION ['cart'])
Unset ($ _ SESSION ['cart']);
?>

Session_start ();
Session_destroy ();
?>


Session instance

/**
* Verify the validity of the session
*
*/
Function sessionVerify (){
If (! Isset ($ _ SESSION ['User _ agent']) {
$ _ SESSION ['User _ agent'] = MD5 ($ _ SERVER ['remote _ ADDR ']
. $ _ SERVER ['http _ USER_AGENT ']);
}
/* If the user's session ID is forged, re-allocate the session ID */
Elseif ($ _ SESSION ['User _ agent']! = MD5 ($ _ SERVER ['remote _ ADDR ']
. $ _ SERVER ['http _ USER_AGENT ']) {
Session_regenerate_id ();
}
}

/**
* Destroy a session
* Perfect implementation in three steps.
*
*/
Function sessionDestroy (){
Session_destroy ();
Setcookie (session_name (), '', time ()-3600 );
$ _ SESSION = array ();
}
?>


Session solves the problem that PHP allows you to store user information on servers for future use (that is, user names, items in the shopping cart. However, the session information is temporary and will be deleted soon after the user leaves the website and uses the session.

It is important to think about it if session temporary storage is applicable to your website. If you need a long-term storage, you need to find another solution, such as a MySQL database tutorial.

Session work creates a unique identifier (UID) for each visitor, and stores variables in this basic ID card. This helps prevent two users from accessing the same webpage when data is acquired and another time of confusion occurs.

Note: If you do not have session programming experience, we do not recommend that you use highly secure sessions on your website. due to security vulnerabilities, you need some advanced technologies to block them.

Start a PHP session
Before you can start storing user information in your PHP session, you must first start the session. When you start a session, it must be sent in HTML or text before the beginning of your code.

Http://www.bkjia.com/PHPjc/632000.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632000.htmlTechArticlePHP Session usage and Sessions into the instance application session header information has been issued the same reason as the cookie. in php Tutorial 5, all php session registry configuration options are programming can be...

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.