This repository was archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount.class.php
148 lines (140 loc) · 5.74 KB
/
count.class.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
<?php
class Count {
protected $db,
$htmlpath;
public $page;
function __construct($host,$user,$password,$db,$htmlpath = "") {
$dsn = 'mysql:dbname='.$db.';host='.$host ;
try{
$this->db = new PDO($dsn, $user, $password);
}catch (PDOException $e){
return false;
}
$this->htmlpath = $htmlpath;
return true;
}
//Get a name based off of the page loading it.
public function getSuggestedName(){
$filename = $_SERVER["SCRIPT_FILENAME"];
$filename = str_replace(".php","",$filename);
$filename = str_replace($this->htmlpath,"",$filename);
$filename = str_replace("/","_",$filename);
return $filename;
}
//Get the number of hits.
public function getHits($Page = null){
$Page = $Page;
if($Page == null){
$Page = $this->getSuggestedName();
}elseif(strlen($Page) == 0){
$Page = $this->getSuggestedName();
}
if ($getHitsQuery = $this->db->prepare("SELECT hitcount FROM hits WHERE page=:page")){
$getHitsQuery->bindParam(':page', $Page);
$getHitsQuery->execute();
$results = $getHitsQuery->fetch(PDO::FETCH_ASSOC);
return $results['hitcount'];
}else{
return false;
}
}
//Get reffering ip
public function getUserIP(){
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP)){
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP)){
$ip = $forward;
}
else{
$ip = $remote;
}
return $ip;
}
//Check if the vistior has been here within the last week.
public function hasVisited($Page){
$Ref = $this->getUserIP();
if(!filter_var($Ref, FILTER_VALIDATE_IP)){
throw new Exception('Did not get valid ip');
return;
}
if ($getVisted = $this->db->prepare("SELECT time_visited FROM history WHERE ip=:ip AND page=:page")){
$getVisted->bindParam(':ip', $Ref);
$getVisted->bindParam(':page', $Page);
$getVisted->execute();
$results = $getVisted->fetch(PDO::FETCH_ASSOC);
$hits = $getVisted->rowCount();
if($hits > 0){
$stored = new DateTime($results['time_visited'] );
$current = new DateTime();
$diff = $current->diff($stored);
$diff = abs($diff->format('%R%a'));
if(7 <= $diff){
$delete = $this->db->prepare("DELETE FROM history WHERE ip=:ip AND page=:page");
$delete->bindParam(':ip', $Ref);
$delete->bindParam(':page', $Page);
$delete->execute();
return false;
}else{
return true;
}
}else{
return false;
}
}
return false;
}
//Add a page hit.
public function addHit($Page = null){
if($Page == null){
$Page = $this->getSuggestedName();
}elseif(strlen($Page) == 0){
$Page = $this->getSuggestedName();
}
$visited = $this->hasVisited($Page);
if($visited === false){
$currentCount = $this->db->prepare("SELECT hitcount FROM hits WHERE page=:page");
$currentCount->bindParam(':page',$Page);
$currentCount->execute();
$found = $currentCount->rowCount();
if($found > 0){
$updateCount = $this->db->prepare("UPDATE hits SET hitcount= hitcount + 1 WHERE page=:page");
$updateCount->bindParam(":page",$Page);
if(!$updateCount->execute()){
throw new Exception('Unable to update hits table!');
}
//Update history table to prevent someone from refreshing the page
$addIPHistory = $this->db->prepare("INSERT INTO history (time_visited, ip, page) VALUES (:time, :ip, :page)");
$addIPHistory->bindParam(":page",$Page);
$IPaddr = $this->getUserIP();
$addIPHistory->bindParam(":ip",$IPaddr);
$time = new DateTime();
$time = $time->format('Y-m-d');
$addIPHistory->bindParam(":time",$time);
if(!$addIPHistory->execute()){
throw new Exception('Unable to update history table!');
}
}else{
//Update history table to prevent someone from refreshing the page
$addIPHistory = $this->db->prepare("INSERT INTO history (time_visited, ip, page) VALUES (:time, :ip, :page)");
$addIPHistory->bindParam(":page",$Page);
$addIPHistory->bindParam(":ip",$this->getUserIP());
$time = new DateTime();
$time = $time->format('Y-m-d');
$addIPHistory->bindParam(":time",$time);
if(!$addIPHistory->execute()){
throw new Exception('Unable to update history table!');
}
//Create row in page table.
$createPageRow = $this->db->prepare("INSERT INTO hits (page, hitcount) VALUES (:pages, 1)");
$createPageRow->bindParam(":pages",$Page);
if(!$createPageRow->execute()){
throw new Exception('Unable to insert new page into hits table!');
}
}
}
}
}