-
Notifications
You must be signed in to change notification settings - Fork 2
/
counter.php
481 lines (264 loc) · 9.07 KB
/
counter.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<?php
/*----------------------------
-------- ++ simPHP ++ --------
A simple PHP hit and visitor counter
Version: 2.0.1 - Refined by Zillinium
Description:
this counts both regular and unique views on multiple
webpages and returns the Loads count, Visitors count and
the current users IP Address.
The stats can be displayed on any PHP-enabled
webpage or any HTML page using a GET method.
INSTALLING INFO
- Copy this file to desired path on website
- Make a counter.txt file in the same path as this
file, it will be used for the log.
- You can access it from <script> with GET 'show=this'
- this way the script can be included in .html files also.
EXAMPLE USAGE CODE
<script type="text/javascript" src="counter.php?show=this"></script>
OPTIONAL .HTACCESS .PHP REMOVAL
Add to .htaccess file
# PHP REMOVAL - HUP.php becomes HUP
# EXAMPLE : HUP?url=http... over HUP.php?url=
#
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
#
# RETURN 404 IF REQUEST IS .PHP
#
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]
#
IF YOU USE HTACCESS, This method sometimes has unexpected results when used with other .htaccess RewriteRule's
also the new example usage would become,
EXAMPLE USAGE CODE
<script type="text/javascript" src="counter?show=this"></script>
For more .htaccess tweaks https://pastebin.com/6FxeZwna
You MUST have read/write permissions on files
Script refined for html support by :
Zillinium : http://facebook.com/Zillinium
Original PHP Script (simPHP) by Ajay: me@ajay.ga
----------------------------*/
/*----------CONFIG----------*/
function realIp()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$ip = realIp();
// Relative URL of text file that holds hit info:
$lf_name = "counter.txt";
// Save new log file each month
// 0 = No
// 1 = Yes
$monthly = 0;
// Path to store old files:
// Default for June, 2012:
// oldfiles/6-12.txt
$monthly_path = "oldfiles";
// Count unique hits or total hits:
// 0 = Total hits
// 1 = Unique hits
// 2 = Both unique and total
$type = 2;
// Text to display
// before total hits
$beforeTotalText = "Load : ";
// Before unique hits
$beforeUniqueText = "Visitor : ";
// Before current user IP
$beforeIP = "You are : ";
// Display hits on this page:
// 0 = No
// 1 = Yes
$display = 1;
// Only change this if you are recording both values
// Separator for unique and total hits display - use HTML tags! (line break is default)
$separator = "<br \>";
// Default would output:
// Visits: 10
// Unique Visits: 10
/*--------------------------*/
/*--------BEGIN CODE--------*/
$log_file = dirname(__FILE__) . '/' . $lf_name;
// Check for "?display=true" in URL
if ($_GET['display'] == "true") {
// Show include() info
die("<pre><? include(\"" . dirname(__FILE__) . '/' . basename(__FILE__) . "\"); ?></pre>");
} else {
// Get visitor IP
$uIP = realIp();
// Check for "hits.txt" file
if (file_exists($log_file)) {
// Get contents of log file
$log = file_get_contents($log_file);
if ($monthly) {
// Check if today is first day of month
// Also check if prev month log file exists already
$prev_name = $monthly_path . '/' . date("n-Y", strtotime("-1 month")) . '.txt';
if (date('j') == 1 && !file_exists($prev_name)) {
// If it is first day of month,
// move previous log file to subdir and create new file
// Ensure that monthly dir exists
if (!file_exists($monthly_path)) {
mkdir($monthly_path);
}
copy($log_file, $prev_name);
// Write new data based on config
if ($type == 0) {
// Total hits
$toWrite = "1";
$info = $beforeTotalText . "1";
} else if ($type == 1) {
// Unique hits
$toWrite = "1;" . $uIP . ",";
$info = $beforeUniqueText . "1";
} else if ($type == 2) {
// Unique and total
$toWrite = "1;1;" . $uIP . ",";
$info = $beforeTotalText . "1" . $separator . $beforeUniqueText . "1" . $separator . $beforeIP . $ip;
}
write_logfile($toWrite, $info);
} else {
// Still same month as before, so just increment counters
// What to do depends on type from config
if ($type == 0) {
// Total hits
// Create info to write to log file and info to show
$toWrite = intval($log) + 1;
$info = $beforeTotalText . $toWrite;
} else if ($type == 1) {
// Separate log file into hits and IPs
$hits = reset(explode(";", $log));
$IPs = end(explode(";", $log));
$IPArray = explode(",", $IPs);
// Check for visitor IP in list of IPs
if (array_search($uIP, $IPArray, true) === false) {
// IP doesnt' exist so increase hits and include IP
$hits = intval($hits) + 1;
$toWrite = $hits . ";" . $IPs . $uIP . ",";
} else {
// If IP exists don't change anything
$toWrite = $log;
}
// Info to show
$info = $beforeUniqueText . $hits;
} else if ($type == 2) {
// Both total hits and unique hits
// Separate log file into regular hits, unique hits, and IPs
$pieces = explode(";", $log);
$totalHits = $pieces[0];
$uniqueHits = $pieces[1];
$IPs = $pieces[2];
$IPArray = explode(",", $IPs);
// Always increase regular hits, regardless of IP
$totalHits = intval($totalHits) + 1;
// Search for visitor IP in list of IPs
if (array_search($uIP, $IPArray, true) === false) {
// IP doesn't exist so increase unique hits and append IP
$uniqueHits = intval($uniqueHits) + 1;
$toWrite = $totalHits . ";" . $uniqueHits . ";" . $IPs . $uIP . ",";
} else {
// If IP already exists just keep unique hits unchanged
$toWrite = $totalHits . ";" . $uniqueHits . ";" . $IPs;
}
// Info to show
$info = $beforeTotalText . $totalHits . $separator . $beforeUniqueText . $uniqueHits . $separator . $beforeIP . $ip;
}
write_logfile($toWrite, $info);
}
} else {
// What to do depends on type from config
if ($type == 0) {
// Total hits
// Create info to write to log file and info to show
$toWrite = intval($log) + 1;
$info = $beforeTotalText . $toWrite;
} else if ($type == 1) {
// Separate log file into hits and IPs
$hits = reset(explode(";", $log));
$IPs = end(explode(";", $log));
$IPArray = explode(",", $IPs);
// Check for visitor IP in list of IPs
if (array_search($uIP, $IPArray, true) === false) {
// IP doesnt' exist so increase hits and include IP
$hits = intval($hits) + 1;
$toWrite = $hits . ";" . $IPs . $uIP . ",";
} else {
// If IP exists don't change anything
$toWrite = $log;
}
// Info to show
$info = $beforeUniqueText . $hits;
} else if ($type == 2) {
// Both total hits and unique hits
// Separate log file into regular hits, unique hits, and IPs
$pieces = explode(";", $log);
$totalHits = $pieces[0];
$uniqueHits = $pieces[1];
$IPs = $pieces[2];
$IPArray = explode(",", $IPs);
// Always increase regular hits, regardless of IP
$totalHits = intval($totalHits) + 1;
// Search for visitor IP in list of IPs
if (array_search($uIP, $IPArray, true) === false) {
// IP doesn't exist so increase unique hits and append IP
$uniqueHits = intval($uniqueHits) + 1;
$toWrite = $totalHits . ";" . $uniqueHits . ";" . $IPs . $uIP . ",";
} else {
// If IP already exists just keep unique hits unchanged
$toWrite = $totalHits . ";" . $uniqueHits . ";" . $IPs;
}
// Info to show
$info = $beforeTotalText . $totalHits . $separator . $beforeUniqueText . $uniqueHits . $separator . $beforeIP . $ip;
}
write_logfile($toWrite, $info);
}
} else {
// If "hits.txt" doesn't exist, create it
$fp = fopen($log_file, "w");
fclose($fp);
// Write file according to config above
if ($type == 0) {
$toWrite = "1";
$info = $beforeTotxalText . "1";
} else if ($type == 1) {
$toWrite = "1;" . $uIP . ",";
$info = $beforeUniqueText . "1";
} else if ($type == 2) {
$toWrite = "1;1;" . $uIP . ",";
$info = $beforeTotalText . "1" . $separator . $beforeUniqueText . "1" . $separator . $beforeIP . $ip;
}
write_logfile($toWrite, $info);
}
}
/**
* Writes given data to the logfile and echoes data if the option
* says so in config
* Requires: A string of data to write to the file and a string
* of data to print
*/
function write_logfile($data, $output) {
global $log_file;
// Put $toWrite in log file
file_put_contents($log_file, $data);
// Display info if is set in config
if ($display == 1) {
echo $output;
}
}
$outCode = '<div id="show">'. $info . '</div>';
if(isset($_GET['show']) && $_GET['show']=='this') $outCode = "document.write('$outCode');";
echo $outCode;
?>