全方位站长技能、SEO优化学习平台
当前位置:网站首页 > 站长笔记 > 正文

PHP如何完成验证码功能?

作者:admin发布时间:2023-11-17分类:站长笔记浏览:162


温馨提示:手机扫码可阅读当前文章!
文章简介:在使用php开发程序的时候,特别是在用户注册登录的页面,如果不设置验证码功能,很有可能被人利用工具,批量注册账号或者暴力破解用户账号信息,对网站或者用户造成损失。那么php如何完成验证码功能呢?一、验证码的生成我们可以写一个yzm.php文...

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


PHP如何完成验证码功能?  第1张

一、验证码的生成


我们可以写一个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编程知识和建站经验。

欢迎您,来自美国的朋友,您的IP:18.119.163.238,您的网络:麻省理工学院


站长笔记排行
随机推荐
猜你喜欢

服务热线

1888888888

要发发发发发发

站长微信公众号

站长微信公众号

分享:

支付宝

微信