Jika anda sedang mendevelope web aplikasi tentu anda ingin tahu berapa yang online di web anda.
Berikut saya kasih tutorial
Script Untuk Melihat User Online
Creating table
Pertama membuat database dan tabel
CREATE TABLE `online_users` (
`session_id` CHAR(150) NOT NULL,
`last_activity` INT(11) NOT NULL DEFAULT '0'
);
Creating php file
Langkah selanjutnya ialah membuat dan menggunakan 'SA_USERSONLINE' Class!
SA_USERSONLINE class:
<?php
/*
* Author : D
* Website: http://database.net
*/
class SA_USERSONLINE
{
protected $DB_HOST = DB_HOST;
protected $DB_NAME = DB_NAME;
protected $DB_USER = DB_USER;
protected $DB_PWD = DB_PWD;
protected $session_id = null;
protected $time = null;
protected $timeout = 15;
protected $link = null;
protected $stmt = null;
function __construct ()
{
-
-
-
-
$this->DB_PWD, $this->DB_NAME);
}
/**
* Gets current online users
*/
function get_online_users ()
{
$this->delete_update_onlineusers();
$this->insert_onlineusers();
-
'SELECT session_id FROM online_users');
-
}
private function already_registred ()
{
-
"SELECT session_id FROM online_users WHERE session_id='$this->session_id'");
-
return false;
return true;
}
private function insert_onlineusers ()
{
if (! $this->already_registred()) {
-
"INSERT INTO online_users VALUES('$this->session_id',$this->time)");
}
}
private function delete_update_onlineusers ()
{
$timeout = $this->time - ($this->timeout * 60);
-
"DELETE FROM online_users WHERE last_activity<=$timeout");
-
"UPDATE online_users SET last_activity=$this->time WHERE session_id='$this->session_id'");
}
/**
* Set timeout in minutes.
*
* @param int $timeout
*/
function set_timeout ($timeout)
{
$this->timeout = ((int) $timeout);
}
}
?>
Example usage:
$usersOnline = new SA_USERSONLINE();
echo 'Online users: ', $usersOnline->get_online_users();
Juga, anda bisa customize session timeout(dalam menit):
$usersOnline->set_timeout(15);
Selamat mencoba..
Artikel keren lainnya:
Belum ada tanggapan untuk "PHP - Script Untuk Melihat User Online"
Post a Comment