使用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 相关文章推荐
一个简单计数器的源代码
Oct 09 PHP
又一个php 分页类实现代码
Dec 03 PHP
PHP url 加密解密函数代码
Aug 26 PHP
PHP删除数组中的特定元素的代码
Jun 28 PHP
php将textarea数据提交到mysql出现很多空格的解决方法
Dec 19 PHP
两种php去除二维数组的重复项方法
Nov 04 PHP
ThinkPHP中limit()使用方法详解
Apr 19 PHP
Laravel中encrypt和decrypt的实现方法
Sep 24 PHP
PHP区块查询实现方法分析
May 12 PHP
PHP常见的几种攻击方式实例小结
Apr 29 PHP
PHP 构造函数和析构函数原理与用法分析
Apr 21 PHP
详解PHP设计模式之依赖注入模式
May 25 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
php检索或者复制远程文件的方法
2015/03/13 PHP
PHP+JS三级菜单联动菜单实现方法
2016/02/24 PHP
ThinkPHP+EasyUI之ComboTree中的会计科目树形菜单实现方法
2017/06/09 PHP
JS去除右边逗号的简单方法
2013/07/03 Javascript
浅析JavaScript中的隐式类型转换
2013/12/05 Javascript
jQuery中slideUp()方法用法分析
2014/12/24 Javascript
js+HTML5实现视频截图的方法
2015/06/16 Javascript
js判断子窗体是否关闭的方法
2015/08/11 Javascript
微信支付如何实现内置浏览器的H5页面支付
2015/09/25 Javascript
基于Javascript实现文件实时加载进度的方法
2016/10/12 Javascript
js 实现一些跨浏览器的事件方法详解及实例
2016/10/27 Javascript
js实现省份下拉菜单效果
2017/02/15 Javascript
jQuery插件FusionWidgets实现的Bulb图效果示例【附demo源码下载】
2017/03/23 jQuery
微信小程序实现星级评分和展示
2018/07/05 Javascript
vue.js使用v-model实现表单元素(input) 双向数据绑定功能示例
2019/03/08 Javascript
原生JS使用Canvas实现拖拽式绘图功能
2019/06/05 Javascript
Node.js HTTP服务器中的文件、图片上传的方法
2019/09/23 Javascript
Vue 实例中使用$refs的注意事项
2021/01/29 Vue.js
[02:04]完美世界城市挑战赛秋季赛报名开始 谁是solo路人王?
2019/10/10 DOTA
python中实现定制类的特殊方法总结
2014/09/28 Python
Python中分数的相关使用教程
2015/03/30 Python
Python实现基本数据结构中栈的操作示例
2017/12/04 Python
浅谈numpy库的常用基本操作方法
2018/01/09 Python
Python 数据处理库 pandas 入门教程基本操作
2018/04/19 Python
pandas使用get_dummies进行one-hot编码的方法
2018/07/10 Python
python 中xpath爬虫实例详解
2019/08/26 Python
Python可变参数会自动填充前面的默认同名参数实例
2019/11/18 Python
Python实现从N个数中找到最大的K个数
2020/04/02 Python
基于python实现计算两组数据P值
2020/07/10 Python
《争吵》教学反思
2014/02/15 职场文书
2014乡镇“三八”国际劳动妇女节活动总结
2014/03/01 职场文书
入党积极分子十八届四中全会思想汇报
2014/10/23 职场文书
行政申诉状范文
2015/05/20 职场文书
学校体育节班级口号
2015/12/25 职场文书
Python趣味实战之手把手教你实现举牌小人生成器
2021/06/07 Python
详解Flask开发技巧之异常处理
2021/06/15 Python