搜索
首页后端开发php教程php session验证码不一致总显示上次验证码的解决方法

  1. $(".login").live('click',function(){

  2. var username=$(".input_user").val();
  3. var password=$(".input_ps").val();
  4. var code=$('.input_checkcode').val();
  5. if(username==""){
  6. alert("用户名不能为空");
  7. return false;
  8. }
  9. if(password==""){
  10. alert("密码不能为空");
  11. return false;
  12. }
  13. var URL="checkLogin.php?";

  14. var DATA="&username="+username+"&password="+password+"&code="+code;
  15. $.getJSON(URL+DATA,function(json){
  16. if(json.code=='code_error'){
  17. alert('验证码错误,请重新输入验证码');
  18. }
  19. if(json.username=='true_u'&&json.password=='true_p'){
  20. //alert(json.username+"|"+username+'...1');
  21. window.location="index.php";
  22. }
  23. if(json.username=='error_u'||json.password=='error_p'){
  24. alert("用户名输入或密码输入有误,请检查后重新登陆!");
  25. window.location="login.php";
  26. }
  27. });
  28. });
复制代码

后台checkLogin.php关键代码:

  1. $code=$_GET['code'];
  2. if($code!=$_SESSION['captchaCode']['content'])
  3. {$adminInfo['code']='code_error';};
  4. if($row['username']==$username&&$row['password']==$password){
  5. $_SESSION['username']=$row['username'];
  6. $adminInfo['username']='true_u';
  7. $adminInfo['password']='true_p';
  8. mysql_close();
  9. }else
  10. if($row['username']!=$username){
  11. $adminInfo['username']='error_u';
  12. }
  13. if($row['password']!=$password){
  14. $adminInfo['password']='error_p';
  15. }
  16. //var_dump($adminInfo);exit;
  17. echo json_encode($adminInfo);
复制代码

