PHP的数字验证码

Numeric Captcha for PHP(PHP的数字验证码)
本文介绍了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

这篇关于PHP的数字验证码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)