Home > php教程 > php手册 > 浅谈php用户身份认证(二)

浅谈php用户身份认证(二)

WBOY
Release: 2016-06-21 09:13:01
Original
1210 people have browsed it

                  浅谈php用户身份认证(二)      
                        爆米花 2001年12月28日 www.westxj.net
(二)基于http的多用户验证  
  上次给大家介绍了基于http的单用户验证,这次利用mysql数据库
储存多用户数据,进行多用户验证。
1、首先建立mysql数据库
mysql>create database user;        //建立数据库user
mysql>use user;                    //打开数据库user
mysql>create table user_data(      //建立数据表user_data
id int(9) not null aoto_increment, //id为自动增加整数字段
username varchar(10) not null,     //用户姓名
password varcher(10) not noll,     //密码
primary key(id);                   //设id为主键
);
2、身份验证程序
$error = "/www/error/error.php";
if ($PHP_AUTH_PW=="")                //如密码为空
{              
Header("WWW-Authenticate: Basic realm=\"用户登陆\"");
Header("HTTP/1.0 401 Unauthorized"); //验证
include($error);                     //定向error,php文件
exit;
}
else
{
mysql_connect("localhost", "root", "1234");     //连接数据库
$result = mysql_db_query("user","select password
from user_data where username='$PHP_AUTH_USER'");
                           //送查询字符串到mysql数据库中
$row = mysql_fetch_array($result); //返回数组资料
$passwd = $row[0];
mysql_close($db_id);               //关闭数据库  
if ($PHP_AUTH_PW!=$passwd)      //密码验证
{
Header("WWW-Authenticate: Basic realm=\"用户登陆\"");
Header("HTTP/1.0 401 Unauthorized");
include($error);
exit;
}
}   




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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template