PHP如何完成验证码功能?

本文详细介绍了如何在PHP开发中实现和验证用户注册登录页面的验证码功能,包括验证码的生成、在页面上的显示以及验证过程,以提升网站安全性。
摘要由CSDN通过智能技术生成

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


一、验证码的生成

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

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中,方便后续进行验证。当然生成的验证码图片你可以进行更多的装饰,这里就不赘述了。

二、验证码的调用

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

PHP代码:

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

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

PHP代码:

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

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

三、验证码的验证

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

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编程知识和建站经验。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

文煞

请你告诉我如何描述此刻的感激!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值