PHP to create a session of the method examples of detailed, session _php tutorial

Source: Internet
Author: User

PHP to create a session of the method examples of detailed, session detailed


The example in this article describes how PHP creates a session. Share to everyone for your reference. The specific analysis is as follows:

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
Copy the Code code as follows: <?php
echo "------How to save session data---------
";
1. Initialize session
Session_Start ();
2, save data, you can save the data types include: String, Integer, double type, array, object, etc.
$_session[' name ']= "Baidu";//Save string
$_session[' age ']=80;//Preserve integer type

Save Array
$arr = Array ("name" = "Xiao Chen", "Age" =>25, "job" and "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).

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/948407.html www.bkjia.com true http://www.bkjia.com/PHPjc/948407.html techarticle PHP to create a session of the method of the example, the session of the detailed example of this article describes the method of PHP creation session. Share to everyone for your reference. The specific analysis is as follows: Save session only need two ...

  • 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.