Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP验证码不会显示图像_Php_Captcha - Fatal编程技术网

PHP验证码不会显示图像

PHP验证码不会显示图像,php,captcha,Php,Captcha,我编写了这个代码,它应该向我显示一个带有文本的图像,但图像就是不显示。有什么想法吗 <?php header("Content-type: image/png"); $md5 = md5(microtime() * mktime()); $string = substr($md5,0,5); $captcha = imagecreatefrompng('bg.png'); $black = imagecolorallocate($cap

我编写了这个代码,它应该向我显示一个带有文本的图像,但图像就是不显示。有什么想法吗

<?php

    header("Content-type: image/png"); 
    $md5 = md5(microtime() * mktime()); 

    $string = substr($md5,0,5);

    $captcha = imagecreatefrompng('bg.png'); 

    $black = imagecolorallocate($captcha, 0, 0, 0); 
    $line = imagecolorallocate($captcha,233,239,239); 

    imageline($captcha,0,0,39,29,$line); 
    imageline($captcha,40,0,64,29,$line);

    imagestring($captcha, 5, 20, 10, $string, $black); 

    $_SESSION['key'] = md5($string); 

    imagepng($captcha);
    imagedestroy($captcha);

?>


检查您的PHP错误日志文件,在我的本地主机上,我发现您的代码有几个错误。

您不能使用
mktime
,这样您会得到以下错误
严格的标准:mktime()[function.mktime]
,它不会显示图像

替换

$md5 = md5(microtime() * mktime());

你的产出


此脚本在我的本地主机上正常工作。请检查文件夹中是否存在
bg.png
。因为如果没有找到bg.png,它将生成错误

谢谢

这对我也有用。 您应该检查图像的路径

使用:

$captcha = imagecreatefrompng(dirname(__FILE__) . '/bg.png'); 
而不是:

$captcha = imagecreatefrompng('bg.png');

这个代码对我有用。尝试注释header函数,查看PHP是否显示任何错误您也可以查看web服务器错误日志,以检查PHP错误(如果PHP.ini中启用了
log\u errors
),是否安装了GD PHP扩展?在Debian/Ubuntu中:
apt get install php5 gd
thank you guys修复了它,他的代码在mktime()函数中出现了一些问题。只是用了一种不同的方法来生成一个随机数:yap在时间之前修复了它,只需删除标题即可查看错误。非常感谢。
$captcha = imagecreatefrompng('bg.png');