在用户的机器上提供图像和保存cookie


Serving images and saving cookies on the user's machine

我知道当用户从我的服务器加载图像时,可以通过更改apache设置在用户的机器上植入cookie。

然而,我想知道是否有可能在HTML代码中包含一个图像,其中将有参数,我可以激活一些脚本来记录这些参数,例如:

<img src="http://www.mysite.com/myimage.jpg?somcode=123&customer=abcd" />

作为加载图片的回报,我想在用户的机器上保存一个cookie, somecode=123customer=abcd,并将此信息保存在我的数据库中。

这可能吗?

谢谢,

文件名不使用。jpg,而使用。php。这样你就不需要修改服务器配置了,正常的图像文件仍然可以正常使用:

<img src="http://www.mysite.com/myimage.php?somcode=123&customer=abcd" />
然后你的myimage.php文件:
<?php
// Your tracking and processing code here
$customer = $_GET['customer'];
// Then, either:
header("Location: /urlpath/to/the/actual/image.jpg"); /* Option 1 */
// OR:
header("Content-Type: image/jpeg");
readfile("/filepath/to/the/actual/image.jpg"); /* Option 2 */

我更喜欢选项1,因为它允许您的web服务器非常有效地提供实际图像,而不是PHP(选项2)。

我很确定你可以这样做,只要让apache把。jpg扩展名当作php文件。

你可以在你的。htaccess文件的特定文件夹中添加以下内容(不要把它放在根目录中,否则它会这样对待所有的jpg);

AddType application/x-httpd-php .php .jpg