-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.php
30 lines (26 loc) · 1.01 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
<?php
// CHECK IF PAGE EXISTS IN PAGE HIT TABLE
function checkPageName($page_name){
$sql = "SELECT * FROM ".$GLOBALS['hits_table_name']." WHERE page = :page";
$query = $GLOBALS['db']->prepare($sql);
$query->execute([':page' => $page_name]);
if ($query->rowCount() == 0){
$sql = "INSERT INTO ".$GLOBALS['hits_table_name']." (page, count) VALUES (:page, 0)";
$query = $GLOBALS['db']->prepare($sql);
$query->execute([':page' => $page_name]);
}
}
// UPDATE PAGE HIT COUNT
function updateCounter($page_name){
checkPageName($page_name);
$sql = "UPDATE ".$GLOBALS['hits_table_name']." SET count = count+1 WHERE page = :page";
$query = $GLOBALS['db']->prepare($sql);
$query->execute([':page' => $page_name]);
}
// UPDATE VISITOR INFO
function updateInfo(){
$sql = "INSERT INTO ".$GLOBALS['info_table_name']." (ip_address, user_agent) VALUES(:ip_address, :user_agent)";
$query = $GLOBALS['db']->prepare($sql);
$query->execute([':ip_address' => $_SERVER["REMOTE_ADDR"], ':user_agent' => $_SERVER["HTTP_USER_AGENT"]]);
}
?>