PHP - Users Online Script

Submitted by: 
Language: 
Visitors have accessed this post 6990 times.

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.  
  3. `session_id` CHAR(150) NOT NULL,
  4.  
  5. `last_activity` INT(11) NOT NULL DEFAULT '0'
  6.  
  7. );

Creating php file

Next step is create and using 'SA_USERSONLINE' Class!

SA_USERSONLINE class:

  1. <?php
  2.  
  3. /*
  4.  * Author : Reza Ramezanpour <<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>>
  5.  * Website: <a href="http://softafzar.net
  6. " rel="nofollow">http://softafzar.net
  7. </a> */
  8.  
  9. class SA_USERSONLINE
  10. {
  11.  
  12. protected $DB_HOST = DB_HOST;
  13.  
  14. protected $DB_NAME = DB_NAME;
  15.  
  16. protected $DB_USER = DB_USER;
  17.  
  18. protected $DB_PWD = DB_PWD;
  19.  
  20. protected $session_id = null;
  21.  
  22. protected $time = null;
  23.  
  24. protected $timeout = 15;
  25.  
  26. protected $link = null;
  27.  
  28. protected $stmt = null;
  29.  
  30. function __construct ()
  31. {
  32. $this->session_id = session_id();
  33. $this->time = time();
  34. $this->link = mysqli_connect($this->DB_HOST, $this->DB_USER,
  35. $this->DB_PWD, $this->DB_NAME);
  36. }
  37.  
  38. /**
  39.   * Gets current online users
  40.   */
  41. function get_online_users ()
  42. {
  43. $this->delete_update_onlineusers();
  44. $this->insert_onlineusers();
  45. $this->stmt = mysqli_query($this->link,
  46. 'SELECT session_id FROM online_users');
  47. return mysqli_num_rows($this->stmt);
  48. }
  49.  
  50. private function already_registred ()
  51. {
  52. $this->stmt = mysqli_query($this->link,
  53. "SELECT session_id FROM online_users WHERE session_id='$this->session_id'");
  54. if (! $this->stmt || mysqli_num_rows($this->stmt) <= 0)
  55. return false;
  56. return true;
  57. }
  58.  
  59. private function insert_onlineusers ()
  60. {
  61. if (! $this->already_registred()) {
  62. mysqli_query($this->link,
  63. "INSERT INTO online_users VALUES('$this->session_id',$this->time)");
  64. }
  65. }
  66.  
  67. private function delete_update_onlineusers ()
  68. {
  69. $timeout = $this->time - ($this->timeout * 60);
  70. mysqli_query($this->link,
  71. "DELETE FROM online_users WHERE last_activity<=$timeout");
  72. mysqli_query($this->link,
  73. "UPDATE online_users SET last_activity=$this->time WHERE session_id='$this->session_id'");
  74. }
  75.  
  76. /**
  77.   * Set timeout in minutes.
  78.   *
  79.   * @param int $timeout
  80.   */
  81. function set_timeout ($timeout)
  82. {
  83. $this->timeout = ((int) $timeout);
  84. }
  85. }
  86.  
  87. ?>

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.

Comments

GOOD TUTORIAL YOU HAVE HERE,
KEEP ON SHARING,TNX GODBLESS : )

GOD IS GOOD ALL THE TIME: )

Thanks for sharing. very useful ;)

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.