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.a
And associate this string with my id (1030000000093602 ).ServerIn this way, the server will knowdiM4og.6c3c602eff435b68a0e2aeca90254482.a
Which 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.a
This 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 yesid
It 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$_SESSION
When 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 isPHPSESSION
Php 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.