[英]Create image captcha with PHP
Hi i want to create a image captcha with php , i have the next code for my form.嗨,我想用php创建一个图像验证码,我的表单有下一个代码。
<form action="" method="POST">
<div class="label_form">Usuario:</div> <input type="text" name="user"/><br>
<div class="label_form">Contraseña:</div> <input type="password" name="pass"/><br>
<img alt="Numeros aleatorios" src="layouts/captcha.php" />
<input class="label_form" type="text" name="num"/><br>
<input type="submit" value="ENTRAR" name="submit"/>
</form>
This is the code for validation before to send the form:这是发送表单之前的验证代码:
if (isset($_POST["submit"])) {
if ($_SESSION['img_number'] != $_POST['num']) {
echo "<div class='msg_error'>Los caracteres no se corresponden.</div>";
} else {
/*DO STUFF*/
}
}
And in other file with the name captcha.php
i have the code php for generate the image:在另一个名为
captcha.php
文件中,我有用于生成图像的代码php :
header("Content-type: image/png");
$string = "abcdefghijklmnopqrstuvwxyz0123456789";
for ($i = 0; $i < 5; $i++) {
$pos = rand(0, 36);
$str. = $string {
$pos
};
}
$img_handle = ImageCreate(60, 22) or die("Es imposible crear la imagen");
$back_color = ImageColorAllocate($img_handle, 102, 102, 153);
$txt_color = ImageColorAllocate($img_handle, 255, 255, 255);
ImageString($img_handle, 31, 5, 0, $str, $txt_color);
Imagepng($img_handle);
session_start();
$_SESSION['img_number'] = $str;
This give a image broken showing the alt from img
"Numeros aleatorios"
, so that tell me the file.php
is calling fine the img
but the code for generated is not working, any help is gratefull :D thanks.这给出了一个图像,显示来自
img
"Numeros aleatorios"
的 alt,所以告诉我file.php
调用了img
但生成的代码不起作用,任何帮助都非常感谢:D 谢谢。
Try changing:尝试改变:
$str .= $string{$pos};
to到
$str .= $string[$pos]; //Array of strings.
I've updated my answer:我已经更新了我的答案:
<?php
header("Content-type: image/png");
$string = "abcdefghijklmnopqrstuvwxyz0123456789";
for ($i = 0; $i < 5; $i++) {
$pos = rand(0,36);
$str .= $string[$pos];
}
$img_handle = ImageCreate (60, 22) or die ("Es imposible crear la imagen");
$back_color = ImageColorAllocate($img_handle,102,102,153);
$txt_color = ImageColorAllocate($img_handle,255,255,255);
ImageString($img_handle, 31, 5, 0, $str, $txt_color);
Imagepng($img_handle);
session_start();
$_SESSION['img_number'] = $str;
?>
In my local server the captcha image is showing properly.在我的本地服务器中,验证码图像显示正确。
Your code is good the only thing you missed is declaring $str = ""
at the top of your catpcha.php
file您的代码很好,您唯一错过的是在
catpcha.php
文件顶部声明$str = ""
Here is a working capture of your code:这是您的代码的工作捕获:
Your $string
length is 36, so you should generate a random position between 0 and 35, like this:你的
$string
长度是 36,所以你应该生成一个 0 到 35 之间的随机位置,像这样:
$pos = rand(0,35);
If not, you sometimes get a broken image and a PHP Notice.如果没有,您有时会收到损坏的图像和 PHP 通知。
The reason why you get a broken image without declaring $str = ""
is because this line不声明
$str = ""
就得到一个损坏的图像的原因是因为这一行
$str .= $string{$pos};
Throws a PHP
Notice and corrupts the image.抛出一个
PHP
通知并损坏图像。 (Of course only if you display_errors , that's why it worked perfectly for @Danny's server) (当然,只有当你display_errors 时,这就是为什么它对@Danny 的服务器工作完美)
Use this code in captacha.php, You can try this: 在captacha.php中使用此代码,您可以尝试以下操作:
<?php
@session_start();
header("Content-type: image/png");
$_SESSION["captcha"] = substr(md5(time()),0,5);
$im = imagecreate(110, 30);
$white = imagecolorallocate($im, 244, 255, 255);
$red = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$size = $_SESSION["captcha"];
$text = "$size";
$font = 'comic.TTF';
imagettftext($im, 20, 0, 25, 20, $red, $font, $text);
imagettftext($im, 20, 0, 25, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im);
?>
You need a font like comic.TTF
for image font style. 您需要像
comic.TTF
这样的字体作为图像字体样式。
More details about Captcha in PHP 有关PHP中的Captcha的更多详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.