The use of cookies to record user data on the client without the need to call from the server every time, this can improve the efficiency of the Web page, reduce the pressure on the server
The following example simulates the operation of a login interface, using the invocation of a cookie
Create a database and create a file that is called by the database because you want to connect to the database and call the database's dataCreate a table of users and insert data, where data is inserted into information including user name, password, and permissions
Create a conn.inc.php in the root directory to hold the database connection
conn.inc.php
<?php $mysqli =new mysqli ("localhost", "root", "XXXXX", "sqldb");
connection database included in login.php, using the Mysqli method, the password is encrypted using the MD5 () method<?php if (isset ($_post["sub")) { include "conn.inc.php"; $sql = "SELECT id from users where name= ' {$_post[" name "]} ' and password= '". MD5 ($_post["password"]). "'"; $result = $mysqli->query ($sql); Save Data if ($result->num_rows > 0) { $row = $result->fetch_assoc (); $time =time () *1800; Setcookie ("username", $_post["name"], $time); Setcookie ("UID", $row ["id"], $time); Setcookie ("IsLogin", 1); Header ("Location:index.php"); Jump interface } echo "username password wrong"; }? >
Note that the cookie needs to indicate the length of time the data is saved, "username", "uid", "IsLogin" in the saved dataIndex.php involves page jumps, creating a common jump page for the class comm.php
comm.php: If the return is "IsLogin" to jump
<?php //judgment: If not logged in automatically jump to the login page if (!$_cookie["IsLogin"]) { header ("Location:login.php"); }
Log in successfully after the jump to the index.php page, login is unsuccessful, return to log in againindex.php contains conn.inc.php and comm.php
<?php include "comm.php"; Determine if login success include "conn.inc.php"; echo "User <b>". $_cookie["username"]. " </b> Hello! This is the homepage of the website "; echo "Your permission is as follows:<br>"; $sql = "Select Allow_1,allow_2,allow_3,allow_4 from Users where id= ' {$_cookie[" UID "]} '"; $result = $mysqli->query ($sql); $user = $result->fetch_assoc (); if ($user ["Allow_1"]) { echo "111111111111<br>"; } if ($user ["allow_2"]) { echo "222222222222<br>"; } if ($user ["Allow_3"]) { echo "333333333333<br>"; } if ($user ["Allow_4"]) { echo "444444444444<br>"; } ? > <a href= "test.php" > second page </a><br> <a href= "test2.php" > third page </a><br > <a href= "logout.php" > Exit </a><br>
The last second page and the third page are the same as index.php.Logout logout.php need to log out user information
<?php include "comm.php"; Determine if the login is successful $username =$_cookie["username"]; Remove username //logout Setcookie ("username"); Setcookie ("UID"); Setcookie ("IsLogin"); echo $username. " Goodbye ";? ><br><a href= "login.php" > re-login </a>
Full code:
login.php:
<?php if (Isset ($_post["sub")) {include "conn.inc.php"; $sql = "SELECT id from users where name= ' {$_post[" name "]} ' and password= '". MD5 ($_post["password"]). "'"; $result = $mysqli->query ($sql); Save data if ($result->num_rows > 0) {$row = $result->fetch_assoc (); $time =time () *1800; Setcookie ("username", $_post["name"], $time); Setcookie ("UID", $row ["id"], $time); Setcookie ("IsLogin", 1); Header ("Location:index.php"); Jump interface} echo "User name password is incorrect"; }?>
PHP Learning Cookie Session Control