How to use cookies to control access authorization in php, phpcookie access authorization_PHP tutorial

WBOY
Release: 2016-07-13 10:09:30
Original
1086 people have browsed it

php uses cookies to control access authorization, phpcookie access authorization

The example in this article describes how PHP uses cookies to control access authorization. Share it with everyone for your reference. The details are as follows:

Copy code The code is as follows:
If(isset($_POST['name'])||isset($_POST['pass'])){
//If a form is submitted
//Detect the required values ​​in the form
If(empty($_POST['name'])){
                die("Please enter your username!");
                                                                                                              If(empty($_POST['pass'])){
                die("Please 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 there is a record set to return
If(mysql_num_rows($result) == 1){
//If there is a row of records, return
//Indicates that the verification has passed
//Create a session, set a login flag to 1, and save the current username in a cookie
session_start();
$_SESSION['auth'] = 1;
              setcookie("username", $_POST['name'], time()+(84600*30));
echo "User access has been authorized!";
          }else{ 
echo "Wrong username or password!";
                                                                                                              //Release the record set
Mysql_free_result($result);
//Close the database
Mysql_close($connection);
}  
else{
//If no form is submitted, display an HTML form
?>



                                                                                                              
                                                         Username
                                                                                              Password
                                                                                                            
                                                                                                                                                                                              
}  
?>

I hope this article will be helpful to everyone’s PHP programming design.

http://www.bkjia.com/PHPjc/945696.html

truehttp: //www.bkjia.com/PHPjc/945696.htmlTechArticlephp uses cookies to control access authorization, phpcookie access authorization. This article describes how php uses cookies to control access authorization. . Share it with everyone for your reference. The details are as follows:...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!