2012-11-22 36 views
0

如何檢查服務器端PHP中此驗證碼腳本的正確性?如何檢查此驗證碼腳本的正確性

我用這個腳本來顯示驗證碼圖片並通過ajax檢查。現在我想在服務器端PHP中檢查它。怎麼做?

captcha.php

<?php 
//Start a session so we can store the captcha code as a session variable. 
session_start(); 

// Decide what characters are allowed in our string 
// Our captcha will be case-insensitive, and we avoid some 
// characters like 'O' and 'l' that could confuse users 
$charlist = '23456789ABCDEFGHJKMNPQRSTVWXYZ'; 

// Trim string to desired number of characters - 5, say 
$chars = 5; 
$i = 0; 
while ($i < $chars) 
{ 
    $string .= substr($charlist, mt_rand(0, strlen($charlist)-1), 1); 
    $i++; 
} 

// Create a GD image from our background image file 
$captcha = imagecreatefrompng('images/captcha.png'); 

// Set the colour for our text string 
// This is chosen to be hard for machines to read against the background, but 
// OK for humans 
$col = imagecolorallocate($captcha, 0, 0, 0); 

// Write the string on to the image using TTF fonts 
imagettftext($captcha, 29, 1, 15, 29, $col, 'images/fonts/arial.ttf', $string); 

// Store the random string in a session variable 
$_SESSION['secret_string'] = $string; 

// Put out the image to the page 
header("Content-type: image/png"); 
imagepng($captcha); 
?> 

回答

0

你有圖形驗證碼在$_SESSION['secret_string'],當用戶提交表單的比賽張貼代碼$_SESSION['secret_string']

0

爲了驗證在服務器端的圖形驗證碼,你可以做

<?php 
session_start(); 
//code is the name of your captcha text field 
if($_SESSION['secret_string']==$_POST['code']) 
echo "Captcha verfication passed.."; 
else 
echo "Captcha verfication failed.."; 
?>