PHP Create Session Method Walkthrough

Source: Internet
Author: User
This time to bring you to the PHP creation session Method Step-up, PHP to create a session method of attention to what, the following is the actual case, together to see.

It only takes two steps to save the session, to open the session and save the session data. By default, the session is saved in the server-side C:\Windows\Temp folder (the saved path can be modified in the php.ini file: Open Session.save_path, fill in the saved path).

Session Creation Code

<?php
Echo "------How to save session data---------<br/>";
1. Initialize session
Session_Start ();
2, save the data, you can save the data types include: String, Integer, double, array, object, such as
$_session[' name ']= "Baidu";//Save String
$_session[' age ']=80;//save integer

//Save array
$arr = Array ("name" + "Xiao Chen", "Age" =>25, "job" = "programmer");
$_session[' person '] = $arr;

//Save 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 "Save Success";
?

Under the C:\windows\temp file, locate a file that holds the session and open it as:

Description

(1) Each session is separated by a semicolon;
(2) Take the first session as an example: name denotes a key value, S represents a string (corresponding: I for integer type, a for array, O for object, etc.), 4 for length, and "Baidu" for key value.

Detailed knowledge (very important):

(1) Each session (that is, open a browser to visit a website, when the browser is closed, the session will end) corresponding to a session file;
(2) The session file is created when the session_start () is executed, but at this time, the file is empty and if there is session data, the file is written;
(3) session data default retention time is 1440 seconds, this time is daze time, that is, during this time, did not use the session file (if used, then the modified time of the file will be automatically updated-right-click to see the file properties can be seen). You can modify this default value in the php.ini file: session.gc_maxlifetime = 1440;
(4) Top priority: When the server returns to the client browser request, the session information (such as: PHPSESSID=0PK6FMAMNK1BTCGBCF444DND76) is returned to the browser as a cookie (again, You can use HttpWatch to grab the package for viewing). When the browser accesses other pages of the site, the cookie information is sent to the server based on HTTP coordination. The server then finds the corresponding session file according to this information (the corresponding file name is: sess_0pk6fmamnk1btcgbcf444dnd76).

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

Laravel 5 How to stop an XSS cross-site attack

PHP Array Access interface arrayaccess using the detailed

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.