php验证码实现

在工作之余呢,想着为大家分享一些小小的实例,为人为己,共同学习,一起进步!好了,太多的煽情的话也不会说,呵呵,直接开始吧!


以下是原生php代码实现:


verity.php

<?php  
header("content-type:text/html;charset=utf-8");  
  
//开启session  
session_start();  
  
//准备画布  
$im=imagecreatetruecolor(50,25);  
  
//准备涂料  
$black=imagecolorallocate($im,0,0,0);  
$gray=imagecolorallocate($im,200,200,200);  
  
//背景填充  
imagefill($im,0,0,$gray);  
  
//文字居中  
$x=(50-10*4)/2;  
$y=(25-5)/2+5;  
  
//准备文字  
$arr=array_merge(range(0,9),range('a','z'),range('A','Z'));  
shuffle($arr);  
$str=implode(array_slice($arr,0,4));  
  
//把$str放入session中,方便所有页面中调用  
$_SESSION['vstr']=$str;  
  
$file="fonts/simsun.ttc";  
imagettftext($im,10,0,$x,$y,$black,$file,$str);  
  
  
//输出到浏览器上或保存起来  
header("content-type:image/png");  
imagepng($im);  
  
//关闭画布  
imagedestory($im);  
?>  

form.html

<html>  
 <head>  
    <meta charset="utf-8">  
    <title>登录</title>  
    <style>  
        table{  
              
            border-collapse:collapse;  
        }  
        img{  
            margin-top:5px;  
        }  
    </style>  
 </head>  
  
 <body>  
    <h2>用户登录页面</h2>  
    <hr>  
    <table width="500px" border="1px">  
        <form action="submit.php" method="post">  
            <tr>  
            <td>姓    名:</td>  
            <td><input type="text" name="username" id=""></td>  
            </tr>  
            <tr>  
            <td>密    码:</td>  
            <td><input type="password" name="password" id=""></td>  
            </tr>  
            <tr>  
            <td>验证码:</td>  
            <td><input type="text" name="vcode">   
            <img src="verity.php"></td>  
            </tr>  
            <tr>  
            <td><input type="submit" value="提交"></td>  
            <td><input type="reset" value="重置"></td>  
            </tr>  
        </form>  
    </table>  
 </body>  
</html>  


submit.php

<?php  
header("content-type:text/html;charset=utf-8");  
session_start();  
$code=strtolower($_POST['vcode']);  
$str=strtolower($_SESSION['vstr']);  
      
if($code==$str){  
    //页面跳转  
    echo "<script>location='http://www.baidu.com'</script>";  
}else{  
    echo "<script>alert('验证码输入错误');</script>";  
    echo "<a href='form.html'>返回登录页面</a>";  
          
}  
?>


至此,验证码已经完美生成,hello world!  hello php!  so easy!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
验证码可以用 PHP GD 库来生成图像,然后把图像输出到浏览器上。以下是一个简单的 PHP 验证码实现: ```php <?php // 开始会话 session_start(); // 验证码中使用的字符集 $charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; // 创建一个随机字符串 $code = ''; for ($i = 0; $i < 4; $i++) { $code .= $charset[mt_rand(0, strlen($charset) - 1)]; } // 将随机字符串存储到会话中 $_SESSION['code'] = $code; // 创建一个 100x30 像素的图像 $image = imagecreatetruecolor(100, 30); // 设置背景色为白色 $bg_color = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $bg_color); // 生成干扰线 for ($i = 0; $i < 5; $i++) { $line_color = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imageline($image, mt_rand(0, 100), mt_rand(0, 30), mt_rand(0, 100), mt_rand(0, 30), $line_color); } // 生成干扰点 for ($i = 0; $i < 50; $i++) { $pixel_color = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imagesetpixel($image, mt_rand(0, 100), mt_rand(0, 30), $pixel_color); } // 将随机字符串写入图像 $text_color = imagecolorallocate($image, 0, 0, 0); imagestring($image, 5, 20, 8, $code, $text_color); // 发送图像到浏览器 header('Content-type: image/png'); imagepng($image); // 销毁图像 imagedestroy($image); ?> ``` 在 HTML 表单中使用验证码时,需要将用户输入的验证码和会话中存储的验证码进行比较,以验证用户输入的是否正确。比较的代码如下: ```php <?php // 开始会话 session_start(); if ($_POST['code'] == $_SESSION['code']) { // 验证码输入正确 } else { // 验证码输入错误 } ?> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值