(二)简单的登陆注册系统--增加验证码部分

login.html

<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<h1>登陆系统</h1>
<form action="login1.php" method="post">
用户名:<input type="text"  name="username"/>
<br/>
密&nbsp;&nbsp;码:<input type="password"  name="password"/>
<br/>
<input type="submit" value="登陆" />
<input type="button" value="注册" onclick="window.location.href='register.php'"/>
</form>
</html>

login1.php

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<?php
//session_start();
$conn=mysql_connect("localhost","root","root");
if(!$conn) {
 echo "connect fail".mysql_error();
}
$db=mysql_select_db("login",$conn);
if(!$db)
 echo "select database fail!";
mysql_query("set names gb2312");
$name=$_POST['username'];
$pwd=$_POST['password'];
if($name&&$pwd){
  $sql="select * from login_info where username='$name' and password=md5('$pwd')";
  $result=mysql_query($sql);
  $rows=mysql_fetch_array($result);
  if($rows){
   echo "<script>";
   echo "alert('登陆成功,页面将跳转到百度');";
   echo "location.href='http://www.baidu.com';";
   echo "</script>";
  }
     else{
   echo "<script>";
   echo "alert('用户名或密码有错误!');";
   echo "location.href='login.html';";
   echo "</script>";
     }
}
else if($name==''||$pwd==''){   
  echo "<script>";
  echo "alert('用户名或密码不能为空!');";
  echo "location.href='login.html';";
  echo "</script>";
}
mysql_close($conn);

register.php

<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head></head>
<h2>这里是注册页面</h2>
<script type="text/javascript">
function create_code(){
 document.getElementById('code').src='verification.php?n='+Math.random()*10000;
}
</script>
</head>
<form action="check_form.php" method="post">
<table>
<tr><td><B>新用户注册</B></td></tr>
<tr><td>用户名:</td><td><input type="text"  name="username" required="required"/></td></tr>
<tr><td>密码:</td><td><input type="password"  name="password1" required="required"/></td></tr>
<tr><td>确认密码:</td><td><input type="password"  name="password2" required="required"/></td></tr>
<tr><td>性别:</td><td><input type="text"  name="sex" placeholder="选填项"/></td></tr>
<tr><td>验证码:</td><td><input  type="text" maxlength="4" name="yzm_code1" id="srand" required="required"/></td></tr>
<tr><td></td><td><img id="code" name="code" src="verification.php" onclick="create_code()" title="点击图片刷新验证码" ></td></tr>
<tr><td><input type="submit" value="注册"></td>
<td><input type="reset" value="重置"/></td></tr>
</table>
</form>
</html>

check_form.php

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<?php
header("Content-Type:text/html;charset=utf-8");
session_start();
if($_SESSION["yzm_code2"]!=$_POST["yzm_code1"]){
 echo "<script>";
 echo "alert('验证码输入错误,请重新输入');";
 echo "location.href='register.php';";
 echo "</script>";
}else {
 $conn=mysql_connect("localhost","root","root");
 if(!$conn) {
  echo "connect fail".mysql_error();
 }
 $db=mysql_select_db("login",$conn);
 if(!$db)
  echo "select database fail!";
 $name_check=$_POST['username'];
 $password_check1=$_POST['password1'];
 $password_check2=$_POST['password2'];
 $sex_check=$_POST['sex'];
 if ($password_check1!=$password_check2){
  echo "<script>";
  echo "alert('两次输入的密码不一致');";
  echo "location.href='register.php';";
  echo "</script>";
 }
 
  else {
  $sql="select * from login_info where username='$name_check'";
  $result=mysql_query($sql,$conn);
  if(mysql_num_rows($result)>0){
   echo "<script>";
   echo "alert('用户名已经存在,请重新输入!');";
   echo "location.href='register.php';";
   echo "</script>";
  }
  
  else {  
  echo "<script>";
  echo "alert('注册成功!');";
  echo "location.href='login.html';";
  echo "</script>";
  $sqlinsert="insert into login_info (username,password,sex) value ('$name_check',md5('$password_check1'),'$sex_check')";
  mysql_query($sqlinsert);
  }
    
 }
}
mysql_close($conn);

verification.php

<?php 
session_start(); //在把用户信息存储到 PHP session 中之前,首先必须启动会话。session_start()就是启动session 的意思
get_code(4,60,20);//调用函数生成四个随机数
function get_code($num,$w,$h){ //这里把$w改成$weight,$h改成$height就出错了,不知道为什么,可能是因为后两个变量不能随便用吧
 $code="";
 for($i=0;$i<$num;$i++){ //循环拼接验证码字符串,其中$num是验证码的个数
  $code.=rand(0,9);
 }
 $_SESSION["yzm_code2"]=$code;  //将生成的验证码写入session,备验证时用
 header("Content-type:image/PNG");  //定义图片头部
 $im=imagecreate($w, $h);//创建图片,定义颜色值
 $black=imagecolorallocate($im, 0, 153, 51);  //边框色
 $gray=imagecolorallocate($im, 200, 200, 200); //填充背景色
 $bgcolor=imagecolorallocate($im,255, 255, 255);
 imagefill($im, 0, 0, $gray);  //填充背景
 imagerectangle($im, 0, 0, $w-1, $h-1, $black); //画边框
 $style=array($black,$black,$black,$black,$black,$gray,$gray,$gray,$gray,$gray);//随机绘制两条虚线,起干扰作用
 imagesetstyle($im,$style);  
 $y1=rand(0,$h);
 $y2=rand(0,$h);
 $y3=rand(0,$h);
 $y4=rand(0,$h);
 imageline($im, 0, $y1, $w, $y3, IMG_COLOR_STYLED);
 imageline($im, 0, $y2, $w, $y4, IMG_COLOR_STYLED);
 for ($i = 0; $i<80; $i++) { //在画布上随机生成大量黑点,起干扰作用;
  imagesetpixel($im, rand(0, $w), rand(0, $h), $black); //将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成
 }
 $strx = rand(3, 8);
 for ($i = 0; $i<$num; $i++) {
  $strpos = rand(1, 6);
  imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black);
  $strx += rand(8, 12);
 }
 imagepng($im); //输出图片
 imagedestroy($im); //释放图片所占内存
}

 参考例子:http://www.tuicool.com/articles/6ruYbq

                 http://jingyan.baidu.com/article/495ba841099a5338b30edeec.html

转载于:https://my.oschina.net/zhangxuman/blog/505985

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值