-
- session_start();
- //Randomly generate a string of verification codes
- function random($len) {
- $srcstr="ABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
- mt_srand();
- $strs=" ";
- for($i=0;$i<$len;$i++) {
- $strs.=$srcstr[mt_rand(0,33)];
- }
- return ($strs);
- }
- $str =random(5); //Randomly generated string
- $width=60; //Width of the verification code image
- $height=25; //Height of the verification code image
- //Date in the past
- header(" Expires:Mon,26 Jul 1997 05:00:00 GMT");
- //always modified
- header("Last-Modified:".gmdate("D,d M Y H:i:s")."GMT");
- //HTTP/1.1
- header("Cache-Control:no-store,no-cache,must-revalidate");
- header("Cache-Control:post-check=0,pre-check=0",false );
- //HTTP/1.0
- header("Pragma:no-cache");
- header("Content-Type:image/png");
-
- $im=imagecreate($width,$height);
- $ back=imagecolorallocate($im,0xFF,0xFF,0xFF); //Background color
- $pix=imagecolorallocate($im,187,190,247); //Blurry point color
- $font=imagecolorallocate($im,41,163,238); //Font Color
- //Draw 1000 blurred points
- mt_srand();
- for($i=0;$i<1000;$i++) {
- imagesetpixel($im,mt_rand(0,$width),mt_rand(0 ,$height),$pix);
- }
- imagestring($im,5,7,5,$str,$font);//Draw a randomly generated string
- imagerectangle($im,0,0,$width -1,$height-1,$font);//Draw a 1px border around the verification code image
- imagepng($im);//Create a PNG format graphic
- imagedestroy($im);//Handle the image Deconstruct and release in memory space
- $_SESSION["auth_code"]=$str;
- ?>
Copy code
Example 2, PHP random verification code
php generates simple verification code
-
- $image_width=140;
- $image_height=50;
- srand(microtime()*10000);
- for($i=0;$i<4;$i++){
- $number.=dechex(rand(0,15));
- }
- $check=$number;
- $im=imagecreate($image_width,$image_height);
- imagecolorallocate($im,255,255,255);
- for($i =0;$i $font=rand(40,80);
- $x=rand(1,8)+$image_width*$i/4;
- $y= rand(1,$image_height/4);
- $color=imagecolorallocate($im,rand(0,100),rand(0,150),rand(0,200));
- imagestring($im,$font,$x,$y, $check[$i],$color);
- }
- imagepng($im);
- imagedestroy($im);
- ?>
Copy code
|