入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

完善PHP登陆注册页面,同时连接mysql数据库

创建时间:2012-07-20 投稿人: 浏览次数:134

这个是对上次博文修改和完善,在纪老师的要求上提高。

主要有八个页面

login.php 登陆页面

check.php  验证页面

mysql_connect.php 数据库连接页面

ms_login.php 从数据库中提取数据验证

register.php 注册页面

register_1.php 是注册验证,并写入到数据库中

showing.php  图片验证码

huangying.html  是登陆成功的页面


//=================================================

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>欢迎您的到来</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="check.php">
<div class="div">
 	用户名<input type="text" name="name" >
	密码:<input type="password" name="password">
	<div class="button">
	<input type="submit" value="提交">
	<input type="reset" value="清除">
	<a href="register.php" >注  册</a>
	
</div>
</div>

</form>


</body>
</html>

//================================================

check.php  验证页面

<?php
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="login.php">返回</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="login.php">返回</a>";

}
?>

//=========================================================

mysql_connect.php 数据库连接页面

<?php
// 连接服务器,并且选择test数据库
function(){
	
$db = mysql_connect("localhost","root","123") 
	or die("连接数据库失败!");

mysql_select_db("user")
	or die ("不能连接到user".mysql_error());
	
	
}
	
?>

//========================================================

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;
}

?>


===================================

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>欢迎您的到来</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="register_1.php">
<div class="div">
    <div class="text">
 	用户名<input type="text" name="name" ></div>
 	<div class="text">
	密码:<input type="password" name="password"></div>
	<div class="text">
	再次输入密码:<input type="password" name="pwd_again"></div>
	<div class="text">
	验证码:<input type="text" name="check"><img src="showimg.php"></img></div>
	
	<div class="text">
	<input type="radio" name="agree" value="是否同意我们的条款">同意我们的条款?</div>
	
	<input type="submit" value="提交">
	<input type="reset" value="清除">
	

</div>

</form>


</body>
</html>


//========================================================

register_1.php 是注册验证,并写入到数据库中

<?php
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"注册成功!";
    	}
    }
}
?>
//================================================
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);
?>

//=======================================

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>欢迎您</title>
</head>
<body>
<h1>欢迎您的到来!</h1>
</body>
</html>



声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。