PHP uses cookies to control access authorization methods, Phpcookie Access Authorization _php Tutorial

Source: Internet
Author: User

PHP uses cookies to control access authorization methods, Phpcookie access authorization


This example describes how PHP uses cookies to control access authorization. Share to everyone for your reference. Specific as follows:

Copy CodeThe code is as follows: <?php
if (isset ($_post[' name ')) | | Isset ($_post[' pass ')) {
If a form has been submitted
Detect required values in a form
if (Empty ($_post[' name '])) {
Die ("Please enter user name! ");
}
if (Empty ($_post[' pass '))) {
Die ("Please enter the password!") ");
}
Setting Database variables
$host = "localhost";
$user = "root";
$pass = "zq19890319";
$db = "Cookie";
Open 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 recordset returned
if (mysql_num_rows ($result) = = 1) {
If a row of records returns
Indicates that validation has passed
Create a session, set a login flag to 1, and save the current user name in a cookie
Session_Start ();
$_session[' auth '] = 1;
Setcookie ("username", $_post[' name '], time () + (84600*30));
echo "User access is authorized! ";
}else{
echo "Wrong user name or password! ";
}
Release recordset
Mysql_free_result ($result);
Close the database
Mysql_close ($connection);
}
else{
If there is no form submission, an HTML form is displayed
?>







<?php
}
?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/945696.html www.bkjia.com true http://www.bkjia.com/PHPjc/945696.html techarticle PHP uses cookies to control access authorization methods, Phpcookie Access Authorization This article describes how PHP uses cookies to control access authorization. Share to everyone for your reference. Specific as follows: ...

  • Related Article

    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.