Is writing sessions to the database replaced by PHP's original Session?

Source: Internet
Author: User
I have seen many write sessions into the database. If the session is written to the database, is it equivalent to implementing the database session? Which of the following is invalid? I have seen many write sessions into the database. If the session is written to the database, is it equivalent to implementing the database session? Which of the following is invalid?

Reply content:

I have seen many write sessions into the database. If the session is written to the database, is it equivalent to implementing the database session? Which of the following is invalid?

I think we should first clarify what session is. I personally think that session is an implementation, which is not the same as cookie.

Example login to http://segmentfault.com/

  • When the login succeeds, the server will generatediM4og.6c3c602eff435b68a0e2aeca90254482.aAnd associate this string with my id (1030000000093602 ).ServerIn this way, the server will knowdiM4og.6c3c602eff435b68a0e2aeca90254482.aWhich user does this string belong.

    Of course, the storage method is free to choose, in the form of file storage, database, redis, memcached

  • After the user logs on successfullydiM4og.6c3c602eff435b68a0e2aeca90254482.aThis string is sent to the client browser as a cookie. Taking sf as an example, this string is stored insfsess.

  • In this way, the server will read this string from the cookie sent by the client every time you access the log, and query the server to check whether there is a logon record for this string, if yesidIt indicates that the user has logged on.

This is the implementation principle of session. This is the same for python and java websites.

Next, let's talk about the built-in PHP$_SESSIONWhen we use this code, php actually does the above thing, but it is automatic and does not need to consider the specific implementation. The session string stored on the php server is stored as a file, and the cookie name on the client isPHPSESSIONPhp automatically manages all session expiration information. Of course, you can change the cookie name and expiration time by configuring php.

This is the advantage of php. In fact, the session module that the language needs to implement independently has a piece of code. Of course, this is also its shortcoming, which makes it difficult for us to understand the specific concepts and implementation principles of sessions.

I have seen many write sessions into the database. If the session is written to the database, is it equivalent to implementing the database session? Which of the following is invalid?

Therefore, there is no self-built problem of losing or losing traffic. It is just a better way to verify the session, leaving away the php code of dummies.

In addition, sf should use redis storage and Typecho database storage. For details, refer.

It only changes the location where the session is stored, and the session still exists, but is called from the database.

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.