How the HTTP database in PHP is validated for login

Source: Internet
Author: User
Tags http authentication
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" 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.