PHP 数字验证码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6041555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Numeric Captcha for PHP
提问by Adam Lynch
Is there a numericcaptcha available for PHP?
是否有可用于PHP的数字验证码?
(which doesn't rely on JavaScript being turned on)
(它不依赖于 JavaScript 被打开)
EDIT:
编辑:
- I know there are JS-independent captchas out there.
- I know there are PHP captchas out there.
- I know there are numeric captchas out there.
- 我知道那里有独立于 JS 的验证码。
- 我知道那里有 PHP 验证码。
- 我知道那里有数字验证码。
But I'm looking for a PHP numeric Javascript-independentcaptcha.
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.
但我正在寻找一个PHP 数字 Javascript 独立验证码。
我发现的唯一数字验证码要么是针对 ASP.NET 的,要么是基于 jQuery/JS 的。
我不希望其中任何一个作为问题的答案。
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.
我不是在这里讨论一个小网站。在答案中,我想知道您的建议是否会给服务器带来很大压力。
回答by tradyblix
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);
Display it in a form:
以表格形式显示:
<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
回答by fire
CAPTCHA's dont necessarily rely on javascript (I think your thinking of reCATCHA), a stand alone image is all you need.
CAPTCHA 不一定依赖于 javascript(我认为您对 reCATCHA 的想法),您只需要一个独立的图像。
At it's most simple, a numeric CAPTCHA would work like...
在最简单的情况下,数字验证码会像...
<?php
session_start();
if (isset($_POST['code'])) {
if ($_POST['code'] == $_SESSION['captcha']) {
echo "Captcha valid";
}
else {
echo "Captcha NOT valid";
}
}
$_SESSION['captcha'] = mt_rand(10000, 99999);
?>
<form action="" method="post">
<p>Enter this number: <?php echo $_SESSION['captcha']; ?></p>
<p><input type="text" name="code" /> <input type="submit" value="Submit" />
</form>
回答by Alexander
You can use http://www.captcha.ru/kcaptcha/Default this library generate key with alphabet symbols, but you can change config file(set new value for variable $allowed_symbols = "0123456789" as example) for generating only numeric keys.
您可以使用http://www.captcha.ru/kcaptcha/默认此库生成带有字母符号的密钥,但您可以更改配置文件(例如为变量 $allowed_symbols = "0123456789" 设置新值)以仅生成数字键.
回答by Fidi
May be you have also thought about a figletcaptcha. This saves lots of time, because you don't need images to be rendered. In my homepage I am using this kind of captcha. You may take a look at this page(scroll to the bottom, you can't oversee it ;)). I've implemented it via Zend-Framework. Better said with Zend_Captcha_Figlet.
可能您还考虑过figlet 验证码。这可以节省大量时间,因为您不需要渲染图像。在我的主页中,我正在使用这种验证码。你可以看看这个页面(滚动到底部,你无法监督它;))。我已经通过 Zend-Framework 实现了它。最好用Zend_Captcha_Figlet说。
However I think it's hard to implement if you are not using Zend_Form. But I think it's quite a nice solution.
但是,我认为如果您不使用 Zend_Form,则很难实现。但我认为这是一个很好的解决方案。
EDIT
编辑
Another solution would be a blind-captcha. It's very easy to implement and works great (had good experience with that for a long time). It works like this:
另一种解决方案是盲验证码。它很容易实现并且效果很好(很长一段时间都有很好的经验)。它是这样工作的:
- In your form provide an input-field with an empty value.
- Hide it initially via css (this should not be done with inline-styles)
- in your form-validation (php) check if this field contains a value. if so it's probably a spambot who filled out each and every input-field.
- A user won't fill it, because he doesn't see it (of course he will see it, if he has disabled css in his browser)
- 在您的表单中提供一个带有空值的输入字段。
- 最初通过 css 隐藏它(这不应该用内联样式来完成)
- 在您的表单验证 (php) 中检查此字段是否包含值。如果是这样,它可能是填写每个输入字段的垃圾邮件机器人。
- 用户不会填充它,因为他没有看到它(当然他会看到它,如果他在浏览器中禁用了 css)
Easy, but effective.
简单,但有效。
回答by Fidi
Don't go for any other php files for adding numerical captcha in your form.Just try the following
不要使用任何其他 php 文件在表单中添加数字验证码。请尝试以下操作
<input id="num1" type="text" name="num1" value="<?php echo rand(1,4); ?>" readonly="readonly" /> +
<input id="num2" type="text" name="num2" value="<?php echo rand(5,9); ?>" readonly="readonly" /> =
<input id="captcha" class="captcha" type="text" onblur="check_captcha(this.value);" maxlength="2" />
and in the function you can validate easily
并且在功能中您可以轻松验证
<script>
function check_captcha(res)
{
if(res)
{
var val=parseInt($('#num1').val())+parseInt($('#num2').val());
if(val!=res)
{
alert('Incorrect value, please try again');
$('#captcha').val('');
$('#captcha').focus();
}
}
}
</script>
回答by user610961
Just open any captcha php file and remove letters from the string which generates captcha word
只需打开任何验证码 php 文件并从生成验证码字的字符串中删除字母