FIGURINGSOLID
Code Reviewer
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
Hello, if you are developing a web application, you may need this online users script to show how many users is online.
Yesterday i wrote a php class for it and decide to share it.
Creating table
Well, Before using this class, you must create a table in your database:
Creating php file
Next step is create and using 'SA_USERSONLINE' Class!
SA_USERSONLINE class:
Example usage:
Also, You can customize session timeout(in minutes):
$usersOnline
->
set_timeout
(
15
)
;
NOTE: If database connection is already established, You should remove lines 34 and 35:
Original tutorial here : http://softafzar.net/thread1717.html .
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Download
Yesterday i wrote a php class for it and decide to share it.
Creating table
Well, Before using this class, you must create a table in your database:
- CREATE
TABLE
`online_users`
(
- `session_id`
CHAR
(
150
)
NOT
NULL
,
- `last_activity`
INT
(
11
)
NOT
NULL
DEFAULT
'0'
- )
;
Creating php file
Next step is create and using 'SA_USERSONLINE' Class!
SA_USERSONLINE class:
- <?php
- /*
- * Author : Reza Ramezanpour <[email protected]>
- * Website: http://softafzar.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 (
)
- {
- session_start
(
)
;
- $this
->
session_id
=
session_id
(
)
;
- $this
->
time
=
time
(
)
;
- $this
->
link
=
mysqli_connect
(
$this
->
DB_HOST
,
$this
->
DB_USER
,
- $this
->
DB_PWD
,
$this
->
DB_NAME
)
;
- }
- /**
- * Gets current online users
- */
- function
get_online_users (
)
- {
- $this
->
delete_update_onlineusers
(
)
;
- $this
->
insert_onlineusers
(
)
;
- $this
->
stmt
=
mysqli_query
(
$this
->
link
,
- 'SELECT session_id FROM online_users'
)
;
- return
mysqli_num_rows
(
$this
->
stmt
)
;
- }
- private
function
already_registred (
)
- {
- $this
->
stmt
=
mysqli_query
(
$this
->
link
,
- "SELECT session_id FROM online_users WHERE session_id='$this->session_id
'"
)
;
- if
(
!
$this
->
stmt
||
mysqli_num_rows
(
$this
->
stmt
)
<=
0
)
- return
false
;
- return
true
;
- }
- private
function
insert_onlineusers (
)
- {
- if
(
!
$this
->
already_registred
(
)
)
{
- mysqli_query
(
$this
->
link
,
- "INSERT INTO online_users VALUES('$this->session_id
',$this->time
)"
)
;
- }
- }
- private
function
delete_update_onlineusers (
)
- {
- $timeout
=
$this
->
time
-
(
$this
->
timeout
*
60
)
;
- mysqli_query
(
$this
->
link
,
- "DELETE FROM online_users WHERE last_activity<=$timeout
"
)
;
- mysqli_query
(
$this
->
link
,
- "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
(
)
;
Also, You can customize session timeout(in minutes):
$usersOnline
->
set_timeout
(
15
)
;
NOTE: If database connection is already established, You should remove lines 34 and 35:
- $this
->
link
=
mysqli_connect
(
$this
->
DB_HOST
,
$this
->
DB_USER
,
- $this
->
DB_PWD
,
$this
->
DB_NAME
)
;
Original tutorial here : http://softafzar.net/thread1717.html .
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Download
You must upgrade your account or reply in the thread to view hidden text.