How does the HTTP database in PHP validate the login? Let's take a look at an example implementation of database authentication login for HTTP in PHP.
1. about how file authentication works
Parse the login account and password for the. htpasswd file that was created.
Extract the HTTP Authentication login ID and password from the foreground
$_server[' Php_auth_user ' and $_server[' PHP_AUTH_PW ']
Roughly the same as the code and about the database way
Code address
https://github.com/956077081/PHP_demo/blob/master/HttpLogin.php
2. File comparison on a database-based approach
<?php //Verify HTTP two parameters//$_server[' php_auth_user ']//$_server[' php_auth_pw '];//echo $_server[' Php_auth_user '] ." \ r \ n ";//echo $_server[' PHP_AUTH_PW '];function authenticate_user () { header (" Www-authenticate:basic realm = ') Project ' "); Header ("http/1.1 401 Unauthorized");} $user = $_server[' Php_auth_user '); $passwd = $_server[' php_auth_pw '];if (!isset ($user) | |! Isset ($passwd)) { authenticate_user ();} else{ $db = new mysqli ("localhost", "root", "123456", "Httpauth"); $stm = $db->prepare ("Select name, passwd from Auth where name=? and passwd=? "); $stm->bind_param ("ss", $user, $passwd); $stm->execute (); $stm->store_result (); if ($stm->num_rows = = 0) { authenticate_user (); } else{ echo "You is sucessful to login!"; }
corresponding database (Httpauth) PlainText authentication method can also do the database and PHP original data encryption comparison
use httpauth;create table autht (id int (TEN) UNSIGNED not NULL Auto_increment,name varchar (+) NOT null default ' ', passwd varchar (+) NOT null default ' ', Harsh varchar (default '), PRIMARY KEY (Id,nam e), INDEX ' ASD ' (name) ENGINE = INNODB, DEFAULT CHARSET = UTF8 comment= "http login Verification"