下面这是一款利用php生成验证码程序了,利用了srand随便函数生成并用php gd库的图像函数生成验证图片,再把生成的数据保存到session全局变量.
调用此页面,如果下面的式子成立,则生成验证码图片,代码如下:
- 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);
- }
调用方法,代码如下:
<img src="code.php?action=verifycode" />
判断验证码是否输入正确,代码如下:
- if( $_post['code'] == $_session['login_check_num'] )
- {
- echo '验证码正确';
- }
- else
- {
- echo '验证码错误';
- }
|