Home  >  Article  >  Backend Development  >  How to verify login in http database in PHP

How to verify login in http database in PHP

不言
不言Original
2018-07-19 16:08:473516browse

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[&#39;PHP_AUTH_USER&#39;]
//$_SERVER[&#39;PHP_AUTH_PW&#39;];
//echo $_SERVER[&#39;PHP_AUTH_USER&#39;]."\r\n";
//echo $_SERVER[&#39;PHP_AUTH_PW&#39;];
function authenticate_user ()
{
    header("WWW-Authenticate: Basic realm =&#39;Project&#39;");
    header("HTTP/1.1 401 unauthorized");
}
$user = $_SERVER[&#39;PHP_AUTH_USER&#39;];
$passwd =  $_SERVER[&#39;PHP_AUTH_PW&#39;];
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 &#39;&#39; ,
passwd VARCHAR(50)  not NULL DEFAULT &#39;&#39;,
harsh VARCHAR(50)  DEFAULT &#39;&#39;,
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!

Statement:
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