How php uses cookies to control access authorization

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.