mirror of
https://github.com/slawkens/myaac.git
synced 2025-10-14 09:44:55 +02:00
feature: visitors counter shows now user browser, and also if its bot
This commit is contained in:
@@ -34,10 +34,12 @@ class Visitors
|
||||
$this->cleanVisitors();
|
||||
|
||||
$ip = $_SERVER['REMOTE_ADDR'];
|
||||
$userAgentShortened = substr($_SERVER['HTTP_USER_AGENT'] ?? 'unknown', 0, 255);
|
||||
|
||||
if($this->visitorExists($ip))
|
||||
$this->updateVisitor($ip, $_SERVER['REQUEST_URI']);
|
||||
$this->updateVisitor($ip, $_SERVER['REQUEST_URI'], $userAgentShortened);
|
||||
else
|
||||
$this->addVisitor($ip, $_SERVER['REQUEST_URI']);
|
||||
$this->addVisitor($ip, $_SERVER['REQUEST_URI'], $userAgentShortened);
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
@@ -75,26 +77,26 @@ class Visitors
|
||||
$db->exec('DELETE FROM ' . $db->tableName(TABLE_PREFIX . 'visitors') . ' WHERE ' . $db->fieldName('lastvisit') . ' < ' . (time() - $this->sessionTime * 60));
|
||||
}
|
||||
|
||||
private function updateVisitor($ip, $page)
|
||||
private function updateVisitor($ip, $page, $userAgent)
|
||||
{
|
||||
if($this->cacheEnabled) {
|
||||
$this->data[$ip] = array('page' => $page, 'lastvisit' => time());
|
||||
$this->data[$ip] = array('page' => $page, 'lastvisit' => time(), 'user_agent' => $userAgent);
|
||||
return;
|
||||
}
|
||||
|
||||
global $db;
|
||||
$db->exec('UPDATE ' . $db->tableName(TABLE_PREFIX . 'visitors') . ' SET ' . $db->fieldName('lastvisit') . ' = ' . time() . ', ' . $db->fieldName('page') . ' = ' . $db->quote($page) . ' WHERE ' . $db->fieldName('ip') . ' = ' . $db->quote($ip));
|
||||
$db->update(TABLE_PREFIX . 'visitors', ['lastvisit' => time(), 'page' => $page, 'user_agent' => $userAgent], ['ip' => $ip]);
|
||||
}
|
||||
|
||||
private function addVisitor($ip, $page)
|
||||
private function addVisitor($ip, $page, $userAgent)
|
||||
{
|
||||
if($this->cacheEnabled) {
|
||||
$this->data[$ip] = array('page' => $page, 'lastvisit' => time());
|
||||
$this->data[$ip] = array('page' => $page, 'lastvisit' => time(), 'user_agent' => $userAgent);
|
||||
return;
|
||||
}
|
||||
|
||||
global $db;
|
||||
$db->exec('INSERT INTO ' . $db->tableName(TABLE_PREFIX . 'visitors') . ' (' . $db->fieldName('ip') . ' ,' . $db->fieldName('lastvisit') . ', ' . $db->fieldName('page') . ') VALUE (' . $db->quote($ip) . ', ' . time() . ', ' . $db->quote($page) . ')');
|
||||
$db->insert(TABLE_PREFIX . 'visitors', ['ip' => $ip, 'lastvisit' => time(), 'page' => $page, 'user_agent' => $userAgent]);
|
||||
}
|
||||
|
||||
public function getVisitors()
|
||||
@@ -107,7 +109,7 @@ class Visitors
|
||||
}
|
||||
|
||||
global $db;
|
||||
return $db->query('SELECT ' . $db->fieldName('ip') . ', ' . $db->fieldName('lastvisit') . ', ' . $db->fieldName('page') . ' FROM ' . $db->tableName(TABLE_PREFIX . 'visitors') . ' ORDER BY ' . $db->fieldName('lastvisit') . ' DESC')->fetchAll();
|
||||
return $db->query('SELECT ' . $db->fieldName('ip') . ', ' . $db->fieldName('lastvisit') . ', ' . $db->fieldName('page') . ', ' . $db->fieldName('user_agent') . ' FROM ' . $db->tableName(TABLE_PREFIX . 'visitors') . ' ORDER BY ' . $db->fieldName('lastvisit') . ' DESC')->fetchAll();
|
||||
}
|
||||
|
||||
public function getAmountVisitors()
|
||||
|
4
system/migrations/34.php
Normal file
4
system/migrations/34.php
Normal file
@@ -0,0 +1,4 @@
|
||||
<?php
|
||||
// add user_agent column into visitors
|
||||
|
||||
$db->exec('ALTER TABLE `' . TABLE_PREFIX . "visitors` ADD `user_agent` VARCHAR(255) NOT NULL DEFAULT '';");
|
@@ -9,6 +9,7 @@
|
||||
<th>IP</th>
|
||||
<th>Last visit</th>
|
||||
<th>Page</th>
|
||||
<th>Browser</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -17,6 +18,7 @@
|
||||
<td>{{ visitor.ip }}</td>
|
||||
<td>{{ visitor.lastvisit|date("H:i:s") }}</td>
|
||||
<td><a href="{{ visitor.page }}">{{ visitor.page|slice(0, 50) }}</a></td>
|
||||
<td>{{ visitor.browser }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
|
Reference in New Issue
Block a user