登录  /  注册

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

高洛峰
发布: 2017-02-03 17:51:01
原创
1529人浏览过

本文实例为大家分享了一个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=&#39;ShowKey.php?&#39;+Math.random()" alt="PHP编写登录验证码功能 附调用方法" >
登录后复制

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

更多PHP编写登录验证码功能 附调用方法相关文章请关注PHP中文网!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号