本文章給大家介紹一個非常不錯的生成驗證碼的php程序,首先當用戶請求HTTP的時候,服務器端就創建一個唯一的sessionid,這個是session會話ID,然後就去啟動GD庫或者imagemagick這些畫圖工具,把程序生成的隨機的字符寫到一張圖片裡面,然後顯示到客戶端,再由用戶輸入提交數據,然後我們程序把生成驗證保存在session中進行比較,這樣就完成了驗證碼的生成與驗證了。
•新建一個PHP文件captcha_code_file.php
代碼如下 復制代碼//首先開啟session
session_start();
//定義前台顯示驗證碼長&寬
$image_width = 120;
$image_height = 40;
$characters_on_image = 6;
$font = './monofont.ttf';
//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)
$possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
$random_dots = 10;
$random_lines = 30;
$captcha_text_color="0x142864";
$captcha_noice_color = "0x142864";
//定義要生成驗證碼的字符串
$code = '';
$i = 0;
while ($i < $characters_on_image) {
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}
$font_size = $image_height * 0.75;
$image = @imagecreate($image_width, $image_height);
/* setting the background, text and noise colours here */
$background_color = imagecolorallocate($image, 255, 255, 255);
$arr_text_color = hexrgb($captcha_text_color);
$text_color = imagecolorallocate($image, $arr_text_color['red'],
$arr_text_color['green'], $arr_text_color['blue']);
$arr_noice_color = hexrgb($captcha_noice_color);
$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'],
$arr_noice_color['green'], $arr_noice_color['blue']);
/* generating the dots randomly in background */
for( $i=0; $i<$random_dots; $i++ ) {
imagefilledellipse($image, mt_rand(0,$image_width),
mt_rand(0,$image_height), 2, 3, $image_noise_color);
}
/* generating lines randomly in background of image */
for( $i=0; $i<$random_lines; $i++ ) {
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
}
/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox($font_size, 0, $font, $code);
$x = ($image_width - $textbox[4])/2;
$y = ($image_height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
/* Show captcha image in the page html page */
header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
//設置session,做驗證
$_SESSION['6_letters_code'] = $code;
function hexrgb ($hexstr)
{
$int = hexdec($hexstr);
return array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
}
調用頁面顯示驗證碼頁面index.php
代碼如下 復制代碼<?php
session_start();
if(isset($_REQUEST['Submit'])){
// code for check server side validation
if(empty($_SESSION['6_letters_code'] ) ||
strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
{
$msg="您輸入的驗證碼有誤,請重新輸入!";
}else{
echo "您輸入的是正確的!";
// Captcha verification is Correct. Final Code Execute here!
}
}
?>
<style type="text/css">
.table{
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
color:#333;
background-color:#E4E4E4;
}
.table td{
background-color:#F8F8F8;
}
</style>
<form action="" method="post" name="form1" id="form1" >
<table width="400" border="0" align="center" cellpadding="5" cellspacing="1">
<?php if(isset($msg)){?>
<tr>
<td colspan="2" align="center" valign="top"><?php echo $msg;?></td>
</tr>
<?php } ?>
<tr>
<td align="right" valign="top"> 驗證碼Demo:</td>
<td><img src="captcha_code_file.php?rand=<?php echo rand(0,20);?>" id='captchaimg' onclick="refreshCaptcha();" ><br>
<label for='message'>請輸入驗證碼:</label>
<br>
<input id="6_letters_code" name="6_letters_code" type="text">
<br>
如果看不到,請 <a href='javascript: refreshCaptcha();'>點我</a> 刷新一下!
</p></td>
</tr>
<tr>
<td> </td>
<td><input name="Submit" type="submit" onclick="return validate();" value="Submit"></td>
</tr>
</table>
</form>
<script type='text/javascript'>
//定義的刷新請求
function refreshCaptcha()
{
var img = document.images['captchaimg'];
img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
}
</script>