开发者

Detect when image (or any type of file) is accessed from site and notify via email

I plan on adding a logo image to an email newsletter and use it as a tracking image. This image wil开发者_StackOverflow中文版l be hosted on my site (eg. my_logo.gif) and referenced in the html of the email (eq. <img src="http://www.example.com/my_logo.gif" />). I would like to create a script in php that emails me when that image is accessed/viewed. Is this possible? If so, how?

All ideas and suggestions to complete this are welcome.


Put something like this in an .htaccess file in the image directory:

RewriteEngine On
RewriteRule ^(.+\.jpg)$ image.php?image=$1 [nocase]

Put something like this into a file image.php in the same directory:

// send email here

header('Content-Type: image/jpeg');
readfile($_GET['image']);

This is only a simple example, make sure you validate the $_GET['image'] variable to make sure it's an image in the same directory, otherwise someone might be able to abuse this script to read files in other directories (image.php?image=../../../passwords).


//Display image
$image=imagecreatefromjpeg('my_logo.jpg');
header('Content-Type: image/jpeg');
imagejpeg($image);

//logging or notifying stuff here
// ...

and just link to this script like <img src="script.php" />


You can redirect the request to an .jpg file via .htaccess and count it etc.


You can achieve this by using the readfile() function and sending the proper content-type headers. You can execute other program code before or after the image is sent to the browser.

If the following program code has a long execution time, you will want to look into the output control functions.


It would probably be easier to link to the php file and have that forward or present the image itself AND count/track/email you whenever the php file is accessed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