为什么这个 PHP 跟踪像素无法正常工作?
我正在使用 PHP 设置一个简单的像素跟踪脚本,下面的技术在技术上是有效的,但是当我在 Safari 中查看检查器时,我收到以下警告(1by1.gif 是 42B gif):
esource 被解释为文档,但是 使用 MIME 类型 image/gif 传输。
header("Content-type: image/gif");
header("Content-Length: 42");
echo file_get_contents("/path/to/1by1.gif");
// do tracking stuff below here
我查看了其他跟踪像素,它们都显示在检查器中,就好像它们是实际图像一样,即使具有 .php 扩展名。有什么想法如何解决该警告吗?
编辑:
我尝试执行以下操作,但收到相同的警告:
header("Content-type: image/gif");
$img = imagecreatefromstring(file_get_contents("/path/to/1by1.gif"));
imagegif($img);
I'm working on setting up a simple pixel tracking script with PHP, and the below technically works, but when I look at the inspector in Safari I get the following warning (1by1.gif is a 42B gif):
esource interpreted as document but
transferred with MIME type image/gif.
header("Content-type: image/gif");
header("Content-Length: 42");
echo file_get_contents("/path/to/1by1.gif");
// do tracking stuff below here
I've looked at other tracking pixels, and they all show in the inspector as if they are an actual image, even with the .php extension. Any ideas how to fix that warning?
EDIT:
I tried doing the following and I get the same warning:
header("Content-type: image/gif");
$img = imagecreatefromstring(file_get_contents("/path/to/1by1.gif"));
imagegif($img);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在 HTML 源代码中编写
1x1.gif
(或其他一些虚构的名称),然后让 Apache 实际提供 PHP 脚本。您可以使用.htaccess
执行此操作,方法如下:这样 Safari 就会看到
gif
扩展名,并且不会抱怨。You could write
1x1.gif
(or some other made up name) in your HTML source and then have Apache actually serve the PHP script. You can do this with.htaccess
with something along the lines of:This way Safari sees the
gif
extension and won't complain.嗯,这很有趣。如果我删除内容长度并仅使用以下内容,它似乎可以完美工作。有人知道为什么会这样吗?
Well this is interesting. If I remove the content-length and just use the following, it appears to work perfectly. Anyone know why that might be?
我是一个疑惑。函数 file_get_contents() 用于从文本文件中获取内容。你来这里的目的是什么?该函数以字符串或 false 形式返回内容。您的 echo 语句本质上传输的是正确解释为文档而不是 gif 的结果。
更新:我花了一段时间才重现此内容并看到警告。回显 file_get_contents() 确实会在浏览器中显示 gif,简单的 include() 也会显示警告。这个警告会给您带来任何麻烦还是这只是为了选美比赛?我只能推测 Safari 的 Inspector 有点挑剔。 Chrome 中的同一工具不会显示警告。
I'm a puzzled. Function file_get_contents() is for getting content from a text file. What is your intend here? The function returns the content as string or false. Your echo statement essentially transfers that result which is correctly interpreted as document and not a gif.
Update: Took me a while to even reproduce this and see the warning. Echoing the file_get_contents() shows indeed the gif in the browser, so does a simple include() which also shows the warning. Does this warning causes you any trouble or is this just for a beauty contest? I can only speculate that the Safari's Inspector is a little picky. The same tool in Chrome does not show a warning.
此代码
这里很多事情都可能出错。例如,将图像写回可能不会产生完全相同的字节流,也许更多,也许更少。这可能会使您的
Content-Length
标头无效,并且浏览器不喜欢您在此类内容上撒谎。或者其中一行中可能有一条通知或警告,它们将作为 GIF 数据之前的内容发出。这肯定看起来像一个“文档”,而不是 Webkit 的图像数据。通过
file_get_contents
/include
/echo
提供文件消除了通过 GD 进行过滤的步骤。如果代码在没有该步骤的情况下正常工作,则错误就在那里。This code
Many things could go wrong here. For example, writing the image back out might not produce the same exact stream of bytes, maybe more, maybe less. This could make your
Content-Length
header invalid, and browsers don't like it when you lie about such stuff. Or maybe there's a Notice or Warning in one of the lines, which would be emitted as content before the GIF data. That would certainly look like a "document" instead of as image data to Webkit.Serving the file through
file_get_contents
/include
/echo
eliminates the filter-through-GD step. If the code works properly without that step, the error was somewhere there.