PHP Novice Learning Cookie Control access authorization

Source: Internet
Author: User
In a formal project, there will always be applications that have authorization controls, such as RBAC permissions. This article helps beginners to better understand PHP and use cookies by sharing the method of using cookies to control access authorization.

<?php if (isset ($_post[' name ')) | | Isset ($_post[' pass ')) {//If there is a form that has the value required in the Submit//detection form if (empty ($_post[' name '))) {Die ("Please enter User name!          "); } if (Empty ($_post[' pass '))) {Die ("Please enter the password!")          ");          }//Set database variable $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 ')." ";          Executes 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 the validation has passed//establish a Session, set a login flag of 1, and save the current username in the cookie SessIon_start ();              $_session[' auth '] = 1;              Setcookie ("username", $_post[' name '], time () + (84600*30)); echo "User access is authorized!          "; }else{echo "wrong username or password!          ";          }//Release record set Mysql_free_result ($result);      Close Database Mysql_close ($connection);           } else{//If there is no form submission, an HTML form is displayed?> 


Related recommendations:

thinkphp RBAC Experience _php Tutorial

The compilation of the PHP learning calendar

PHP learns Curl's crawler instances

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.