-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.visitors.php
More file actions
114 lines (100 loc) · 2.52 KB
/
class.visitors.php
File metadata and controls
114 lines (100 loc) · 2.52 KB
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
<?php
/*
This SQL query will create the table to store your object.
CREATE TABLE visitors (
`time` time NOT NULL default '00:00:00',
`ip` int(4) unsigned NOT NULL default '0'
) ENGINE=MyISAM;";
*/
/**
* <b>Online Vistors Count</b> class.
* @author Alfredo Sanchez
* @version 1.0 / PHP4
* @see http://www.tildemark.com/programming/php
* @copyright Free for personal & commercial use.
*/
class visitors
{
/**
* @var tinyint
*/
var $interval = 300; // 5 minutes interval
/**
* @var tinyint
*/
var $visitors = 0; // online visitors
/**
* @var int
*/
var $hits = 0; // online impressions
/**
* Function that inserts the visitors IP address into the database.
* If the IP address is a NULL value then assume its from the localhost.
* Returns the row id of the inserted data from the database.
*
* @return integer $id
*/
function insert()
{
global $REMOTE_ADDR;
if($REMOTE_ADDR == NULL) $REMOTE_ADDR = "127.0.0.1";
$ip = ip2long($REMOTE_ADDR);
$db = new Database();
$connection = $db->Connect();
$query = "INSERT INTO `visitors` (time, ip) VALUES (NOW(), $ip)";
$id = $db->InsertOrUpdate($query, $connection);
return $id;
}
/**
* Function that deletes all entries that older than the $interval value.
* Returns a boolean value base on the success of the deletion.
*
* @param none
* @return integer $id
*/
function clean()
{
$db = new database();
$connection = $db->Connect();
$query = "DELETE FROM `visitors` WHERE time < DATE_SUB(NOW(), INTERVAL $this->interval SECOND)";
return Database::InsertOrUpdate($query, $connection);
}
/**
* Function that displays the counted number of visitors from the database.
* Returns a numeric value
*
* @return integer $id
*/
function display($viewtype = NULL)
{
if ($viewtype == NULL) $viewtype=0;
switch ($viewtype){
case 1: //count hits
$query = "SELECT count(ip) as count FROM `visitors`";
$row = $this->update($query);
return $row["count"];
break;
case 2: //count visitors
default:
$query = "SELECT count(DISTINCT ip) AS count FROM `visitors`";
$row = $this->update($query);
return $row["count"];
break;
}
}
/**
* A helper function that is used to update rows in the database.
* Returns a numeric value
*
* @return integer $id
*/
function update($query)
{
$db = new database();
$connection = $db->Connect();
$this->clean();
$cursor = mysql_query($query, $connection);
return mysql_fetch_assoc($cursor);
}
}
?>