PHP 验证码例子

1、生成验证码

<?php

session_start();

//生成验证码图片

Header("Content-type: image/PNG");

srand((double)microtime()*1000000);

$im = imagecreate(62,20);

$red = ImageColorAllocate($im, 235,128,177);

$white = ImageColorAllocate($im, 255,255,255);

$gray = ImageColorAllocate($im, 200,200,200);

imagefill($im,68,30,$red);

while(($authnum=rand()%100000)<10000);

//将四位整数验证码绘入图片

imagestring($im, 5, 10, 3, $authnum, $white);

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

//存在session中

$_SESSION['SESSION_VALIDATE_CODE'] = $authnum.'';

?>

2、jquery validation 表单验证框架 远程Ajax请求以下文件,返回 true 或 false

<?php

    session_start();

    $validateCode=$_GET['validateCode']; // 前端表单传过来的用户填写的验证码

    echo $_SESSION['SESSION_VALIDATE_CODE']==$validateCode?"true":"false";

?>

猜你喜欢

转载自zsjg13.iteye.com/blog/2236881