验证码的程序
XML/HTML代码
- //字体文件
- $font = $other_path.'./include/system/luxisr.ttf';
- $width = array_key_exists('width', $_GET) ? $_GET['width'] : 70;
- $high = array_key_exists('high' , $_GET) ? $_GET['high'] : 20;
- $size = array_key_exists('size' , $_GET) ? $_GET['size'] : 12;
- //生成验证码
- $text = '';
- for($i=0; $i<4; $i++){
- $text .= rand(0,9);
- }
- session_start();
- $_SESSION['check_code']=$text;
- $image = imagecreate($width, $high);
- $whiteColor = imagecolorallocate($image, 255, 255, 255);
- $blackColor = imagecolorallocate($image, 0, 0, 0);
- $count = $width * $high / 8;
- for($i = 0; $i < $count; $i++){
- $randomColor = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
- imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $high), $randomColor);
- }
- $fontSize = imagettfbbox($size, 0, $font, $text);
- $centerX = abs($fontSize[2] - $fontSize[0]);
- $centerY = abs($fontSize[5] - $fontSize[3]);
- $x = ($width - $centerX) / 2;
- $y = ($high - $centerY) / 2 + $centerY;
- ///生成图片
- imagettftext($image, $size, mt_rand(-5, +5), $x, $y, $blackColor, $font, $text);
- imagerectangle($image, 0, 0, $width - 1, $high - 1, $blackColor);
- imagepng($image);
- imagedestroy($image);
提交表单中的程序
XML/HTML代码
- session_start();
- $vote_code=$_SESSION['check_code'];
- $vote_check=(int)$code;
- if(empty($vote_code) || $vote_code!=$vote_check){
- echo '<script>alert("非法投票,请检查验证码!");</script>';
- echo '<script>history.go(-1);</script>';
- exit();
- }
HTML中包含验证码
XML/HTML代码
- <img src="checkcode.php?t=<?=time();?>" id="checkcode" align=absmiddle style="margin-left:16px;margin-bottom:5px;">
- <input type="text" name="code" id="code" value="" size=8 style="margin-left:16px;margin-bottom:3px;" />