简体   繁体   English

如何在PHP中跨域使用像素跟踪

[英]How to use Pixel Tracking across domains in PHP

I understand the basics of pixel tracking, I'm able to insert a pixel that references an image on my tracking domain to other websites. 我了解像素跟踪的基础知识,我能够将跟踪域中引用图像的像素插入其他网站。

However, how do I increment that actual pixel count on my tracking domain? 但是,如何在跟踪域上增加实际像素数? Is there some kind of log that tells me every time that pixel image was served up? 是否有某种日志告诉我每次提供像素图像? Am i able to do something like: 我可以做类似的事情:

<img src="http://www.foo.com/serveImage/getImage.php?id=5123" />

then have the getImage page, serve up the image, and increment based on the id that was passed in? 然后有getImage页面,提供图像,并根据传入的id递增? or is there a better way to achieve this? 或者有更好的方法来实现这一目标吗?

Thank you in advance. 先感谢您。

if you want to just output a gif this is a quick simple way, just make sure your script doesn't output anything else before or after: 如果您只想输出一个gif,这是一种快速简单的方法,只需确保您的脚本在之前或之后不输出任何其他内容:

header("Content-type: image/gif");
header("Content-length: 43");
$fp = fopen("php://output","wb");
fwrite($fp,"GIF89a\x01\x00\x01\x00\x80\x00\x00\xFF\xFF",15);
fwrite($fp,"\xFF\x00\x00\x00\x21\xF9\x04\x01\x00\x00\x00\x00",12);
fwrite($fp,"\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02",12);
fwrite($fp,"\x44\x01\x00\x3B",4);
fclose($fp);

Yes you have the right idea. 是的,你有正确的想法。

You give each site or page a unique ID, which is then passed in the image src. 您为每个站点或页面提供一个唯一的ID,然后在图像src中传递。 So in your example the ID is 5123. 所以在你的例子中ID是5123。

In your getImage.php script then use this ID to increment the database (or however you record the data) and return any image that you want to. 在getImage.php脚本中,然后使用此ID增加数据库(或者记录数据)并返回您想要的任何图像。 If you want the image you return to show the number of hits you can create an image on the fly with the GD extention - see the PHP manual for more information on it. 如果您希望返回的图像显示可以使用GD扩展动态创建图像的命中数 - 请参阅PHP手册以获取更多信息。

this is my track code: 这是我的跟踪代码:

<?php

$id = $_GET['site_id'];

// do track

$imageFile = 'images/pixel.jpg';
$im = imagecreatefromjpeg($imageFile);
header('Content-type: image/jpeg');
imagejpeg($im);


?>

Kind of a tangential answer, but too long for a comment: 这是一个切向答案,但评论太长了:

You don't necessarily need to increment anything, depending on how you implement it. 您不一定需要增加任何内容,具体取决于您实现它的方式。 If you're aiming for making it super-fast, just relying on the server request logs should suffice. 如果您的目标是超速运行,那么仅依靠服务器请求日志就足够了。 Every request for "getImage.php?q=5123" will already be there, you just need to pluck out the relevant info from the querystring. 每个请求“getImage.php?q = 5123”都已经存在,你只需要从查询字符串中提取相关信息。

You can parse the logs into a nice, query-able database later (via cron et al), out of band where it won't affect serving up the tracking bugs. 您可以稍后将日志解析为一个好的,可查询的数据库(通过cron等人),带外不会影响提供跟踪错误。 Doing it all in one shot is a little more elegant, but if you're handling a lot of requests, the logs are already there anyway. 一次性完成所有这一切都更优雅,但如果你处理了很多请求,那么日志已经存在了。

Bonus: server logs also have referrers & timestamps, so you can more easily see if someone is directly hammering getImage.php or linking to it from elsewhere to game the numbers, should those numbers be worth something. 奖励:服务器日志也有引用和时间戳,所以你可以更容易地看到有人直接锤击getImage.php或从其他地方链接到游戏数字,如果这些数字是值得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM