Home Backend Development PHP Tutorial Solution to the inconsistent php session verification code and always display the last verification code

Solution to the inconsistent php session verification code and always display the last verification code

Jul 25, 2016 am 09:04 AM

  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("Username cannot be empty");
  7. return false;
  8. }
  9. if(password==""){
  10. alert("Password cannot be empty");
  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('Verification code error, please re-enter the verification code');
  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( "The username or password entered is incorrect, please check and log in again!");
  25. window.location="login.php";
  26. }
  27. });
  28. });
Copy code

Backend checkLogin.php key code:

  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);
Copy code

The details are as follows: checkCode.class.php//Verification code

  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. //@Define the height of the verification code image
  13. private $height;
  14. //@Define the width of the verification code image
  15. private $width;
  16. //@Define the number of verification code characters
  17. private $textNum;
  18. //@Define the content of the verification code characters
  19. private $textContent;
  20. //@ Define character color
  21. private $fontColor;
  22. //@Define random text color
  23. private $randFontColor;
  24. //@Define font size
  25. private $fontSize;
  26. //@Define font
  27. private $fontFamily;
  28. //@ Define the background color
  29. private $bgColor;
  30. //@Define the random background color
  31. private $randBgColor;
  32. //@Define the character language
  33. private $textLang;
  34. //@Define the number of interference points
  35. private $noisePoint;
  36. / /@Define the number of interference lines
  37. private $noiseLine;
  38. //@Define whether to distort
  39. private $distortion;
  40. //@Define the distorted image source
  41. private $distortionImage;
  42. //@Define whether there is a border
  43. private $showBorder;
  44. //@Define verification code image source
  45. private $image;
  46. //@Constructor constructor
  47. public function Captcha(){
  48. $this->textNum=4;
  49. $this->fontSize=16;
  50. $ this->fontFamily='c:\windows\fontsSIMYOU.ttf';//Set the Chinese font, you can change it to the Linux directory
  51. $this->textLang='en';
  52. $this->noisePoint=30 ;
  53. $this->noiseLine=3;
  54. $this->distortion=false;
  55. $this->showBorder=false;
  56. }
  57. //@Set the image width
  58. public function setWidth($w){
  59. $this->width=$w;
  60. }
  61. //@Set the image height
  62. public function setHeight($h){
  63. $this->height=$h;
  64. }
  65. //@Set the character Number
  66. public function setTextNumber($textN){
  67. $this->textNum=$textN;
  68. }
  69. //@Set character color
  70. public function setFontColor($fc){
  71. $this->fontColor=sscanf ($fc,'#%2x%2x%2x');
  72. }
  73. //@Set the font size
  74. public function setFontSize($n){
  75. $this->fontSize=$n;
  76. }
  77. // @Set the font
  78. public function setFontFamily($ffUrl){
  79. $this->fontFamily=$ffUrl;
  80. }
  81. //@Set the character language
  82. public function setTextLang($lang){
  83. $this->textLang= $lang;
  84. }
  85. //@Set the picture background
  86. public function setBgColor($bc){
  87. $this->bgColor=sscanf($bc,'#%2x%2x%2x');
  88. }
  89. //@Set the number of interference points
  90. public function setNoisePoint($n){
  91. $this->noisePoint=$n;
  92. }
  93. //@Set the number of interference lines
  94. public function setNoiseLine($n){
  95. $this ->noiseLine=$n;
  96. }
  97. //@Set whether to display the border
  98. public function setDistortion($b){
  99. $this->distortion=$b;
  100. }
  101. //@Set whether to display the border
  102. public function setShowBorder($border){
  103. $this->showBorder=$border;
  104. }
  105. //@Initialization verification code picture
  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. //@Generate random characters
  118. public function randText($type){
  119. $string='';
  120. switch($type){
  121. case 'en':
  122. $ str='ABCDEFGHJKLMNPQRSTUVWXY3456789';
  123. for($i=0;$i<$this->textNum;$i++){
  124. $string=$string.','.$str[mt_rand(0,29)];
  125. }
  126. break;
  127. case 'cn':
  128. for($i=0;$i<$this->textNum;$i++) {
  129. $string=$string.','.chr(rand(0xB0, 0xCC)).chr(rand(0xA1,0xBB));
  130. }
  131. $string=iconv('GB2312','UTF-8',$string); //Convert encoding to utf8
  132. break;
  133. }
  134. return substr ($string,1);
  135. }
  136. //@Output text to verification code
  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;$i<$this->textNum;$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;$i<$this->noisePoint;$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;$i<$this->noiseLine;$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;$x<$this->width;$x++){
  169. for($y=0;$y<$this->height;$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
  • 复制代码


    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1658
    14
    PHP Tutorial
    1257
    29
    C# Tutorial
    1231
    24
    How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

    Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

    Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

    There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

    PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

    PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

    Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

    In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

    What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

    HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

    PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

    PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

    Explain Arrow Functions (short closures) introduced in PHP 7.4. Explain Arrow Functions (short closures) introduced in PHP 7.4. Apr 06, 2025 am 12:01 AM

    The arrow function was introduced in PHP7.4 and is a simplified form of short closures. 1) They are defined using the => operator, omitting function and use keywords. 2) The arrow function automatically captures the current scope variable without the use keyword. 3) They are often used in callback functions and short calculations to improve code simplicity and readability.

    PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

    PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

    See all articles