Maison  >  Article  >  développement back-end  >  php session验证码不一致总显示上次验证码的解决方法

php session验证码不一致总显示上次验证码的解决方法

WBOY
WBOYoriginal
2016-07-25 09:04:231347parcourir
  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
  • 复制代码


    Déclaration:
    Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn