-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccessLogger.php
70 lines (59 loc) · 2.1 KB
/
AccessLogger.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
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
// Include and instantiate the class.
require_once 'Mobile_Detect.php';
class AccessLogger
{
private $sql;
private $table;
private $apiKey;
// Make them visible in case other portions of code needs them
public $IP;
public $Detect;
public $Headers;
public $Geolocation;
public $UserAgent;
// The table is needed because each page has its own
// instead of one enormous one with all pages
function __construct ($table) {
// Database connection
require_once 'login.php';
$this->sql = new mysqli($hostName, $userName, $passWord, $dataBase);
if ($this->sql->connect_error) {
die($this->sql->connect_error);
}
$this->table = $table;
$this->apiKey = $apiKey;
}
function log() {
// Getting information
$this->Detect = new Mobile_Detect;
$device = "";
// First load the page
if($this->Detect->isMobile() && !$this->Detect->isTablet()) {
$device = "mobile";
} else {
$device = "PC";
}
// Just check if tablet
if($this->Detect->isTablet()) {
$device = "tablet";
}
$this->IP = $_SERVER['REMOTE_ADDR'];
$this->Headers = apache_request_headers();
$this->UserAgent = preg_replace("/\'/", "\'", $this->Headers['User-Agent']);
$geolocate = json_decode(file_get_contents("http://api.ipstack.com/$this->IP?access_key=$this->apiKey&output=json&legacy=1"), true);
if (isset($geolocate['country_name'])) {
$this->Geolocation = preg_replace("/\'/", "\'", $geolocate['country_name'] . ', ' . $geolocate['region_name'] . ', ' . $geolocate['city']);
} else {
$this->Geolocation = '';
}
$query = "INSERT INTO `$this->table` VALUES(NULL, NULL, '$this->IP', '$device', '$this->Geolocation', '$this->UserAgent')";
if (!$this->sql->query($query)) {
echo "Could not insert into database: " . $this->sql->error;
}
$this->sql->close();
}
}
?>