Server-side session technology (communication Protocol)
By subway:
Shenzhen Tong
Get the phone brush.
Cookies (data stored in the client's browser)
Session (data stored in the Web server)
How do I use cookies?
1. Set/unregister cookies (new cookie.php)
<?php/*array (' user ' = ' ghostwu ') *///function Setcookie (c_name,value,expiredays) //variable name, value, Expiration days Setcookie ( "User", "Zhangsan");//Set Success if (Isset ($_get[' act ') && $_get[' act '] = = ' logout ') { //time (): Gets the current timestamp Setcookie ("User", "", Time ()-1);//Clears the user's value and expires it}?> <a href= "? act=logout" > Logout </a>
2. Remove the cookie (new cookie2.php)
<?php /* All cookies inside php are stored in the Super global variable $_cookie $_cookie is a class array stored in the browser * //Print_r ($_cookie) ; echo $_cookie[' user '; if (!empty ($_cookie[' user ')) { echo $_cookie[' user ']; }? >
3. A login page for setting cookies (new login.php)
Use cookie to save username and password when login is successful
Setcookie ("name", $userName); /
Setcookie ("pwd", $USERPWD);
<! DOCTYPE html>
4. Use the saved cookie---> Login Success page (new success.php)
<! DOCTYPE html>
How do I use the session?
1. Set/Logoff session(new session. php)
<?php /* To use the session, you must first open the session with Session_Start () An array of classes is created on the Web server $_session * / Session_Start (); $_session[' name '] = ' zhangsan '; if (Isset ($_get[' act ')) && $_get[' act '] = = ' Logout ' ) { //delete an item of an array, or delete a variable unset ($_session[ ' name ']); }? ><a href= "? act=logout" > Logout </a>
2. Remove session(new session2.php)
<?php session_start (); if (!empty ($_session[' name ')) { echo $_session[' name ']; }? >
3. One setting session Login page for (new login2.php)
<! DOCTYPE html> 4. Use saved Session ---> Login Success page (new succeed2.php)
<! DOCTYPE html> 5. Save the session ---> registration page (new reg.php) when the registration page is registered successfully
<! DOCTYPE html>
Front-end small White's daily learning record----PHP (7) Session and Cookie