1. As the HTTP protocol is a stateless protocol, so the server needs to record the state of the user, you need to use some kind of mechanism to know the specific user, this mechanism is Session. typical scenario such as shopping cart, when you click the order button, because the HTTP protocol is stateless, so do not know which user operation, so the server to create a specific user for a specific user Session, used to identify the user, and track the user, so as to know how many books inside the shopping cart. This Session is saved on the server side and has a unique identifier. There are many ways to save Sessions on the server side, in-memory, database, and file. When clustering should also consider the transfer of Session, in large sites, there will generally be a dedicated cluster of Session servers, used to save user sessions, this time Session information is placed in memory, using some caching services such as Memcached and so on to put Session.
3. Cookies can actually be used in some user-friendly scenarios. Imagine you've logged into a website one time and don't want to enter your account again the next time you log in. This information can be written to a cookie, and when you visit the website, the script on the website page can read this information and automatically fill in the username for you, which can be convenient for the user. That's where the cookie name comes from, a little sweetener for the user.
So, to summarize. Session is a data structure kept on the server side to track the status of the user, this data can be kept in clusters, databases, files. Cookie is a mechanism for the client to save user information, used to record some information about the user, but also a way to implement Session.
session_start() will do two things.
Where and how this file is stored is related to how the program is executed and varies from browser to browser; this step produces a serialized string - PHPSESSID.
The storage location is specified by the session.save_path parameter, the name is similar to "sess_b2f326ee7a8b7617c215a30d22a602f1", "sess_" represents this is a session file, "b2f326ee7a8b7617c215a30d22a602f1" that is, the PHPSESSID of this session, and the client's PHPSESSID must be the same. This file holds the specific values in the $_SESSION variable, in the format of
Variable name | Variable type : [length] : value
session_id() Retrieves the current session ID. vocabulary: string session_id(string [id]);
This function obtains or reconfigures the code that currently holds the Session. Without the id parameter, only the current Session ID is obtained; with the id parameter, the Session ID is set to the newly specified id. The input and return are strings. exports session_id()
<?php session_start(); echo session_id(); // exports dqr58dnuqj2gufvg4o3tmjb9v4 ?>
<?php session_id("NowaMagic"); session_start(); echo session_id(); // exports NowaMagic ?>
Extensions for .session: where the default session is stored. In the php.ini configuration file there is this line session.save_handler = files, files， Explainedphp The default is to save with file read/writesession of。 So which directory is it in?？ Keep looking.。session.save_path = "/tmp", This line is preceded by a ; , the instructions are commented out, but even so, the default php The session is also saved here, in the /tmp directory. Above.