For a front-end developer, a cookie I think everyone is familiar with, often encapsulating some methods such as Setcookie,getcookie, the session is like a most familiar stranger, when we work with the backend developer project will use it, but do not understand its nature, Let's take a detailed discussion of the following
Cookies
storage location : stored on client
role : Store data across pages in this domain (we seem to use it generally for username,passward)
A cookie typically contains information such as:
Transmission : Here is an HTTP request message
Every time a request is sent, the cookie is sent back to the background with the HTTP message
The relationship between a cookie and a session
Session
Below I use the PHP language as a case, to explain the session
From the above you can see that the session is stored on the server side and stored as a file.
Session has a lot of features, such as the expiration time, and so on, we look at, open the php.ini file (there is a lot of PHP configuration information, I have to remove more than n comments)
I'll take a look at that SessionID.
As you can see, it coincides with the Session.name = "Phpsessid" above.
We already know how the front-end browser is carrying SessionID transmission to the background, as well as the background storage session file location, the background specifically using SessionID analysis and utilization, we continue to analyze
To analyze the mechanism, see the following PHP code
<?phpSession_Start (); Header"Content-type:text/html;charset=utf-8");if($_session[' username '] !=' Success ') {/ */php/index.php is the current file path * / $string= <<< EOF <form action="/php/index.php"Method="POST"> <input type="Text"Name="Value"> <input type="Submit"> </form>EOF;Echo $string; }if($_session[' username '] ==' Success ') {Echo "Login Successful". Php_eol; }if($_post[' value '] ==' ys ') {$_session[' username '] =' Success ';Echo "Login Successful". Php_eol; }?>
Executes the PHP file and experiences the session process
The SessionID in the header of the cookie that found HTTP is the same as the server session file name
This allows you to find a unique session file based on each request session, and then see what the file looks like.
username|S:7:"success";s|S:7:"success";ss|S:7:"success";
This is the contents of this file, there are S,ss,username, where s and SS is the name I tested before (can be ignored), that is,
username|S:7:"success";
Then each time the background can determine the unique session according to SessionID, set such as $_session[' username ', such as to determine, to achieve the communication between the cookie.
Conclusion
At this point, the relationship between the cookie and the session is finished, where there is a place to explain the wrong, or there is a place to add, welcome to comment, Thank you ~
Deep understanding of browser session mechanism (session && cookie)