1. Session
(1) The origin and introduction of Session
Session: in In computers, especially in network applications, it is called "session control". The survival time is the time that elapses from entering the website to closing the website when the user browses the website, that is, the time it takes the user to browse the website. time.
Since Http is a stateless protocol, it is only responsible for requesting the server. When it responds to the server, it loses contact with the browser. The user's personal information cannot be saved, just like the relationship between a shopping mall and a vending machine or ordinary people, so in order to make up for this shortcoming, Session came out, and Session needs to be version 4.1 or above.
php Chinese website learning topic: php session (including pictures, texts, videos, cases)
(2) Session mechanism
The session mechanism adopts a solution to maintain state on the server side, and has its own corresponding unique identifier session_id, which distinguishes the previous information of different users
For example: when we go to a restaurant to eat, we have a membership card with my personal information and validity period. The restaurant can find my personal information every time I come.
( 3) How session works
1>When a session is enabled for the first time, a unique identifier is stored in a local cookie.
2>First use the session_start() function, and PHP loads the stored session variables from the session warehouse.
3>When executing a PHP script, register the session variable by using the session_register() function.
4>When the PHP script execution ends, the session variables that have not been destroyed will be automatically saved in the local session library under a certain path. This path can be specified by session.save_path in the php.ini file. You can load it and use it the next time you browse the web.
Saved files:
(4) What are the practical applications of Session projects
1> Record user login information, use global
2> to create verification codes, and verify comparison operations
3> Saving sessions to the database can achieve: a. Controlling that only one person can log in to an account
(5) Advantages and Disadvantages of Session
1> Advantages of session: uniqueness, convenient calling, not occupying too many resources, it is a group of temporary files stored on the server data. Under normal circumstances, when we log in as a user, we will store the user data in the session. In this way, it can be easily called on any page, and each client will generate a unique session_id, so there will be no confusion. And after closing the browser, the server will have a session recycling mechanism to automatically delete expired sessions
2> Disadvantages: It is saved in the form of cookies on the client, and it is useless if cookies are disabled.
(6) The connection and difference between Session and Cookie
1>Contact: Session also needs to save an identity on the client side, so it is necessary to use Cookie. Session is The session and cookie that work through cookies are connected through $_COOKIE['PHPSESSID']. Through $_COOKIE['PHPSESSID'], you can know the session ID and obtain other information.
2> Difference: The cookie mechanism uses a solution that maintains state on the client (browser), while the session mechanism uses a solution that maintains state on the server side
( 7) Use of Session
sesstion_start()
; // First open the session
$_SESSION['user'] = 'username'
; // Store username in it
echo $_SESSION['user']
; // Directly output username
session_destroy()
; // Destroy session
2. Cookie
(1) Origin and introduction of Cookie
Cookie is used as the Internet The product is generated to save some basic information of the user. It can also be understood as some information temporarily saved by the server on your computer. Next time you come, take a look at this information and then load it directly. Send specific information.
(2) Cookie mechanism
The mechanism of Cookie and Session are similar. They will generate a specific value for each user. This time it is in the client Saved on the end, when we open the website that saves our personal information in the cookie, it will submit our personal information to the server, and then request the corresponding information from the server
(3) Principle of Cookie
When you visit a website, you can save a small amount of data from the server to the hard disk, such as user name, id, etc. When you visit next time, the server will first look for this file and then read it. Relevant information, so that you can log in to the website directly without entering your username and password
(4) What are the applications of cookies in actual project
1>Remember username and password
(5) Advantages and disadvantages of Cookie
1>Advantages: a. No server resource space required b. Longer persistence
2>Disadvantages: a. The client size is limited b. If the user disables the cookie, it will be useless c. Unsafe
(6) The difference and connection between Cookie and Session
1>Contact: Cookie can save a unique identifier for the session on the client side
2>Difference: The Cookie mechanism uses a solution that maintains state on the client side (browser), while The session mechanism adopts a solution to maintain state on the server side
(7) Use of Cookie
1> Creation and update: setCookie($cookieName,$value, time() seconds);
Example: setcookie("UserName","imooc",time() 2*7*24*3600);
Value: $uName=$ _COOKIE['UserName'];
2>Delete Cookie
setcookie($cookieName,value,time()-秒数);
//or
setcookie($cookiename, '');
//or
setcookie($cookiename, '');
Instance:
setcookie("UserName","imooc",time()-3600);
The above content is for reference only.
For more related questions, please visit the PHP Chinese website: PHP Video Tutorial
The above is the detailed content of What is the difference between session and cookie in php?. For more information, please follow other related articles on the PHP Chinese website!