具体如下: checkCode.class.php//验证码

  1. /*
  2. * Captcha Class base on PHP GD Lib
  3. * @author Design
  4. * @version 1.0
  5. * @copyright js8.in 2010 bbs.it-home.org
  6. * @demo
  7. * include('captchaClass.php');
  8. * $captchaDemo=new Captcha();
  9. * $captchaDemo->createImage();
  10. */
  11. class Captcha{
  12. //@定义验证码图片高度
  13. private $height;
  14. //@定义验证码图片宽度
  15. private $width;
  16. //@定义验证码字符个数
  17. private $textNum;
  18. //@定义验证码字符内容
  19. private $textContent;
  20. //@定义字符颜色
  21. private $fontColor;
  22. //@定义随机出的文字颜色
  23. private $randFontColor;
  24. //@定义字体大小
  25. private $fontSize;
  26. //@定义字体
  27. private $fontFamily;
  28. //@定义背景颜色
  29. private $bgColor;
  30. //@定义随机出的背景颜色
  31. private $randBgColor;
  32. //@定义字符语言
  33. private $textLang;
  34. //@定义干扰点数量
  35. private $noisePoint;
  36. //@定义干扰线数量
  37. private $noiseLine;
  38. //@定义是否扭曲
  39. private $distortion;
  40. //@定义扭曲图片源
  41. private $distortionImage;
  42. //@定义是否有边框
  43. private $showBorder;
  44. //@定义验证码图片源
  45. private $image;
  46. //@Constructor 构造函数
  47. public function Captcha(){
  48. $this->textNum=4;
  49. $this->fontSize=16;
  50. $this->fontFamily='c:\\windows\\fonts\SIMYOU.ttf';//设置中文字体,可以改成linux的目录
  51. $this->textLang='en';
  52. $this->noisePoint=30;
  53. $this->noiseLine=3;
  54. $this->distortion=false;
  55. $this->showBorder=false;
  56. }
  57. //@设置图片宽度
  58. public function setWidth($w){
  59. $this->width=$w;
  60. }
  61. //@设置图片高度
  62. public function setHeight($h){
  63. $this->height=$h;
  64. }
  65. //@设置字符个数
  66. public function setTextNumber($textN){
  67. $this->textNum=$textN;
  68. }
  69. //@设置字符颜色
  70. public function setFontColor($fc){
  71. $this->fontColor=sscanf($fc,'#%2x%2x%2x');
  72. }
  73. //@设置字号
  74. public function setFontSize($n){
  75. $this->fontSize=$n;
  76. }
  77. //@设置字体
  78. public function setFontFamily($ffUrl){
  79. $this->fontFamily=$ffUrl;
  80. }
  81. //@设置字符语言
  82. public function setTextLang($lang){
  83. $this->textLang=$lang;
  84. }
  85. //@设置图片背景
  86. public function setBgColor($bc){
  87. $this->bgColor=sscanf($bc,'#%2x%2x%2x');
  88. }
  89. //@设置干扰点数量
  90. public function setNoisePoint($n){
  91. $this->noisePoint=$n;
  92. }
  93. //@设置干扰线数量
  94. public function setNoiseLine($n){
  95. $this->noiseLine=$n;
  96. }
  97. //@设置是否扭曲
  98. public function setDistortion($b){
  99. $this->distortion=$b;
  100. }
  101. //@设置是否显示边框
  102. public function setShowBorder($border){
  103. $this->showBorder=$border;
  104. }
  105. //@初始化验证码图片
  106. public function initImage(){
  107. if(empty($this->width)){$this->width=floor($this->fontSize*1.3)*$this->textNum+10;}
  108. if(empty($this->height)){$this->height=$this->fontSize*2;}
  109. $this->image=imagecreatetruecolor($this->width,$this->height);
  110. if(empty($this->bgColor)){
  111. $this->randBgColor=imagecolorallocate($this->image,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
  112. }else{
  113. $this->randBgColor=imagecolorallocate($this->image,$this->bgColor[0],$this->bgColor[1],$this->bgColor[2]);
  114. }
  115. imagefill($this->image,0,0,$this->randBgColor);
  116. }
  117. //@产生随机字符
  118. public function randText($type){
  119. $string='';
  120. switch($type){
  121. case 'en':
  122. $str='ABCDEFGHJKLMNPQRSTUVWXY3456789';
  123. for($i=0;$itextNum;$i++){
  124. $string=$string.','.$str[mt_rand(0,29)];
  125. }
  126. break;
  127. case 'cn':
  128. for($i=0;$itextNum;$i++) {
  129. $string=$string.','.chr(rand(0xB0,0xCC)).chr(rand(0xA1,0xBB));
  130. }
  131. $string=iconv('GB2312','UTF-8',$string); //转换编码到utf8
  132. break;
  133. }
  134. return substr($string,1);
  135. }
  136. //@输出文字到验证码
  137. public function createText(){
  138. $textArray=explode(',',$this->randText($this->textLang));
  139. $this->textContent=join('',$textArray);
  140. if(empty($this->fontColor)){
  141. $this->randFontColor=imagecolorallocate($this->image,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
  142. }else{
  143. $this->randFontColor=imagecolorallocate($this->image,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
  144. }
  145. for($i=0;$itextNum;$i++){
  146. $angle=mt_rand(-1,1)*mt_rand(1,20);
  147. imagettftext($this->image,$this->fontSize,$angle,5+$i*floor($this->fontSize*1.3),floor($this->height*0.75),$this->randFontColor,$this->fontFamily,$textArray[$i]);
  148. }
  149. }
  150. //@生成干扰点
  151. public function createNoisePoint(){
  152. for($i=0;$inoisePoint;$i++){
  153. $pointColor=imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  154. imagesetpixel($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),$pointColor);
  155. }
  156. }
  157. //@产生干扰线
  158. public function createNoiseLine(){
  159. for($i=0;$inoiseLine;$i++) {
  160. $lineColor=imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),20);
  161. imageline($this->image,0,mt_rand(0,$this->width),$this->width,mt_rand(0,$this->height),$lineColor);
  162. }
  163. }
  164. //@扭曲文字
  165. public function distortionText(){
  166. $this->distortionImage=imagecreatetruecolor($this->width,$this->height);
  167. imagefill($this->distortionImage,0,0,$this->randBgColor);
  168. for($x=0;$xwidth;$x++){
  169. for($y=0;$yheight;$y++){
  170. $rgbColor=imagecolorat($this->image,$x,$y);
  171. imagesetpixel($this->distortionImage,(int)($x+sin($y/$this->height*2*M_PI-M_PI*0.5)*3),$y,$rgbColor);
  172. }
  173. }
  174. $this->image=$this->distortionImage;
  175. }
  176. //@生成验证码图片
  177. public function createImage(){
  178. $this->initImage(); //创建基本图片
  179. $this->createText(); //输出验证码字符
  180. if($this->distortion){$this->distortionText();} //扭曲文字
  181. $this->createNoisePoint(); //产生干扰点
  182. $this->createNoiseLine(); //产生干扰线
  183. if($this->showBorder){imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$this->randFontColor);} //添加边框
  184. imagepng($this->image);
  185. imagedestroy($this->image);
  186. if($this->distortion){imagedestroy($this->$distortionImage);}
  187. return $this->textContent;
  188. }
  189. }
  190. ?>
  191. code.php//new 一个对象,负责图片的创建以及验证码文本写入session
  192. session_start();
  193. header("Content-type:image/png");
  194. include('checkCode.class.php');
  195. $captcha5=new Captcha();
  196. //@设置验证码宽度
  197. //$captcha5->setWidth(200);
  198. //@设置验证码高度
  199. //$captcha5->setHeight(50);
  200. //@设置字符个数
  201. $captcha5->setTextNumber(4);
  202. //@设置字符颜色
  203. //$captcha5->setFontColor('#ffffff');
  204. //@设置字号大小
  205. //$captcha5->setFontSize(25);
  206. //@设置字体
  207. $captcha5->setFontFamily('c:\\windows\\fonts\\comic.TTF');
  208. //@设置语言
  209. $captcha5->setTextLang('en');
  210. //@设置背景颜色
  211. //$captcha5->setBgColor('#000000');
  212. //@设置干扰点数量
  213. //$captcha5->setNoisePoint(600);
  214. //@设置干扰线数量
  215. //$captcha5->setNoiseLine(10);
  216. //@设置是否扭曲
  217. //$captcha5->setDistortion(true);
  218. //@设置是否显示边框
  219. $captcha5->setShowBorder(true);
  220. //输出验证码
  221. $code=$captcha5->createImage();
  222. $_SESSION['captchaCode']['content']=$code;
  223. //$_SESSION['captchaCode']['time']=microtime();
  224. ?>
