实现用户注册登录的功能,共需要8个页面:
1,login.php 登陆页面
2,check.php 验证页面
3,mysql_connect().php 数据库连接页面
4,ms_login.php 从数据库中提取数据验证
5,register.php 注册页面
6,register_1.php 是注册验证,并写入到数据库中
7,showing.php 图片验证码
8,huangying.html 是登陆成功的页面
下面我们逐一来实现。
1,login.php 登陆页面
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>php登录页面_www.</title>
</head>
<style type="text/CSS">
.div
{
height:1000px;
width:700px;
text-align:center;
margin:20px;
}
.text
{
}
.button
{
font-size:10px;
}
</style>
<body>
<form method="post" action="/blog_article/check.html">
<div >
用户名<input type="text" name="name" >
密码:<input type="password" name="password">
<div >
<input type="submit" value="提交">
<input type="reset" value="清除">
<a href="/blog_article/register.html" >注 册</a>
</div>
</div>
</form>
</body>
</html>
2,check.php 登录验证页面
<?php
/**
* 登录验证
* edit www.
*/
require_once("ms_login.php");
//require_once ("mysql_connect.php");
$name=$_POST['name'];
$password=$_POST['password'];
if($name == "")
{
echo "请填写用户名<br>";
echo"<script type='text/javascript'>alert('请填写用户名');location='login.php'; </script>";
}
elseif($password == "")
{
//echo "请填写密码<br><a href='/blog_article/login.html'>返回</a>";
echo"<script type='text/javascript'>alert('请填写密码');location='login.php';</script>";
}
else
{
$colum=collect_data();
if(($colum['name'] == $name) && ($colum['password'] == $password))
{
//echo "验证成功!<br>";
echo"<script type='text/javascript'>alert('登陆成功');location='huanying.html';</script>";
}
else
//echo "密码错误<br>";
echo"<script type='text/javascript'>alert('密码错误');location='login.php';</script>";
//echo "<a href='/blog_article/login.html'>返回</a>";
}
?>
3,mysql_connect.php 数据库连接页面
<?php
// 连接服务器,并且选择test数据库
function(){
$db = mysql_connect("localhost","root","123")
or die("连接数据库失败!");
mysql_select_db("user")
or die ("不能连接到user".mysql_error());
}
?>
4,ms_login.php 从数据库中提取数据验证
<?php
//从test数据库中提取数据
function collect_data(){
require_once ("mysql_connect.php");
$sql = "select * from user";
$result = mysql_query()($sql);
$colum= mysql_fetch_array($result);
return colum;
}
?>
5,register.php 注册页面
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>php用户注册页面_www.</title>
</head>
<style type="text/css">
.div
{
height:1000px;
width:700px;
text-align:center;
margin:40px;
}
.text
{
font-size:20px;
margin:20px;
}
.button
{
font-size:10px;
}
</style>
<body>
<h1>注册页面</h1>
<form method="post" action="/blog_article/register_1.html">
<div >
<div >
用户名<input type="text" name="name" ></div>
<div >
密码:<input type="password" name="password"></div>
<div >
再次输入密码:<input type="password" name="pwd_again"></div>
<div >
验证码:<input type="text" name="check"><img src="/blog_article/showimg.html"></img></div>
<div >
<input type="radio" name="agree" value="是否同意我们的条款">同意我们的条款?</div>
<input type="submit" value="提交">
<input type="reset" value="清除">
</div>
</form>
</body>
</html>
6,register_1.php 是注册验证,并写入到数据库中
<?php
/**
* 用户注册验证 写入数据库
* edit www.
*/
require_once 'mysql_connect.php';
$name=$_POST['name'];
$password=$_POST['password'];
$pwd_again=$_POST['pwd_again'];
$code=$_POST['check'];
if($name==""|| $password=="")
{
echo"用户名或者密码不能为空";
}
else
{
if($password!=$pwd_again)
{
echo"两次输入的密码不一致,请重新输入!";
echo"<a href='register.php'>重新输入</a>";
}
else if($code!=$_SESSION['check'])
{
echo"验证码错误!";
}
else
{
$sql="insert into user values('105','$name','$password')";
$result=mysql_query($sql);
if(!$result)
{
echo"注册不成功!";
echo"<a href='register.php'>返回</a>";
}
else
{
echo"注册成功!";
}
}
}
?>
7,showing.php 图片验证码
<?php
/* 网站验证码程序
* 运行环境: PHP5.0.18 下调试通过
* 需要 gd2 图形库支持(PHP.INI中 php_gd2.dll开启)
*
*/
//随机生成一个4位数的数字验证码
$num="";
for($i=0;$i<4;$i++){
$num .= rand(0,9);
}
//4位验证码也可以用rand(1000,9999)直接生成
//将生成的验证码写入session,备验证页面使用
Session_start();
$_SESSION["check"] = $num;
//创建图片,定义颜色值
Header("Content-type: image/PNG");
srand((double)microtime()*1000000);
$im = imagecreate(60,20);
$black = ImageColorAllocate($im, 0,0,0);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,0,0,$gray);
//随机绘制两条虚线,起干扰作用
$style = array($black, $black, $black, $black, $black, $gray, $gray, $gray, $gray, $gray);
imagesetstyle($im, $style);
$y1=rand(0,20);
$y2=rand(0,20);
$y3=rand(0,20);
$y4=rand(0,20);
imageline($im, 0, $y1, 60, $y3, IMG_COLOR_STYLED);
imageline($im, 0, $y2, 60, $y4, IMG_COLOR_STYLED);
//在画布上随机生成大量黑点,起干扰作用;
for($i=0;$i<80;$i++)
{
imagesetpixel($im, rand(0,60), rand(0,20), $black);
}
//将四个数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成
$strx=rand(3,8);
for($i=0;$i<4;$i++){
$strpos=rand(1,6);
imagestring($im,5,$strx,$strpos, substr($num,$i,1), $black);
$strx+=rand(8,12);
}
ImagePNG($im);
ImageDestroy($im);
?>
有关php验证码的内容,大家还可以参考:
一个php验证码的封装类
php自定义大小验证码的实例代码
php生成扭曲及旋转的验证码图片的实例代码
php仿QQ验证码的实现代码
php5验证码类(简易实用型)
php验证码(GD库生成验证码)的例子
php GD库生成验证码的实例
一个比较稳定的php登陆系统验证码
用php生成带有雪花背景的验证码
8,huangying.html 登陆成功的页面
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>欢迎光临_www.</title> </head> <body> <h1>,欢迎您的光临。</h1> </body> </html>
一个完整理的php+mysql+ajax的用户注册实例程序,可以提供检测用户名是否被注册。
本程序包括:
reg.html 用户注册html页面
reg.php php处理代码
conn.php 数据库教程连接文件
1,reg.html代码
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>php+ajax用户注册验证用户是否在存_www.</title>
<style type="text/css教程">
body{
font-size:12px;
text-align:center;
}
.text{
width:180px;
height:12px;
}
p{
width:600px;
height:20px;
line-height:20px;
text-align:left;
}
p label{
display:block;
width:80px;
height:20px;
line-height:20px;
float:left;
text-align:right;
}
p span{
margin-left:10px;
}
</style>
</head>
<body>
<script language="网页特效">
function createxmlhttprequest(){
var xmlhttp;
if(window.activexobject){
xmlhttp = new activexobject("microsoft.xmlhttp");
}else if(window.xmlhttprequest){
xmlhttp = new xmlhttprequest();
}
return xmlhttp;
}
function checkname(){
var name = document.getelementbyid(name); //获取用户名文本框
var span = document.getelementbyid(name_info); //获取用于显示结果的span标记
if(name.value.length <= 4){
span.style.color = #ff0000; //设置span标记内的字体颜色为红色
span.innerhtml = 用户名长度不能少于4个字符!; //span标记内容
return false;
}
var xmlhttp = createxmlhttprequest();//创建异步请求对象
var time = new date().gettime();
var url = reg.php?act=reg&name= + name.value.tolowercase() + &tmp= + time;//构造出请求地址
xmlhttp.open("get",url,true); //建立一个异步请求
/*这里我们使用get方式请求
post方式的请求基本差不多,朋友们自己试试如果不行,在下面给我留言*/
xmlhttp.onreadystatechange = function(){ //监视请求状态
span.style.color = #ff9900;
span.innerhtml = 查询中,请稍候!;
if(xmlhttp.readystate == 4 && xmlhttp.status == 200){
if(xmlhttp.responsetext.indexof(no) != -1){ //如果服务器返回的信息中有no
span.style.color = #cb2121; //设置span标记颜色为红色
span.innerhtml = 用户名[ + name.value + ]已经被别的用户使用!;
}else{//如果返回信息中没有no
span.style.color = #00a800;//设置颜色为绿色
span.innerhtml = 恭喜您,该用户名未被注册!;
}
return true;
delete xmlhttp; //删除请求对象
}
}
xmlhttp.send(null); //发送请求
}
</script>
<form method="post" action="/blog_article/reg.html">
<p><label>用户名:</label><input type="text" id="name" name="user_name"/><span id="name_info"></span></p>
<p><label></label><input type="button" value="检查用户名" onclick="checkname()"/></p>
<p><label>密码:</label><input type="password" /></p>
<p><label> </label><input type="submit" value="注册" /></p>
</form>
</body>
</html>
2,reg.php 用户注册程序
<?php
/**
* 用户注册程序
* edit www.
*/
include("conn.php");
$name=$_get[name];
$name=urldecode($name);
if(strlen($name)>0){
$sql="select username from registration where username = $name";
$result=mysql_query()($sql);
$row=mysql_fetch_array($result);
if($_get[act] == reg){
if(!empty($row[username])){ //只要注册用户名为kaixin的时候,注册页面就会用红色字体提示次用户已被注册!
echo no;
}else{
echo yes;
}
}
}
?>
3,conn.php 数据库文件
<?php
/* created on 下午12:08:25*/
$conn=@mysql_connect()("localhost","root","")or die("www.提示你:连接失败!");
mysql_select_db("reg",$conn);
mysql_query("set names gbk");
?>
4,附,registration 数据表结构
-- phpmyadmin sql dump -- version 2.11.2.1 -- http://www.phpmyadmin.net -- -- 主机: localhost -- 生成日期: 2009 年 05 月 20 日 05:29 -- 服务器版本: 5.0.45 -- php 版本: 5.2.5 set sql_mode="no_auto_value_on_zero"; -- -- 数据库: `reg` -- -- -------------------------------------------------------- -- -- 表的结构 `registration` phpmyadmin导入数据 -- create table `registration` ( `id` tinyint(6) not null auto_increment, `username` varchar(14) not null comment 注册用户名, `userpwd` varchar(14) not null comment 注册密码, primary key (`id`) ) engine=innodb default charset=gb2312 auto_increment=6 ; -- -- 导出表中的数据 `registration` -- insert into `registration` (`id`, `username`, `userpwd`) values (1, admin, admin888), (2, lyn, www.), (3, xiaot, xiaot), (4, xiaoe, xiaoe), (5, www., 5201314110);
了解下用户认证的原理:
首先,需要用户在页面上填入用户名和密码,未注册的用户会提示请先注册。
然后,调用数据库搜索是否有相应的用户。如果有就确认, 无则提醒用户先注册。
以下是用php实现用户登录验证的完整步骤与相关代码。供大家参考。
第一步,制作一个用于登录的页面。
第二步,开始登录后的确认程序的设计。
1,登录页面 login.php:
<?php
mysql_connect()("localhost","user","password")
/*连接数据库,用户名和密码自行修改*/
or die("无法连接数据库,请重试");
mysql_select_db("userinfo")
or die("无法选择数据库,请重试");
$today=date("Y-m-d H:i:s");
$query="
select id
from usertbl
where name=$name and password=$password
/*从数据库中搜索和登录用户相应的资料*/
";
$result=mysql_query()($query);
$numrows=mysql_num_rows($result);
if($numrows==0){
/*验证是否能找出相同资料的用户,不能则未注册*/
echo 非法用户
;
echo 请注册先
;
echo 重试
;
}
else{
$row=mysql_fetch_array($result);
$id=$row[0];
$query="
update usertbl
set lastlogin=$today
where id=$id";
$result=mysql_query($query);
SetCookie("usercookie", "欢迎你,$name");
/*使用cookie,页面认证。
但我未开发完这一块。希望有兴趣的朋友指正*/
echo 登录成功
;
echo 请进!
;
}
?>
第三步,制作一个用于注册的页面。
第四步,注册后的身份确认和输入数据库。
register.php:
<?php
/**
* 用户注册信息确认
* edit www.
*/
mysql_connect("localhost","user","password")
/*请修改用户名和密码*/
or die("无法连接数据库,请重试");
mysql_select_db("userinfo")
or die("无法选择数据库,请重试");
$query="select id from usertbl where name=$name/";
/*从数据库中搜索相同名字的用户资料*/
$result=mysql_query($query);
$numrows=mysql_num_rows($result);
if($numrows!=0) /*找到了当然就是有人先注册了相同的名字*/
{echo 已有人注册此名,请重新选择名字!;}
else
{$query="insert into usertbl values(0,$name,$password,/)";
/*找不到相同的就输入新的用户资料*/
mysql_query($query);
echo 注册成功;
echo 请登录!;}
?>
下一步是cookie的使用,原打算使用cookie来使每一页都能识别用户身份,由于别的页面尚在制作中,所以就想到了使用PHP的引用:
<?php
if(!$usercookie)
{header("非法用户");
}
?>
welcome.php:
<?php
require("cookie.php"); /*调用cookie.php*/
echo $usercookie;
?>
至此一个很简单的用户认证系统就完成了。
最后,别忘记了创建用户数据库。
下面是我的数据库表的结构,数据库名称为:userinfo。
sql代码:
create table usertbl ( ID int auto_increment primary key, Name varchar(30), Password varchar(20), Lastlogin varchar(20) );