Home > php教程 > php手册 > php jquery 验证码代码

php jquery 验证码代码

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-02 09:14:12
Original
1104 people have browsed it

本教程主要利用jquery 的ajax来实现无刷新jquery php验证码代码的提前验证操作,实例代码如下:

<?php 
//调用此页面,如果下面的式子成立,则生成验证码图片 
if($_get[&#39;action&#39;]==&#39;verifycode&#39;){ 
    rand_create(); 
} 
//验证码图片生成 
function rand_create(){ 
    //通知浏览器将要输出png图片 
 
    header(&#39;content-type: image/png&#39;); 
 
    //准备好随机数发生器种子  
    srand((double)microtime()*1000000); 
    //准备图片的相关参数  
    $im = imagecreate(62,20); 
    $black = imagecolorallocate($im, 0,0,0); //rgb黑色标识符 
    $white = imagecolorallocate($im, 255,255,255); //rgb白色标识符 
    $gray = imagecolorallocate($im, 200,200,200); //rgb灰色标识符 
    //开始作图     
    imagefill($im,0,0,$gray); 
    while(($randval=rand()%100000)<10000);{  
        //将四位整数验证码绘入图片 
     session_start(); 
   $_session[&#39;login_check_num&#39;] = $randval;  
        imagestring($im, 5, 10, 3, $randval, $black); 
    }  
    //加入干扰象素  
    for($i=0;$i<200;$i++){ 
        $randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); 
        imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); 
    } 
    //输出验证图片 
    imagepng($im); 
    //销毁图像标识符 
    imagedestroy($im);  
}
Copy after login

静态页面上显示验证码代码为:

验证码:

php jquery 验证码代码

jquery部分的ajax验证代码为:

$.post("session.php", 
      {reg_code:$("#reg_code").val()}, 
      function(data){ 
      if(data === "1"){ 
       //do... 
      }else{ 
       do...      } 
    } 
);
Copy after login

而协助ajax验证的php页面名为session.php,其代码如下:

<?php 
session_start(); 
//检验验证码 
if($_post[&#39;reg_code&#39;] == $_session[&#39;login_check_num&#39;]){ 
   echo 1;//开源代码phprm.com 
}else{ 
   echo 0; 
   exit(); 
}
Copy after login

                     


文章网址:

随意转载^^但请附上教程地址。

Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template