This article mainly introduces how php uses cookies to control access authorization and analyzes the principles and skills of Cookie-Based Access authorization. For more information, see
This article mainly introduces how php uses cookies to control access authorization and analyzes the principles and skills of Cookie-Based Access authorization. For more information, see
This example describes how php uses cookies to control access authorization. Share it with you for your reference. The details are as follows:
The Code is as follows:
<? Php
If (isset ($ _ POST ['name']) | isset ($ _ POST ['pass']) {
// If a form has been submitted
// Check the values in the form
If (empty ($ _ POST ['name']) {
Die ("Enter the user name! ");
}
If (empty ($ _ POST ['pass']) {
Die ("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 a record set is returned
If (mysql_num_rows ($ result) = 1 ){
// If one row of records is returned
// Indicates that the verification has passed
// Create a session, set a login tag to 1, and save the current user name in the cookie
Session_start ();
$ _ SESSION ['auth '] = 1;
Setcookie ("username", $ _ POST ['name'], time () + (84600*30 ));
Echo "User Access authorized! ";
} Else {
Echo "incorrect user name or password! ";
}
// Release the record set
Mysql_free_result ($ result );
// Close the database
Mysql_close ($ connection );
}
Else {
// If no form is submitted, an HTML form is displayed.
?>
<? Php
}
?>
I hope this article will help you with php programming.