How to create a session in php _ PHP

Source: Internet
Author: User
This article mainly introduces how to create a session in php and analyzes in detail the related skills and precautions for creating and using a session in the form of an instance, for more information about how to create a session in php, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

To save a session, you only need to start the session and save the session data in two steps. By default, sessions are stored in the c: \ windows \ temp folder on the server side (the saved path can be in php. modify in INI file: enable session. save_path: Enter the saved path ).

Session creation code

The code is as follows:

<? Php
Echo "------ how to save session data ---------
";
// 1. initialize the session
Session_start ();
// 2. save data. data types that can be saved include string, integer, double, array, and object.
$ _ SESSION ['name'] = "Baidu"; // Save the string
$ _ SESSION ['age'] = 80; // Save the integer

// Save the array
$ Arr = array ("name" => "Chen", "age" => 25, "job" => "programmer ");
$ _ SESSION ['person '] = $ arr;

// Save the object
Class Dog {
Public $ name;
Public $ age;
Public $ color;
Function _ construct ($ name, $ age, $ color ){
$ This-> name = $ name;
$ This-> age = $ age;
$ This-> color = $ color;
}
}
$ Dog = new Dog ("puppy", 2, "yellow ");
$ _ SESSION ['dog'] = $ dog;

Echo "saved successfully ";
?>


Under the C: \ windows \ temp File, find a file to save the session, and open it, for example:

Note:

(1) each session is separated by a semicolon.
(2) take the first session as an example: name represents the key value, s represents the string (correspondingly: I represents an integer, a represents an array, o represents an object, etc), 4 indicates the length, and "Baidu" indicates the key value.

Details (important ):

(1) each session (that is, when the browser is opened to access a website, the session ends when the browser is closed) corresponds to a session file;
(2) the session file is created when session_start () is executed. However, the file is empty. If session data exists, the file is written;
(3) by default, session data is retained for 1440 seconds, which indicates that the session file has not been used during this period of time (if any, the modification time of the file will be automatically updated-right-click to view the file properties ). You can modify the default value in the php. ini file: session. gc_maxlifetime = 1440;
(4) top priority: when the server returns a client browser request, it will return the session information (for example, PHPSESSID = 0pk6fmamnk1btcgbcf444dnd76) to the browser as a cookie (similarly, you can use httpwatch to capture packets ). When the browser accesses other pages of the website, the cookie information is sent to the server according to http coordination. Based on this information, the server finds the corresponding session file (the file name is sess_0pk6fmamnk1btcgbcf444dnd76 ).

I hope this article will help you with php programming.

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.