Cookies
What is a cookie?
A cookie is a small text file stored on a client's hard disk that can be used to hold information about a user.
What is the use of cookies?
Make the Web page more targeted, save the user's important information (preferences, site visits, shopping cart in the shopping history, etc.);
What are the drawbacks of cookies?
Cookies are not generally used to hold large amounts of data, and cookies are stored in clear text in a way that is unsafe for the user's information. The user can also disable the use of cookies through browser settings.
The process of creating a cookie?
Cookies through the server side of the program through the HTTP request and response to the browser, is the header portion of HTTP, must be sent before the other content of the Web page is sent. This must be important. Doing so will cause the program to crash (even if an echo statement was sent before the cookie).
Create a cookie using Setcookie
BOOL Setcookie (string name[,string value,int expire])
Name of the cookie the value of the cookie expire the expiration time (in seconds to accept UNIX timestamp units)
Setcookie ("MyCookie", "PHP from Beginner to Proficient", time () +60);//expiration is 60s
Get the value of a cookie?
By global variable $_cookie["Cookie_name") to get
Use Isset ($_cookie["Cookie_name"]) to determine whether a COOKIE exists
<?phpif (!isset ($_cookie["name"])) {Setcookie ("name", "Joedlut", Time () +60); echo "Set Cookie name to Joedlut";} else{echo $_cookie["name"];}
How do I delete cookies?
There are two ways of doing it?
Delete cookies via Setcookie ()
Setcookie ("name", "", Time ()-1);//simply set the second parameter to a null value, and the cookie expires at less than the current time of the system.
2. Manually delete cookies using the browser
=============================================================
2.Session
A session is a file stored on the server side that holds the current user-specific data and related information. Stronger than cookies, can manage large amounts of data, and is relatively secure. Use session to save arrays, strings, objects, and so on.
Session life cycle: Start with the user access page, until you disconnect the site.
The session uses a unique sessionid to distinguish between different users.
When the session comes back in, the SessionID is stored in the client (session Cookie) and server two locations respectively.
Start a PHP session?
Use the Session_Start () function
void session_start (void);
<?php session_start (); $string = "I am ceo,you son of Bitch"; if (Empty ($_session["name"])) {$_session["name"] = $string; echo $_session["name"]; }else{echo $_session["name"]; }
<?php session_start (); $array = Array ("PHP", "Java", "Python", "Shell", "Perl"); $_session["book"] = $array; foreach ($_session["book"] as $value) {echo $value. " <br> "; }
Delete session?
Delete a single session
unset ($_session["name"]);
Note Unset ($_session) removes the entire $_session, causing the user to not register the $_session variable
2. Delete multiple sessions
$_session=array ();
3. End the current session
Session_destroy ();
========================= Combat: Control page access through the session ================================
This article is from the "thick Product Thin Hair" blog, please make sure to keep this source http://joedlut.blog.51cto.com/6570198/1855581
Getting Started with PHP (9) Cookies and session