验证码和标头有问题 (PHP)

发布于 2024-08-04 14:20:54 字数 967 浏览 1 评论 0原文

我正在尝试向其他人制作的网站添加验证码。 我一直在关注这个 教程,如果我将其放在单独的文件中,它就可以正常工作(因此这不是服务器设置的问题)

但是,当我尝试将其添加到现有页面时,它根本不起作用。当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪是无色的血 2024-08-11 14:20:54

这些随机字符实际上是 PNG 文件的内容。

发生的情况是您将 PNG 数据转储到 HTML 文件中,而不是使用 标记链接到它。

您需要将代码放入其自己的文件中并像这样嵌入:

<img src="image.php">

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:

<img src="image.php">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文