我想在PHP中创建我的自定义Captcha验证,我已经编写了这段代码,但似乎没有用。图像没有被创建,我找不到错误的位置。
PHP:
<?php
session_start();
$string = '';
for ($i = 0; $i < 5; $i++) {
// this numbers refer to numbers of the ascii table (lower case)
$string .= chr(rand(97, 122));
}
$_SESSION['rand_code'] = $string;
//specify the path where the font exists
$dir = 'fonts/arial.ttf';
$image = imagecreatetruecolor(170, 60);
$black = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, 200, 100, 90); // red
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 30, 0, 10, 40, $color, $dir."arial.ttf", $_SESSION['random_code']);
header("Content-type: image/png");
imagepng($image);
?>
HTML:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<p><input type="text" name="name" /> Name</p>
<p><input type="text" name="email" /> Email</p>
<p><textarea name="message"></textarea></p>
<img src="captcha.php"/>
<p><input type="text" name="code" /> Are you human?</p>
<p><input type="submit" name="submit" value="Send" class="button" /></p>
</form>
答案 0 :(得分:1)
改变这个:
imagettftext ($image, 30, 0, 10, 40, $color, $dir."arial.ttf", $_SESSION['random_code']);
对此:
imagettftext ($image, 30, 0, 10, 40, $color, $dir, $_SESSION['random_code']);
您已在此处设置字体文件名:
$dir = 'fonts/arial.ttf';
答案 1 :(得分:1)
由于未定义的var ..
而生成警告更改此行
imagettftext ($image, 30, 0, 10, 40, $color, $dir."arial.ttf", $_SESSION['random_code']);
到这个
imagettftext ($image, 30, 0, 10, 40, $color, $dir."arial.ttf", $_SESSION['rand_code']);
还检查,如果字体文件确实可用且可读,或者您也会收到警告
答案 2 :(得分:0)
这是一个jquery验证验证码?
1.-您是否安装了GD库?
2.-文件名为.php或.jpg?如果命名为.jpg,您的服务器可以像.php文件一样执行.jpg文件吗?
其他推荐。你在尝试使用HoneyPot验证而不是验证码吗?
答案 3 :(得分:0)
首先,将会话var中的文本更改为某些硬编码字符串,例如
imagettftext ($image, 30, 0, 10, 40, $color, $dir , "sdfsfdsfd");
如果仍然无效,请删除
行header("Content-type: image/png");
并在浏览器中打开 captcha.php 的完整网址。它肯定会显示一些错误。