Application of Cookies
Eg8-2.php A simple example of printing the contents of an $_cookie array
<? Php//eg:8.2 $value= "My Cookie value"; //send a simple cookie Setcookie("TestCookie",$value, Time() +60*60*24*30);?>PHPif(isset($_cookie["TestCookie"])) Echo($_cookie["TestCookie"]. "<BR>"); Print_r($_cookie); ?></body>Eg8-3.php record the number of times a user accesses the current page
<?PHPif(isset($_cookie["Num"])) $num=$_cookie["Num"]; Else $num= 0; $num=$num+1; Setcookie("Num",$num, Time() +60*60*24*30); /*Delete cookie data setcookie ("num", $num, Time ()-3600); is actually set the validity period to the past time*/?>PHPif($num>1) Echo("You are already the first".)$num. "This site has been visited. "); Else Echo("You are welcome to visit this site for the first time. "); ?><br><br> below is the body of the Web page <BR></body>————————————————————————————————————————————————————————————————————————
Use cookies when authenticating users
login.php
functionForm_onsubmit (obj) {if(Obj.txtUserName.value = = "") {alert ("Please enter user name"); return false; } if(Obj.txtPwd.value = = "") {alert ("Please enter your password"); return false; } return true;}</script><body><form name= "Form1" method= "Post" action= "" > <label></label></form ><div align= "center" > $_cookie["username"]); ?> "Size=" ></td>User Information<!--Add the ability to read user name information from cookies-</tr> <tr> <td> &L T;p style= "MARGIN-TOP:12PX; margin-bottom:12px "> Password:</td> <td> <p style=" MARGIN-TOP:12PX; margin-bottom:12px "> <input type=" password "name=" txtpwd "value=" <? PHP Echo ($_cookie["Password"]); ?> "size=" > <!--Add the ability to read password information from a cookie-<input name= "Checkboxcookie" type= " CheckBox "Checked> <label> retain user information for one year </label></td> </tr> < ;tr> <td colspan= "2" > <p align= "center" style= "MARGIN-TOP:12PX; margin-bottom:12px "> <input type=" Submit "value=" Login "name=" B1 "style=" FONT-SIZE:11PT; Font-family: Song body "onclick=" return Form_onsubmit (this.form) "> <input type= "reset" value= "reset" name= "B2" style= "font-family: Song body; font-size:11pt "></td> </tr> </table> <p>   ; </p> </form> <p> </div></body>View Codecheck.php
<?PHP//take the username and password you entered $UID=$_post[' txtUserName ']; $PWD=$_post[' Txtpwd ']; //Verify user name and password if($UID= = "Admin" and$PWD= = "Pass") { Echo("You are already logged in successfully, welcome.") "); if($_post[' Checkboxcookie ']== "on") { Setcookie("username",$UID, Time() +60*60*24*365); Setcookie("Password",$PWD, Time() +60*60*24*365); } } Else Echo("Login failed, please return to login again.") ");?>
————————————————————————————————————————————————————————————————————————
Session
Eg8-5.php the session ID and session name
<? PHP Session_Start (); Echo session_id ()); Echo ("<br>"); Echo Session_name ());? >
eg8-6.php example of accessing SESSION data using global array $_session
<?PHP//global variable array $_session set and get session data, which can be accessed anywhere in the program//error_reporting (0);Date_default_timezone_set (' asia/chongqing ');//System Time Difference 8 hours problem//start session, call before accessing array $_session Session_Start(); if($_session["Last_visit"]) { Echo"The last time you visited was:"; Echo Date("Y-m-d, h:i:s",$_session["Last_visit"]); Echo"<br>"; Echo"Number of visits:".$_session["Num_visits"]; } Else Echo"This is your first visit. "; $_session["last_visit"] = Time(); $_session["Num_visits"]++; /*If you do not close the browser, visit other websites and return, and the session will be retained. When you close the browser, the session data is lost*/?>
eg8-8.php example of using the unset () function to release session variables--delete session variables
<? PHP // Use the unset () function to release the session variable //start session Session_Start (); $_session ["Num_visits"]++; unset ($_session["Num_visits"]); Echo ($_session["Num_visits"]);? >
eg8-9.php example of using the session_unset () function to destroy a session
<?PHP//Use the Session_unset () function to destroy a session//start session error_reporting(0); Session_Start(); //Print_r ($_session); $_session[' user '] = ' admin '; Session_unset(); if($_session[' User ']) Echo("User name:".)$_session[' User ']. "<br> session_id=".session_id()); Else Echo("No username found". "<br> session_id=".session_id()); $_session[' user '] = ' admin '; /*The result is: No username found Session_id=hufnddl8v5lpm6j3bar1m3j8u5 thus we know: Session_unset () did not release (destroy) session ID */?>
eg8-10.php example of using the Session_destroy () function to destroy a session
<?PHP/*Use the Session_destroy () function to destroy the session, you can delete the current user corresponding session file, release SessionID, in-memory $_session variable content remains. */ //Start session//error_reporting (0); Session_Start(); //Print_r ($_session); $_session[' user '] = ' admin '; Session_destroy(); Echo("User name:".)$_session[' User ']); Echo("<br> session_id=".session_id()); /*The result is: User name: admin session_id=*/?>
:
PHP Learning notes: PHP session processing (Cookies and session)