-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpixel.php
More file actions
104 lines (82 loc) · 3.07 KB
/
Copy pathpixel.php
File metadata and controls
104 lines (82 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/*
Open tracker for email with external CSS pixel option.
https://freshinbox.com/blog/how-to-build-your-own-email-open-tracking-pixel/
Parameters:
- p: A unique value to track.
- ext_css (optional): Generate CSS that loads an "external CSS pixel".
- css_pixel: Auto generated by ext_css option that records an external CSS pixel open.
Logging notes:
- Request contains only "p", it will be logged as "Regular pixel fetched".
- Request contains "ext_css", it will be logged as "External CSS fetched".
- Request contains "css_pixel", it will be logged as "CSS pixel fetched".
To use regular pixel:
<img src="https://myserver.com/path/to/pixel.php?p=mypixel" width=1>
To use External CSS pixel (only for email clients that support external CSS):
<link rel="stylesheet" href="https://myserver.com/path/to/pixel.php?p=mypixel&ext_css=1">
*/
date_default_timezone_set('America/Los_Angeles');
// You may need to pre-create pixel.log and set writable permissions
$LOG_FILE = "pixel.log";
$CSS_DEBUG = 0; // Setting to 1 will show "style sheet loaded" when using ext_css=1
/*
Get the parameters from the url
ie. https://myserver.com/path/to/pixel.php?p=mypixel
*/
$p = @$_REQUEST['p'];
$ext_css = @$_REQUEST['ext_css']; // generate the CSS for an external CSS pixel (optional)
$css_pixel = @$_REQUEST['css_pixel']; // log an open by the css pixel
if(!$p){
log_request("Test");
die("The pixel is live!");
} elseif($css_pixel){ // preceed $ext_css since $ext_css will eval to true as well
log_request("CSS pixel fetched");
send_image_pixel();
die();
} elseif($ext_css){
log_request("External CSS fetched");
send_css_pixel_stylesheet();
die();
} else {
log_request("Regular pixel fetched");
send_image_pixel();
die();
}
// Log the request
function log_request($note){
global $LOG_FILE;
$p = @$_REQUEST['p'] ?: 'not_set';
$date = new DateTime();
$dateval = $date->format('Y-m-d H:i:s');
$ip = @$_SERVER['REMOTE_ADDR']; // Remote IP address
$agent = @$_SERVER['HTTP_USER_AGENT']; // User Agent
$referer = @$_SERVER['HTTP_REFERER']; // Referer
$list = [$dateval, $p, $note, $ip, $agent, $referer];
$file = fopen($LOG_FILE,"a");
fputcsv($file,$list);
fclose($file);
}
function send_image_pixel(){
// https://stackoverflow.com/questions/3203354/php-script-to-render-a-single-transparent-pixel-png-or-gif/3203394
header('Content-type: image/gif');
header('Cache-Control: no-cache, no-store, must-revalidate');
echo (hex2bin('47494638396101000100900000ff000000000021f90405100000002c00000000010001000002020401003b'));
}
function send_css_pixel_stylesheet(){
global $CSS_DEBUG;
$pixel_url = "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}&css_pixel=1";
$response = "
body {
background-image:url('{$pixel_url}') !important;
}
";
if ($CSS_DEBUG) {
$response .= "
/* for debugging in email */
body::after{content:'style sheet retrieved';background-color:yellow;}";
}
header('Content-type: text/css');
header('Cache-Control: no-cache, no-store, must-revalidate');
echo $response;
}
?>