复制代码

login.php//登陆页面,调用生成的验证码图片

  1. 验证码:
  • http://img.jbxue.com/admin/code.php
  • 复制代码


    声明
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    解释负载平衡如何影响会话管理以及如何解决。解释负载平衡如何影响会话管理以及如何解决。Apr 29, 2025 am 12:42 AM

    负载均衡会影响会话管理,但可以通过会话复制、会话粘性和集中式会话存储解决。1.会话复制在服务器间复制会话数据。2.会话粘性将用户请求定向到同一服务器。3.集中式会话存储使用独立服务器如Redis存储会话数据,确保数据共享。

    说明会话锁定的概念。说明会话锁定的概念。Apr 29, 2025 am 12:39 AM

    Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

    有其他PHP会议的选择吗?有其他PHP会议的选择吗?Apr 29, 2025 am 12:36 AM

    PHP会话的替代方案包括Cookies、Token-basedAuthentication、Database-basedSessions和Redis/Memcached。1.Cookies通过在客户端存储数据来管理会话,简单但安全性低。2.Token-basedAuthentication使用令牌验证用户,安全性高但需额外逻辑。3.Database-basedSessions将数据存储在数据库中,扩展性好但可能影响性能。4.Redis/Memcached使用分布式缓存提高性能和扩展性,但需额外配

    在PHP的上下文中定义'会话劫持”一词。在PHP的上下文中定义'会话劫持”一词。Apr 29, 2025 am 12:33 AM

    Sessionhijacking是指攻击者通过获取用户的sessionID来冒充用户。防范方法包括:1)使用HTTPS加密通信;2)验证sessionID的来源;3)使用安全的sessionID生成算法;4)定期更新sessionID。

    PHP的完整形式是什么?PHP的完整形式是什么?Apr 28, 2025 pm 04:58 PM

    文章讨论了PHP,详细介绍了其完整形式,在We​​b开发中的主要用途,与Python和Java的比较以及对初学者的学习便利性。

    PHP如何处理形式数据?PHP如何处理形式数据?Apr 28, 2025 pm 04:57 PM

    PHP使用$ \ _ post和$ \ _获取超级全局的php处理数据,并通过验证,消毒和安全数据库交互确保安全性。

    PHP和ASP.NET有什么区别?PHP和ASP.NET有什么区别?Apr 28, 2025 pm 04:56 PM

    本文比较了PHP和ASP.NET,重点是它们对大规模Web应用程序,性能差异和安全功能的适用性。两者对于大型项目都是可行的,但是PHP是开源和无关的,而ASP.NET,

    PHP是对病例敏感的语言吗?PHP是对病例敏感的语言吗?Apr 28, 2025 pm 04:55 PM

    PHP的情况敏感性各不相同:功能不敏感,而变量和类是敏感的。最佳实践包括一致的命名和使用对案例不敏感的功能进行比较。

    See all articles

    热AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智能驱动的应用程序,用于创建逼真的裸体照片

    AI Clothes Remover

    AI Clothes Remover

    用于从照片中去除衣服的在线人工智能工具。

    Undress AI Tool

    Undress AI Tool

    免费脱衣服图片

    Clothoff.io

    Clothoff.io

    AI脱衣机

    Video Face Swap

    Video Face Swap

    使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

    热工具

    SublimeText3 Linux新版

    SublimeText3 Linux新版

    SublimeText3 Linux最新版

    SublimeText3汉化版

    SublimeText3汉化版

    中文版,非常好用

    VSCode Windows 64位 下载

    VSCode Windows 64位 下载

    微软推出的免费、功能强大的一款IDE编辑器

    安全考试浏览器

    安全考试浏览器

    Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

    PhpStorm Mac 版本

    PhpStorm Mac 版本

    最新(2018.2.1 )专业的PHP集成开发工具