当前位置: 代码网 > it编程>编程语言>Php > 详解PHP如何完成验证码功能示例

详解PHP如何完成验证码功能示例

2024年05月15日 Php 我要评论
引言在使用php开发程序的时候,特别是在用户注册登录的页面,如果不设置验证码功能,很有可能被人利用工具,批量注册账号或者暴力破解用户账号信息,对网站或者用户造成损失。那么php如何完成验证码功能呢?一

引言

在使用php开发程序的时候,特别是在用户注册登录的页面,如果不设置验证码功能,很有可能被人利用工具,批量注册账号或者暴力破解用户账号信息,对网站或者用户造成损失。那么php如何完成验证码功能呢?

一、验证码的生成

我们可以写一个yzm.php文件,用来生成动态的验证码图片。代码如下:

<?php
session_start();
// 生成随机验证码
$charset = '0123456789abcdefghijklmnpqrstuvwxyzabcvbnmasdfghjkpiuytrewq';
$randomstring = '';
$length = 6; // 验证码长度
$charsetlength = strlen($charset) - 1;
for ($i = 0; $i < $length; $i++) {
    $randomstring .= $charset[random_int(0, $charsetlength)];
}
// 保存验证码到session中
$_session['captcha'] = $randomstring;
// 创建验证码图片
$image = imagecreatetruecolor(120, 40);
$bgcolor = imagecolorallocate($image, 255, 255, 255);
$textcolor = imagecolorallocate($image, 0, 0, 0);
// 填充背景色
imagefilledrectangle($image, 0, 0, 120, 40, $bgcolor);
// 在图片上绘制验证码
imagestring($image, 5, 40, 10, $randomstring, $textcolor);
// 发送图像头部到浏览器
header('content-type: image/png');
// 输出图像到浏览器
imagepng($image);
// 销毁图像资源
imagedestroy($image);
?>

上面的代码可以从“0123456789abcdefghijklmnpqrstuvwxyzabcvbnmasdfghjkpiuytrewq”这一串字符串中随机选择6个字符作为验证码,并命名为$_session['captcha']存入服务器session中,方便后续进行验证。当然生成的验证码图片你可以进行更多的装饰,这里就不赘述了。

二、验证码的调用

我们往往在注用户册或者登录的页面需要告诉用户验证码是多少,让用户准确输入验证码。

 <div id="popup" class="popup">
    <div class="popup-inner">
      <center><h3>用户注册</h3></center>
      <span class="button-close" onclick="closepopup()">&times;</span>
      <form id="joinform" action="sub.php" method="post">
        <div class="form-group">
          <label for="username">账号:</label>
          <input type="text" id="username" name="username" placeholder="请输入账号" required>
        </div>
        <div class="form-group">
          <label for="userpass">密码:</label>
          <input type="pass" id="userpass" name="userpass" placeholder="请输密码" required>
        </div>
        <div class="form-group">
          <label for="captcha">验证码:<img class="ue-image" src="yzm.php"/></label>
          <input type="text" id="captcha" name="captcha" placeholder="请输入验证码" required>
        </div>
        <div class="form-group">
          <div class="button-group">
            <button type="submit">提交</button>
          </div>
        </div>
      </form>
    </div>
  </div>

可以看到,在注册页面,我们直接使用以下代码展示生成的验证码:

<img class="ue-image" src="yzm.php"/>

当然你也可以直接调用$_session['captcha']来展示验证码,但是为了验证功能的有效性和安全性,这里不建议直接调用$_session['captcha']函数。

三、验证码的验证

当用户输入验证码以后,我们需要对验证码进行验证,判断用户是否准确输入了验证码。如果用户未能准确输入验证码,则php文件不再继续执行后面的代码。

<?php
session_start();//首先开启session
if(!empty($_post)){
  $username = $_post['username'];
  $userpass = $_post['userpass'];
  $captcha = $_post['captcha'];
if (!preg_match('/^[a-za-z0-9]+$/', $captcha)) {
echo '<script>alert("验证码不正确!"); window.location.href = "index.php";</script>';exit;    
  }//这里使用正则判断验证码的合法性,如果验证码未按0-9和a-z的规则输入则提示验证码不正确并返回首页。
  if($captcha!=$_session['captcha']){
echo '<script>alert("验证码不正确!"); window.location.href = "index.php";</script>';exit;      
}//这里判断用户输入的验证码是否与yzm.php生成的验证码一直 ,如果不一致则提示验证码不正确并返回首页。
//如果验证码正确,则继续执行下面的代码
.......
//
}
?>

当然本文只是简单介绍php如何设计验证码功能,实际开发中可能需要更完善更丰富的功能,需要对以上代码进行完善和修改。

php编程语言是一款十分简单容易上手的编程语言,非常适合新手学习。

以上就是php如何完成验证码功能的详细内容,更多关于php如何完成验证码功能的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com