PHP编写登录验证码功能 附调用方法


Posted in PHP onMay 19, 2016

本文实例为大家分享了一个PHP写的登录验证码功能,供大家参考,具体内容如下

 ShowKey.php

<?php
session_start();
//设置COOKIE或Session
function esetcookie($name,$str,$life=0){
//本函数将字符串 str 全部变小写字符串使验证码输入不区分大小写----在提交表单进行session比较同样需要次函数转化
 $_SESSION[$name]=strtolower($str);
}

//获取随机字符 此函数区分字符大小写 如果不区分大小写可加入函数strtolower
function domake_password($len) 
{ 
  $chars = array( 
    /*"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", 
    "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", 
    "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", 
    "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", 
    "S", "T", "U", "V", "W", "X", "Y", "Z",*/ "0", "1", "2", 
    "3", "4", "5", "6", "7", "8", "9" 
  ); 
  $charsLen = count($chars) - 1; 
  shuffle($chars);// 将数组打乱
  $output = ""; 
  for ($i=0; $i<$len; $i++) 
  { 
    $output .= $chars[mt_rand(0, $charsLen)]; //获得一个数组元素
  } 
  return $output;
} 

//显示验证码
function ShowKey(){
 $key=domake_password(4);//获取随机值
 $set=esetcookie("checkkey",$key);//将随机值写入cookie或session
 //是否支持gd库
 if(function_exists("imagejpeg")) 
 {
  header ("Content-type: image/jpeg");
  $img=imagecreate(47,20);
  $blue=imagecolorallocate($img,102,102,102);
  $white=ImageColorAllocate($img,255,255,255);
  $black=ImageColorAllocate($img,71,71,71);
  imagefill($img,0,0,$blue);
  imagestring($img,5,6,3,$key,$white);
  for($i=0;$i<90;$i++) //加入干扰象素
  {
   imagesetpixel($img,rand()%70,rand()%30,$black);
  }
  imagejpeg($img);
  imagedestroy($img);
 }
 elseif (function_exists("imagepng"))
 {
  header ("Content-type: image/png");
  $img=imagecreate(47,20);
  $blue=imagecolorallocate($img,102,102,102);
  $white=ImageColorAllocate($img,255,255,255);
  $black=ImageColorAllocate($img,71,71,71);
  imagefill($img,0,0,$blue);
  imagestring($img,5,6,3,$key,$white);
  for($i=0;$i<90;$i++) //加入干扰象素
  {
   imagesetpixel($img,rand()%70,rand()%30,$black);
  }
  imagepng($img);
  imagedestroy($img);
 }
 elseif (function_exists("imagegif")) 
 {
  header("Content-type: image/gif");
  $img=imagecreate(47,20);
  $blue=imagecolorallocate($img,102,102,102);
  $white=ImageColorAllocate($img,255,255,255);
  $black=ImageColorAllocate($img,71,71,71);
  imagefill($img,0,0,$blue);
  imagestring($img,5,6,3,$key,$white);
  for($i=0;$i<90;$i++) //加入干扰象素
  {
   imagesetpixel($img,rand()%70,rand()%30,$black);
  }
  imagegif($img);
  imagedestroy($img);
 }
 elseif (function_exists("imagewbmp")) 
 {
  header ("Content-type: image/vnd.wap.wbmp");
  $img=imagecreate(47,20);
  $blue=imagecolorallocate($img,102,102,102);
  $white=ImageColorAllocate($img,255,255,255);
  $black=ImageColorAllocate($img,71,71,71);
  imagefill($img,0,0,$blue);
  imagestring($img,5,6,3,$key,$white);
  for($i=0;$i<90;$i++) //加入干扰象素
  {
   imagesetpixel($img,rand()%70,rand()%30,$black);
  }
  imagewbmp($img);
  imagedestroy($img);
 }
 else
 {
  //不支持验证码
  header("content-type:image/jpeg\r\n");
  header("Pragma:no-cache\r\n");
  header("Cache-Control:no-cache\r\n");
  header("Expires:0\r\n");
  $fp = fopen("data/vdcode.jpg","r"); 
 }
}
ShowKey();
?>

调用方法:

<img src="ShowKey.php" name="KeyImg" id="KeyImg"  onClick="KeyImg.src='ShowKey.php?'+Math.random()"> 

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

