• We just launched and are currently in beta. Join us as we build and grow the community.

PHP - Users Online Script

FIGURINGSOLID

Code Reviewer
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
129
Likes
36
Bits
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:

  1. CREATE

    TABLE

    `online_users`

    (

  2. `session_id`

    CHAR

    (

    150

    )

    NOT

    NULL

    ,

  3. `last_activity`

    INT

    (

    11

    )

    NOT

    NULL

    DEFAULT

    '0'

  4. )

    ;

Creating php file

Next step is create and using 'SA_USERSONLINE' Class!

SA_USERSONLINE class:
  1. <?php

  2. /*
  3. * Author : Reza Ramezanpour <[email protected]>
  4. * Website: http://softafzar.net
  5. */

  6. class

    SA_USERSONLINE
  7. {

  8. protected

    $DB_HOST

    =

    DB_HOST;

  9. protected

    $DB_NAME

    =

    DB_NAME;

  10. protected

    $DB_USER

    =

    DB_USER;

  11. protected

    $DB_PWD

    =

    DB_PWD;

  12. protected

    $session_id

    =

    null

    ;

  13. protected

    $time

    =

    null

    ;

  14. protected

    $timeout

    =

    15

    ;

  15. protected

    $link

    =

    null

    ;

  16. protected

    $stmt

    =

    null

    ;

  17. function

    __construct (

    )
  18. {
  19. session_start

    (

    )

    ;
  20. $this

    ->

    session_id

    =

    session_id

    (

    )

    ;
  21. $this

    ->

    time

    =

    time

    (

    )

    ;
  22. $this

    ->

    link

    =

    mysqli_connect

    (

    $this

    ->

    DB_HOST

    ,

    $this

    ->

    DB_USER

    ,
  23. $this

    ->

    DB_PWD

    ,

    $this

    ->

    DB_NAME

    )

    ;
  24. }

  25. /**
  26. * Gets current online users
  27. */
  28. function

    get_online_users (

    )
  29. {
  30. $this

    ->

    delete_update_onlineusers

    (

    )

    ;
  31. $this

    ->

    insert_onlineusers

    (

    )

    ;
  32. $this

    ->

    stmt

    =

    mysqli_query

    (

    $this

    ->

    link

    ,
  33. 'SELECT session_id FROM online_users'

    )

    ;
  34. return

    mysqli_num_rows

    (

    $this

    ->

    stmt

    )

    ;
  35. }

  36. private

    function

    already_registred (

    )
  37. {
  38. $this

    ->

    stmt

    =

    mysqli_query

    (

    $this

    ->

    link

    ,
  39. "SELECT session_id FROM online_users WHERE session_id='$this->session_id

    '"

    )

    ;
  40. if

    (

    !

    $this

    ->

    stmt

    ||

    mysqli_num_rows

    (

    $this

    ->

    stmt

    )

    <=

    0

    )
  41. return

    false

    ;
  42. return

    true

    ;
  43. }

  44. private

    function

    insert_onlineusers (

    )
  45. {
  46. if

    (

    !

    $this

    ->

    already_registred

    (

    )

    )

    {
  47. mysqli_query

    (

    $this

    ->

    link

    ,
  48. "INSERT INTO online_users VALUES('$this->session_id

    ',$this->time

    )"

    )

    ;
  49. }
  50. }

  51. private

    function

    delete_update_onlineusers (

    )
  52. {
  53. $timeout

    =

    $this

    ->

    time

    -

    (

    $this

    ->

    timeout

    *

    60

    )

    ;
  54. mysqli_query

    (

    $this

    ->

    link

    ,
  55. "DELETE FROM online_users WHERE last_activity<=$timeout

    "

    )

    ;
  56. mysqli_query

    (

    $this

    ->

    link

    ,
  57. "UPDATE online_users SET last_activity=$this->time

    WHERE session_id='$this->session_id

    '"

    )

    ;
  58. }

  59. /**
  60. * Set timeout in minutes.
  61. *
  62. * @param int $timeout
  63. */
  64. function

    set_timeout (

    $timeout

    )
  65. {
  66. $this

    ->

    timeout

    =

    (

    (

    int)

    $timeout

    )

    ;
  67. }
  68. }

  69. ?>

Example usage:

  1. $usersOnline

    =

    new

    SA_USERSONLINE(

    )

    ;
  2. 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:
  1. $this

    ->

    link

    =

    mysqli_connect

    (

    $this

    ->

    DB_HOST

    ,

    $this

    ->

    DB_USER

    ,
  2. $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.
 

442,401

317,942

317,951

Top