开发者

Why does this PHP tracking pixel not working correctly?

开发者 https://www.devze.com 2023-03-17 05:23 出处:网络
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):

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.g开发者_如何学运维if");
// 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);


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:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^1x1\.gif$ tracking_script.php [NC,L]

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?

header("Content-type: image/gif");
echo file_get_contents("/path/to/1by1.gif");
// do tracking stuff below here


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.


$img = imagecreatefromstring(file_get_contents("/path/to/1by1.gif"));
imagegif($img);

This code

  1. Reads in a GIF image
  2. Passes the image bytes to GD
  3. Asks GD to write a new image.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号