PHP 相关文章推荐
别人整理的服务器变量:$_SERVER
Oct 20 PHP
全世界最小的php网页木马一枚 附PHP木马的防范方法
Oct 09 PHP
PHP 抓取网页图片并且另存为的实现代码
Mar 24 PHP
php检测网页是否被百度收录的函数代码
Oct 09 PHP
php ci框架中加载css和js文件失败的解决方法
Mar 03 PHP
PHP中实现接收多个name相同但Value不相同表单数据实例
Feb 03 PHP
PHP编程求最大公约数与最小公倍数的方法示例
May 29 PHP
PHP基于curl post实现发送url及相关中文乱码问题解决方法
Nov 25 PHP
PHP 数组黑名单/白名单实例代码详解
Jun 04 PHP
PHP中的自动加载操作实现方法详解
Aug 06 PHP
laravel 时间格式转时间戳的例子
Oct 11 PHP
php实现商城购物车的思路和源码分析
Jul 23 PHP
一段实用的php验证码函数
May 19 #PHP
thinkphp3.x中cookie方法的用法分析
May 19 #PHP
thinkphp3.x中display方法及show方法的用法实例
May 19 #PHP
thinkphp3.x连接mysql数据库的方法(具体操作步骤)
May 19 #PHP
thinkphp3.x自定义Action、Model及View的简单实现方法
May 19 #PHP
thinkPHP实现递归循环栏目并按照树形结构无限极输出的方法
May 19 #PHP
php处理json格式数据经典案例总结
May 19 #PHP
You might like
PHP中include()与require()的区别说明
2010/03/10 PHP
PHP pathinfo()获得文件的路径、名称等信息说明
2011/09/13 PHP
php无限极分类递归排序实现方法
2014/11/11 PHP
php在apache环境下实现gzip配置方法
2015/04/02 PHP
PHP 计算两个特别大的整数实例代码
2018/05/07 PHP
PHP7创建COOKIE和销毁COOKIE的实例方法
2020/02/03 PHP
一个tab标签切换效果代码
2009/03/27 Javascript
JavaScript 输入框内容格式验证代码
2010/02/11 Javascript
Jquery中Ajax 缓存带来的影响的解决方法
2011/05/19 Javascript
JavaScript mapreduce工作原理简析
2012/11/25 Javascript
JavaScript学习笔记之定时器
2015/01/22 Javascript
javascript判断并获取注册表中可信任站点的方法
2015/06/01 Javascript
jQuery实现字体颜色渐变效果的方法
2017/03/29 jQuery
javascript 中null和undefined区分和比较
2017/04/19 Javascript
vue router demo详解
2017/10/13 Javascript
Angular ng-animate和ng-cookies用法详解
2018/04/18 Javascript
小程序开发中如何使用async-await并封装公共异步请求的方法
2019/01/20 Javascript
对Layer弹窗使用及返回数据接收的实例详解
2019/09/26 Javascript
vue父组件给子组件的组件传值provide inject的方法
2019/10/23 Javascript
JavaScript实现横版菜单栏
2020/03/17 Javascript
[01:11:35]Liquid vs LGD 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
[05:59]带你看看DPC的台前幕后
2021/03/11 DOTA
Python多叉树的构造及取出节点数据(treelib)的方法
2019/08/09 Python
python中sort和sorted排序的实例方法
2019/08/26 Python
使用Python的turtle模块画国旗
2019/09/24 Python
Django文件上传与下载(FileFlid)
2019/10/06 Python
Python利用FFT进行简单滤波的实现
2020/02/26 Python
Python3 pyecharts生成Html文件柱状图及折线图代码实例
2020/09/29 Python
Jupyter安装链接aconda实现过程图解
2020/11/02 Python
英国最大的美妆产品在线零售商之一:Beauty Bay
2017/09/29 全球购物
法国低价在线宠物商店:bitiba.fr
2020/07/03 全球购物
党的群众路线教育实践活动个人对照检查材料(公安)
2014/11/05 职场文书
2014年学校后勤工作总结
2014/12/06 职场文书
铁人观后感
2015/06/16 职场文书
同学聚会致辞集锦
2015/07/28 职场文书
Java日常练习题,每天进步一点点(38)
2021/07/26 Java/Android