使用php实现网站验证码功能【推荐】


Posted in PHP onFebruary 09, 2017

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

开始之前

在正式开始之前我们需要打开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);

结束,下面是完整代码

<?php $check=rand(1000,9999);
Session_start(); 
$_SESSION["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="img.php"><input type="submit" value="Submit">
</form> 
</body>
</html>

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

检查:action.php

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

相等,输出“正确”

不相等,输出“不正确”

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

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

PHP 相关文章推荐
php学习笔记 php中面向对象三大特性之一[封装性]的应用
Jun 13 PHP
php数据结构与算法(PHP描述) 查找与二分法查找
Jun 21 PHP
PHP实现提取一个图像文件并在浏览器上显示的代码
Oct 06 PHP
php使用base64加密解密图片示例分享
Jan 20 PHP
php实现的替换敏感字符串类实例
Sep 22 PHP
PHP中的静态变量及static静态变量使用详解
Nov 05 PHP
php+jQuery+Ajax实现点赞效果的方法(附源码下载)
Jul 21 PHP
浅析Yii2 GridView 日期格式化并实现日期可搜索教程
Apr 22 PHP
Thinkphp 空操作、空控制器、命名空间(详解)
May 05 PHP
PHP 中魔术常量的实例详解
Oct 26 PHP
php设计模式之备忘模式分析【星际争霸游戏案例】
Mar 24 PHP
PHP实现基本留言板功能原理与步骤详解
Mar 26 PHP
form表单传递数组数据、php脚本接收的实例
Feb 09 #PHP
PHP设置Cookie的HTTPONLY属性方法
Feb 09 #PHP
PHP调试及性能分析工具Xdebug详解
Feb 09 #PHP
php从身份证获取性别和出生年月
Feb 09 #PHP
Yii2框架实现数据库常用操作总结
Feb 08 #PHP
Yii2实现中国省市区三级联动实例
Feb 08 #PHP
PHP+Ajax无刷新带进度条图片上传示例
Feb 08 #PHP
You might like
Thinkphp使用mongodb数据库实现多条件查询方法
2014/06/26 PHP
CodeIgniter采用config控制的多语言实现根据浏览器语言自动转换功能
2014/07/18 PHP
php处理带有中文URL的方法
2016/07/11 PHP
PHP实现的统计数据功能详解
2016/12/06 PHP
学习ExtJS TextField常用方法
2009/10/07 Javascript
js调试工具Console命令详解
2014/10/21 Javascript
JavaScript实现同步于本地时间的动态时间显示方法
2015/02/02 Javascript
JavaScript中的lastIndexOf()方法使用详解
2015/06/06 Javascript
jQuery实现根据滚动条位置加载相应内容功能
2016/07/18 Javascript
浅谈JavaScript 中有关时间对象的方法
2016/08/15 Javascript
JavaScript数据结构之二叉树的查找算法示例
2017/04/13 Javascript
angularjs 的数据绑定实现原理
2018/07/02 Javascript
JavaScript迭代器的含义及用法
2019/06/21 Javascript
微信sdk实现禁止微信分享(使用原生php实现)
2019/11/15 Javascript
详解element上传组件before-remove钩子问题解决
2020/04/08 Javascript
微信h5静默和非静默授权获取用户openId的方法和步骤
2020/06/08 Javascript
jQuery实现简单评论区功能
2020/10/26 jQuery
python中getattr函数使用方法 getattr实现工厂模式
2014/01/20 Python
python 设置文件编码格式的实现方法
2017/12/21 Python
浅谈Python中的zip()与*zip()函数详解
2018/02/24 Python
Python实现简单求解给定整数的质因数算法示例
2018/03/25 Python
对python使用http、https代理的实例讲解
2018/05/07 Python
Python解析Excle文件中的数据方法
2018/10/23 Python
django使用F方法更新一个对象多个对象字段的实现
2020/03/28 Python
深入了解Python 变量作用域
2020/07/24 Python
澳大利亚冒险体验:Adrenaline(跳伞、V8赛车、热气球等)
2017/09/18 全球购物
Origins悦木之源英国官网:雅诗兰黛集团高端植物护肤品牌
2017/11/06 全球购物
乌克兰香水和化妆品网站:Notino.ua
2018/03/26 全球购物
不同浏览器创建XMLHttpRequest方法有什么不同
2014/11/17 面试题
应用心理学个人求职信范文
2013/12/11 职场文书
企业文明单位申报材料
2014/05/16 职场文书
工作求职信
2014/07/04 职场文书
入党积极分子学习党的纲领思想汇报
2014/09/13 职场文书
私人贷款担保书该怎么写呢?
2019/07/02 职场文书
使用nginx配置访问wgcloud的方法
2021/06/26 Servers
「魔法少女伊莉雅」美游粘土人开订
2022/03/21 日漫