使用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配置文件中最常用四个ini函数
Mar 19 PHP
关于Intype一些小问题的解决办法
Mar 28 PHP
自己在做项目过程中学到的PHP知识收集
Aug 20 PHP
PHP 读取Postgresql中的数组
Apr 14 PHP
PHP时间戳与日期之间转换的实例介绍
Apr 19 PHP
php数组中删除元素之重新索引的方法
Sep 16 PHP
php通过curl模拟登陆DZ论坛
May 11 PHP
PHP中创建和验证哈希的简单方法实探
Jul 06 PHP
php获取网站百度快照日期的方法
Jul 29 PHP
详细解读PHP的Yii框架中登陆功能的实现
Aug 21 PHP
thinkphp5.0整合phpsocketio完整攻略(绕坑)
Oct 12 PHP
thinkphp5框架实现数据库读取的数据转换成json格式示例
Oct 10 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 fckeditor 调用的函数
2009/06/21 PHP
PHP中用hash实现的数组
2011/07/17 PHP
php登陆页的密码处理方式分享
2013/10/14 PHP
ThinkPHP自动转义存储富文本编辑器内容导致读取出错的解决方法
2014/08/08 PHP
PHP读取XML格式文件的方法总结
2017/02/27 PHP
Javascript 页面模板化很多人没有使用过的方法
2012/06/05 Javascript
15条JavaScript最佳实践小结
2013/08/09 Javascript
整理的比较全的event对像在ie与firefox浏览器中的区别
2013/11/25 Javascript
解决jquery操作checkbox火狐下第二次无法勾选问题
2014/02/10 Javascript
jquery获取多个checkbox的值异步提交给php
2015/07/07 Javascript
JavaScript制作颜色反转小游戏
2016/09/25 Javascript
详解如何在项目中使用jest测试react native组件
2018/02/09 Javascript
Javasript设计模式之链式调用详解
2018/04/26 Javascript
详解Angular操作cookies方法
2018/06/01 Javascript
AngularJs1.x自定义指令独立作用域的函数传入参数方法
2018/10/09 Javascript
Vue中使用方法、计算属性或观察者的方法实例详解
2018/10/31 Javascript
详解微信小程序开发聊天室—实时聊天,支持图片预览
2019/05/20 Javascript
node.js处理前端提交的GET请求
2019/08/30 Javascript
JavaScript 防盗链的原理以及破解方法
2020/12/29 Javascript
Python使用smtplib模块发送电子邮件的流程详解
2016/06/27 Python
Django入门使用示例
2017/12/12 Python
Ubuntu18.04下python版本完美切换的解决方法
2019/06/14 Python
python多进程并行代码实例
2019/09/30 Python
详解从Django Allauth中进行登录改造小结
2019/12/18 Python
python小程序之4名牌手洗牌发牌问题解析
2020/05/15 Python
解决pip安装tensorflow中出现的no module named tensorflow.python 问题方法
2021/02/20 Python
德国家用电器购物网站:Premiumshop24
2019/08/22 全球购物
SQL Server的固定数据库角色都有哪些?对应的服务器权限有哪些?
2013/05/18 面试题
感恩节活动方案
2014/01/27 职场文书
购房意向书
2014/04/01 职场文书
植树节标语
2014/06/27 职场文书
永不妥协观后感
2015/06/10 职场文书
环境保护宣传标语大全!
2019/06/28 职场文书
话题作文之诚信
2019/11/28 职场文书
SpringBoot生成License的实现示例
2021/06/16 Java/Android
在ubuntu下安装go开发环境的全过程
2022/08/05 Golang