无为清净楼资源网 Design By www.qnjia.com

验证码是网站常用的一项安全措施,也是新人站长较难掌握的一项技能,这里我向大家介绍一简单有效的验证码实现方法。

开始之前

在正式开始之前我们需要打开php的gd2图形库支持(在php.ini,中搜索“php_gd2.dll”,找到“;extension=php_gd2.dll”并去掉句首的分号) 。

可以参考:如何打开php的gd2库

核心:img.php

这个页面生成一张验证码并将正确数值写入 Session

随机一个4位验证码

$check=rand(1000,9999); 

将生成的验证码写入session

Session_start(); 
$_SESSION["check"] = $check;

创建一张图片

$im = imagecreate(80,30);

由于这种图片的背景默认是黑色的所以我们要用白色填充。

imagefill($im,0,0,ImageColorAllocate($im, 255,255,255)); 

使用imageline随机绘制两条实线

$y1=rand(0,30); 
$y2=rand(0,30); 
$y3=rand(0,30); 
$y4=rand(0,30); 
imageline($im,0,$y1,70, $y3,000); 
imageline($im,0,$y2,70, $y4,000);

在随机位置绘制文字

$strx=rand(3,15); 
$stry=rand(2,15); 
imagestring($img,5,$strx,$stry,substr($check,0,1),ImageColorAllocate($img,34,87,100)); 
$strx+=rand(15,20);
$stry=rand(2,15); 
imagestring($img,5,$strx,$stry,substr($check,1,1),ImageColorAllocate($img,781,117,78)); 
$strx+=rand(15,20);
$stry=rand(2,15); 
imagestring($img,5,$strx,$stry,substr($check,2,1),ImageColorAllocate($img,160,40,40)); 
$strx+=rand(15,20);
$stry=rand(2,15); 
imagestring($img,5,$strx,$stry,substr($check,3,1),ImageColorAllocate($img,25,55,10));

输出图像

Header("Content-type: image/PNG"); 
ImagePNG($img);

结束,下面是完整代码

<"check"] = $check; 
$img = imagecreate(80,30); 
imagefill($img,0,0,ImageColorAllocate($img,255,255,255)); 
$y1=rand(0,30); 
$y2=rand(0,30); 
$y3=rand(0,30); 
$y4=rand(0,30); 
imageline($img,0,$y1,70, $y3,ImageColorAllocate($img,55,255,25)); 
imageline($img,0,$y2,70, $y4,ImageColorAllocate($img,55,55,255)); 
$strx=rand(3,15); 
$stry=rand(2,15); 
imagestring($img,5,$strx,$stry,substr($check,0,1),ImageColorAllocate($img,34,87,100)); 
$strx+=rand(15,20);
$stry=rand(2,15); 
imagestring($img,5,$strx,$stry,substr($check,1,1),ImageColorAllocate($img,781,117,78)); 
$strx+=rand(15,20);
$stry=rand(2,15); 
imagestring($img,5,$strx,$stry,substr($check,2,1),ImageColorAllocate($img,160,40,40)); 
$strx+=rand(15,20);
$stry=rand(2,15); 
imagestring($img,5,$strx,$stry,substr($check,3,1),ImageColorAllocate($img,25,55,10)); 
Header("Content-type: image/PNG"); 
ImagePNG($img);

用户界面:index.php

想必大家都知道怎么做,我就直接给出代码了

 <!DOCTYPE html>
<html>
<body>
<form action="action.php" method="post">
<input type="text" name="cikle" placeholder="验证码">
<br>
<img id="cikle" style="-webkit-user-select: none" src="/UploadFiles/2021-04-02/img.php">

以上的代码将用户输入的数值传递到“action.php”中

检查:action.php

这一步要将用户输入数值与session中的数值进行比对

相等,输出“正确”

不相等,输出“不正确”

<"REQUEST_METHOD"] == "POST") {
 if($_SESSION["check"]!=intval($_POST["cikle"])){
 echo "不正确";
 }else{
 echo "正确";
 }
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!

标签:
php实现验证码,验证码功能实现

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?