如何在服务器端 PHP 中检查此验证码脚本的正确性?
我使用此脚本显示验证码图像并通过 ajax 进行检查。现在我也想在服务器端 PHP 中检查它。怎么做?
验证码.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);
?>