1.session After logging in to the previous page, if there is no operation for a long time, you need to log in again after refreshing the page.
Features: (1) The session is stored on the server;
(2) Each person (login) saves a copy of the session;
(3) The session has a default Expiration time; (If too many people log in, there will be too much data. Since it is stored on the server, it will put pressure on the server and will be cleared after a certain period of time)
(4) Any arbitrary data can be stored in the session type of data.
From the above characteristics, it can be concluded that the characteristics of session are relatively safety. The disadvantage is that it puts a certain amount of pressure on the server.
(5) When a page needs to use session, you need to add session_start() at the top of the page; it should be written at the top of the page, and no code can appear in front. The code refers to the PHP code, which can be There is HTML code. It is best to develop a habit and write at the top of the page. Just write one page once.
(6) Manipulate session. a. Assignment. b. Value.
Assignment:
<?php session_start();//session开始标签,在最顶部写。?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <?php $_SESSION["uid"] = "zhangsan";//给$_SESSION["uid"]赋值。?> </body> </html>
Value:
The value can be obtained on any page, for example, when making a login page, you can Assign the login name, password name and other information to $_SESSION["uid"]. You can also get the login name, password name and other values from other pages on the same browser, provided that the assigned page has been opened first. If you directly open other pages to get the value, you will not be able to get it. Then, if you close the browser and then open it again, you must first open the assignment page before other pages can get the value.
<?php session_start();?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <?php echo $_SESSION["uid"]?> </body> </html>
2. Cookie
Features: (1) Cookies are stored on the client side, and the browser will have a cookie clearing option when clearing browsing information.
(2) Save one cookie per person.
(3) There is no default expiration time for cookies.
(4) Only strings can be stored.
From the above characteristics, it can be concluded that cookies are not safe and will not put pressure on the server.
If there are no special requirements, both session and cookie can be used, but when it comes to higher security, session should be selected.
The above is the detailed content of Implement the session and cookie data value transfer function in PHP. For more information, please follow other related articles on the PHP Chinese website!