本教程主要利用jquery 的ajax来实现无刷新jquery php验证码代码的提前验证操作,实例代码如下:
- <?php
-
- if($_get['action']=='verifycode'){
- rand_create();
- }
-
- function rand_create(){
-
-
- header('content-type: image/png');
-
-
- srand((double)microtime()*1000000);
-
- $im = imagecreate(62,20);
- $black = imagecolorallocate($im, 0,0,0);
- $white = imagecolorallocate($im, 255,255,255);
- $gray = imagecolorallocate($im, 200,200,200);
-
- imagefill($im,0,0,$gray);
- while(($randval=rand()%100000)<10000);{
-
- session_start();
- $_session['login_check_num'] = $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);
- }
- ?>
静态页面上显示验证码代码为:
- 验证码:<input class="reg_main_code_input" type="text" name="reg_code" id="reg_code" />
- <img id="reg_code_img" src="code.php?action=verifycode" />
jquery部分的ajax验证代码为:
- $.post("session.php",
- {reg_code:$("#reg_code").val()},
- function(data){
- if(data === "1"){
-
- }else{
- do... }
- }
- );
而协助ajax验证的php页面名为session.php,其代码如下:
- <?php
- session_start();
-
- if($_post['reg_code'] == $_session['login_check_num']){
- echo 1;
- }else{
- echo 0;
- exit();
- }
- ?>
|