php uses cookies to control access authorization, phpcookie access authorization
The example in this article describes how PHP uses cookies to control access authorization. Share it with everyone for your reference. The details are as follows:
Copy code The code is as follows:
If(isset($_POST['name'])||isset($_POST['pass'])){
//If a form is submitted
//Detect the required values in the form
If(empty($_POST['name'])){
die("Please enter your username!");
If(empty($_POST['pass'])){
die("Please enter your password!");
//Set database variables
$host = "localhost";
$user = "root";
$pass = "zq19890319";
$db = "cookie";
//Open the connection
$connection = mysql_connect($host, $user, $pass) or die("Unable to connect!");
//Select a database
mysql_select_db($db) or die("Unable to select database!");
//Create a query
$query = "SELECT * FROM users WHERE name = '".$_POST['name']."' AND pass = SHA1('".$_POST['pass']."')";
//Execute a query
$result = mysql_query($query) or die("Error in query:$query." . mysql_error());
//Whether there is a record set to return
If(mysql_num_rows($result) == 1){
//If there is a row of records, return
//Indicates that the verification has passed
//Create a session, set a login flag to 1, and save the current username in a cookie
session_start();
$_SESSION['auth'] = 1;
setcookie("username", $_POST['name'], time()+(84600*30));
echo "User access has been authorized!";
}else{
echo "Wrong username or password!";
//Release the record set
Mysql_free_result($result);
//Close the database
Mysql_close($connection);
}
else{
//If no form is submitted, display an HTML form
?>
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/945696.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/945696.htmlTechArticlephp uses cookies to control access authorization, phpcookie access authorization. This article describes how php uses cookies to control access authorization. . Share it with everyone for your reference. The details are as follows:...