验证码和标头有问题 (PHP)
我正在尝试向其他人制作的网站添加验证码。 我一直在关注这个 教程,如果我将其放在单独的文件中,它就可以正常工作(因此这不是服务器设置的问题)
但是,当我尝试将其添加到现有页面时,它根本不起作用。当我在 Internet Explorer 中加载页面时,源代码在应该显示图像的地方显示随机字符,例如:
‰PNGIHDRé1°8ö[IDATxœÍ]kp×u>»X‹± (4 ›–9¦:‰-C£V™©4–[ÓL•4“Dm~„njR3ª]*qÒÚ‰£ŽãD–&~Ô±ØØŽ$
在 Firefox 中,我收到消息:无法显示图像“myurl”,因为它包含错误。
我假设这与标题有关,但我不太确定。
这是我用来创建图像的代码:
$md5 = md5(microtime() * mktime());
$string = substr($md5,0,5);
$captcha = imagecreatefrompng("./captcha.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);
header("Content-type: image/png");
imagepng($captcha);
任何建议将不胜感激。谢谢。
I'm trying to add a captcha to a site somebody else has made.
I've been following this tutorial and if I make it in a separate file, it works just fine (so it's not an issue with the server setup)
However, when I try to add it to an existing page, it's not working at all. When I load the page in Internet Explorer, the source code is displayed with random characters where the image should have been displayed such as:
‰PNGIHDRé1°8ö[IDATxœÍ]kp×u>»X‹± (4 ›–9¦:‰-C£V™©4–[ÓL•4“Dm~„njR3ª]*qÒÚ‰£ŽãD–&~Ô±ØØŽ$
In Firefox, I get the message: The image “myurl” cannot be displayed, because it contains errors.
I'm assuming this is something to do with the headers, but I'm not really sure.
This is the code I'm using to create the image:
$md5 = md5(microtime() * mktime());
$string = substr($md5,0,5);
$captcha = imagecreatefrompng("./captcha.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);
header("Content-type: image/png");
imagepng($captcha);
Any advice would be greatly appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些随机字符实际上是 PNG 文件的内容。
发生的情况是您将 PNG 数据转储到 HTML 文件中,而不是使用
标记链接到它。
您需要将代码放入其自己的文件中并像这样嵌入:
Those random characters are actually the contents of a PNG file.
What's happening is you're dumping the PNG data into your HTML file, rather than linking to it with an
<img>
tag.You need to put the code into its own file and embed it like this: