【发布时间】:2019-02-09 18:01:17
【问题描述】:
我的 PHP 验证码在我的 OVH 服务器上不起作用。
使用 wamp 在 localhost 上一切正常。 取消上传后,验证码根本不会显示在我的网站上。
我不知道我做错了什么。
我的 captcha.php 包含在我的表单中,例如:<img src="captcha.php" alt="CAPTCHA" class="captcha-image">
PS : php-gd 已经安装并且是最新的。 PHP版本:7.0.33
验证码.php:
session_start();
$permitted_chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
function generate_string($input, $strength = 10) {
$input_length = strlen($input);
$random_string = '';
for($i = 0; $i < $strength; $i++) {
$random_character = $input[mt_rand(0, $input_length - 1)];
$random_string .= $random_character;
}
return $random_string;
}
$image = imagecreatetruecolor(200, 50);
imageantialias($image, true);
$colors = [];
$red = rand(125, 175);
$green = rand(125, 175);
$blue = rand(125, 175);
for($i = 0; $i < 5; $i++) {
$colors[] = imagecolorallocate($image, $red - 20*$i, $green - 20*$i, $blue - 20*$i);
}
imagefill($image, 0, 0, $colors[0]);
for($i = 0; $i < 10; $i++) {
imagesetthickness($image, rand(2, 10));
$line_color = $colors[rand(1, 4)];
imagerectangle($image, rand(-10, 190), rand(-10, 10), rand(-10, 190), rand(40, 60), $line_color);
}
$black = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image, 255, 255, 255);
$textcolors = [$black, $white];
$fonts = [dirname(__FILE__).'\fonts\Acme.ttf', dirname(__FILE__).'\fonts\Ubuntu.ttf', dirname(__FILE__).'\fonts\Merriweather.ttf', dirname(__FILE__).'\fonts\PlayfairDisplay.ttf'];
$string_length = 6;
$captcha_string = generate_string($permitted_chars, $string_length);
$_SESSION['captcha_text'] = $captcha_string;
for($i = 0; $i < $string_length; $i++) {
$letter_space = 170/$string_length;
$initial = 15;
imagettftext($image, 24, rand(-15, 15), $initial + $i*$letter_space, rand(25, 45), $textcolors[rand(0, 1)], $fonts[array_rand($fonts)], $captcha_string[$i]);
}
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
本地:https://i.imgur.com/rqTK2X3.png 远程:https://i.imgur.com/Z4lEa9r.png
【问题讨论】:
-
您能否在(损坏的)验证码图像上右键单击,将
image link address复制并粘贴到此处(本地和远程) -
这里可能的情况是页面可能会在图像之前生成 php 警告/通知 - 因此浏览器正在获取损坏的图像。我倾向于在最后两行注释掉的情况下运行页面,并检查是否正在生成任何 php 警告,并添加 echo 'test';确保您到达文件的末尾 - 看看是否显示任何内容
-
@saibbyweb : localhost/captcha.php?1549736811543 远程 : example.com/captcha.php?1549733788640
-
@MantisSupport :与行评论相同的结果,我得到了回声测试