/
pixel.php
176 lines (124 loc) · 2.58 KB
/
pixel.php
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/*
Functions -
Get the following information :
- country
- referrer
- date
- page 1 or 2
Check if the record for the referrer exists on same date
If yes, update
If no, create
*/
include("connection.php");
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('PST');
function checkifexist($ref,$date)
{
global $db;
$db->query("SELECT * FROM data WHERE ref='$ref' AND date='$date'");
if($db->count()==0){
return false;
}
else{
return $db->result_array();
}
}
function ccupdate($data,$cc)
{
$dd=json_decode($data);
if($dd->$cc>0){
$dd->$cc++;
}
else
{
$dd->$cc=1;
}
return json_encode($dd);
}
function updater($data,$ref,$date,$cc,$page)
{
//var_dump($data);
global $db;
if($page==1)
{
if($data[0]['t1']==""){
$data[0]['t1']=1;
$arr[$cc] = 0;
$cc = ccupdate(json_encode($arr),$cc);
}
else {
$data[0]['t1']++;
$cc = ccupdate($data[0]['geo1'],$cc);
}
$t11=$data[0]['t1'];
$cc = mysql_real_escape_string($cc);
$sno = $data[0]['sno'];
$db->query("UPDATE data SET t1='$t11', geo1='$cc' WHERE sno='$sno'");
}
if($page==2)
{
if($data[0]['t2']==""){
$data[0]['t2']=1;
$arr[$cc] = 0;
$cc = ccupdate(json_encode($arr),$cc);
}
else {
$data[0]['t2']++;
$cc = ccupdate($data[0]['geo2'],$cc);
}
$t11=$data[0]['t2'];
$cc = mysql_real_escape_string($cc);
$sno = $data[0]['sno'];
$db->query("UPDATE data SET t2='$t11', geo2='$cc' WHERE sno='$sno'");
}
}
function creater($ref,$date,$cc,$page)
{
global $db;
$arr[$cc] = 0;
$cc = ccupdate(json_encode($arr),$cc);
if($page==1)
{
$db->query("INSERT into data(ref,date,geo1,t1) values('$ref','$date','$cc','1')");
}
else
{
$db->query("INSERT into data(ref,date,geo2,t2) values('$ref','$date','$cc','1')");
}
}
if(isset($_GET['r'])){
// fetching all the required parameters
if(isset($_SERVER["HTTP_CF_IPCOUNTRY"])){
$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"]; // to access in PHP
}
else{
$country_code ="UNWN";
}
//$country_code = "CA"; // to access in PHP
$referrer = $_GET['r'];
$date = date("Ymd");
$page = '1';
$data=checkifexist($referrer,$date);
if($data!=false)
{
updater($data,$referrer,$date,$country_code,$page);
}
else
{
creater($referrer,$date,$country_code,$page);
}
}
// Create an image, 1x1 pixel in size
$im=imagecreate(1,1);
// Set the background colour
$white=imagecolorallocate($im,255,255,255);
// Allocate the background colour
imagesetpixel($im,1,1,$white);
// Set the image type
header("content-type:image/jpg");
// Create a JPEG file from the image
imagejpeg($im);
// Free memory associated with the image
imagedestroy($im);
?>