How to verify login in http database in PHP? Next, let’s take a look at an implementation example of http database authentication login in PHP.
1. About the file authentication method
Parse the login account and password stored in the created .htpasswd file
Extract the http sent from the front desk Verify the login account and password
$_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW']
Compare the code and the database method respectively
Code address
https://github.com/956077081/PHP_demo/blob/master/HttpLogin.php
2. About file comparison based on database
<?php //验证Http的两个参数 //$_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 are sucessful to login !"; } }
Corresponding database (httpauth) The plain text verification method can also be used to encrypt and compare the database and PHP original data by yourself
use httpauth; create table autht( id int(10) UNSIGNED not NULL AUTO_INCREMENT, name VARCHAR(50) NOT NULL DEFAULT '' , passwd VARCHAR(50) not NULL DEFAULT '', harsh VARCHAR(50) DEFAULT '', PRIMARY KEY(id,name), INDEX `asd` (name) ) ENGINE = INNODB , DEFAULT CHARSET = UTF8 COMMENT="HTTP登录验证"
Related recommendations:
php User cookie login verification and mysql data login verification methods
php uses curl to simulate logging into a website with a verification code, curl verification code
The above is the detailed content of How to verify login in http database in PHP. For more information, please follow other related articles on the PHP Chinese website!