菜单

How does PHP create session? PHP session method to create examples of detailed

2017年11月22日 - PHPer

In this paper, an example is given to illustrate the method of creating session by php. Share with you for your reference. The detailed analysis is as follows:

Saving session requires only two steps, opening session and saving session data. By default, session is saved in the server c:\windows\temp folder (the saved path can be modified in the php.ini file: open session.save_path, fill in the path to save).

Session create code

Code is as follows

< phpecho? “- how to save the session data ———< br /> //1, sessionsession_start; (//2); initialization, data storage, can save the data types include: string, integer, double, array, object $_SESSION[‘name’]=” Baidu “; / / save string $_SESSION[‘age’]=80; / / $arr / / save save integer array (array =” name “=>” Chen “,” age =&gt “; 25,” job “=>” programmer “); $_SESSION[‘person’] = $arr;

Class Dog{public / / save the object $name; public $age; public $color; function __construct ($name, $age, $color) {$this-> name=$name; age=$age; $this-> $this-> color=$color;}}$dog = new Dog (“puppy”, 2, “yellow”); $_SESSION[‘dog’] = $dog;

Echo “save success”; >

 

In the C:\windows\temp file, find a file to save session, open the following picture:

Explain:

(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 (corresponding: I denotes integer, a denotes array, O denotes object, etc.), 4 denotes length, and Baidu denotes key value.

Detail knowledge (very important):

(1) each session (that is, open the browser to access a web site, the browser is closed when the session is over) corresponds to a session file; (2) session (session_start) in the implementation file created, but this time, the file is empty, if the session data is written to the file; (3) the default session data retention time is 1440 seconds, this time for a time, it is in this period of time, do not use the session file (if used, the file modification time will automatically update – right click to see is to view the file). You can modify the default value in the php.ini file: session.gc_maxlifetime = 1440; (4) priority among priorities: the server returned to the client browser request, the session information (such as: PHPSESSID=0pk6fmamnk1btcgbcf444dnd76, cookie) in the way back to the browser (again, you can use httpwatch to capture view). When the browser accesses other pages of the site, the cookie information is sent to the server according to the HTTP coordination. The server then finds the corresponding session file according to the information (the corresponding file name is: sess_0pk6fmamnk1btcgbcf444dnd76).

发表评论

电子邮件地址不会被公开。