About PHP Sessions: Session and Cookie

Source: Internet
Author: User
Tags session id php session php and mysql sessions sqlite database

What the session processing solves

HTTP (Hypertext Transfer Protocol) defines rules for transferring text, graphics, video, and all other data over the World Wide Web (WWW). HTTP is a stateless protocol that handles every request regardless of the previous and subsequent requests.

Session processing is a workaround for this stateless problem. It is implemented by assigning each site visitor a unique flag attribute called the session ID (SID), and then associating this SID with any number of data.

Session Processing Flow
    1. The browser sends a request to the server
    2. The server establishes a session ID (SID) for the guest, typically the SID is stored in a file on the server side.
    3. Stored on the client ($_cookie[' phpsessid ') or URL parameters)
    4. The browser sends the request to the server again
    5. The server verifies that the SID is still the same session by reading the cookie (or the parameters of the URL)
    6. Leave the site, the session ends
How does the problem SID be written to the cookie?

After the PHP session is started, each guest is assigned a SID, which is also written to the client's cookie, and the default cookie variable is named: PHPSESSID . This is not true at the same time because the next page of code will only be able to fetch cookies after the page is refreshed:

session_start();echo(‘SID:‘.session_id().‘);echo(‘Cookie_sid:‘.$_COOKIE[‘PHPSESSID‘].‘);
The cookie is disabled, will the session expire at the same time?

In the absence of any preparation, the client's disabling of cookies does invalidate the session. In the Book (Third edition of PHP and MySQL programming), the client stores the SID in two ways: a cookie and a rewrite URL to pass. You can change the value in the php.ini file session.use_trans_sid to implement automatic rewrite of the URL, but I did not do the test.

How sessions are stored on the server side

By default, the session is stored on the server as a file. The relevant settings can be made in the php.ini file:

session.save_handler=files session.save"N;MODE;/path"

There are four Save_handler values:

    1. filesFlat File
    2. mmShared memory
    3. sqliteSQLite database
    4. userUser-defined Functions

With the user, you can use a custom function to mate with the session, which looks very powerful. But there's no time for real. Be free to study again.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

About PHP Sessions: Session and Cookie

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.