PHP的数字验证码
(不依赖 JavaScript 被打开)
- 我知道有JS独立的验证码.
- 我知道那里那里有 PHP 验证码.
- 我知道那里有数字验证码.
但我正在寻找一个 PHP numeric Javascript-independent 验证码.
我发现的唯一数字验证码用于 ASP.NET 或基于 jQuery/JS 的验证码.
我不希望任何这些作为问题的答案.
But I'm looking for a PHP numeric Javascript-independent captcha.
The only numeric captcha's I've found are either for ASP.NET or jQuery/JS based ones.
I don't want any of those as an answer to the question.
我不是在这里谈论一个小网站.在答案中,我想知道您的建议是否会给服务器带来很大压力.
And I'm not taking about a small website here. In the answer I'd like to know whether your suggestion puts a lot of strain on the server or not.
推荐答案
我猜在处理验证码的时候是不能避免处理渲染图像的吧?这是一个简单的(可能不是最优雅的):
I guess it can't be avoided to deal with rendering an image when dealing with captchas? Here's a simple one (and may not be the most elegant):
session_start();
$strings = '123456789';
$i = 0;
$characters = 6;
$code = '';
while ($i < $characters)
{
$code .= substr($strings, mt_rand(0, strlen($strings)-1), 1);
$i++;
}
$_SESSION['captcha'] = $code;
//generate image
$im = imagecreatetruecolor(124, 40);
$foreground = imagecolorallocate($im, 0, 0, 0);
$shadow = imagecolorallocate($im, 173, 172, 168);
$background = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 200, 200, $background);
// use your own font!
$font = 'monofont.ttf';
//draw text:
imagettftext($im, 35, 0, 9, 28, $shadow, $font, $code);
imagettftext($im, 35, 0, 2, 32, $foreground, $font, $code);
// prevent client side caching
header("Expires: Wed, 1 Jan 1997 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
//send image to browser
header ("Content-type: image/png");
imagepng($im);
imagedestroy($im);
以表格形式显示:
<img src="captcha.php">
Enter the code above: <input type="text" name="captcha">
提交后,检查输入的代码:
Once submitted, check the entered code:
if ($_POST['captcha'] == $_SESSION['captcha'])
// do your thing
相关文章