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