PHP - Users Online Script


/ Published in: PHP
Save to your folder(s)

Original tutorial here : http://softafzar.net/thread1717.html .


Copy this code and paste it in your HTML
  1. Hello, if you are developing a web application, you may need this online users script to show how many users is online.
  2.  
  3. Yesterday i wrote a php class for it and decide to share it.
  4. Creating table
  5.  
  6. Well, Before using this class, you must create a table in your database:
  7.  
  8. CREATE TABLE `online_users` (
  9.  
  10. `session_id` CHAR(150) NOT NULL,
  11.  
  12. `last_activity` INT(11) NOT NULL DEFAULT '0'
  13.  
  14. );
  15.  
  16. Creating php file
  17.  
  18. Next step is create and using 'SA_USERSONLINE' Class!
  19.  
  20. SA_USERSONLINE class:
  21.  
  22. <?php
  23.  
  24. /*
  25.   * Author : Reza Ramezanpour <[email protected]>
  26.   * Website: http://softafzar.net
  27.   */
  28.  
  29. class SA_USERSONLINE
  30. {
  31.  
  32. protected $DB_HOST = DB_HOST;
  33.  
  34. protected $DB_NAME = DB_NAME;
  35.  
  36. protected $DB_USER = DB_USER;
  37.  
  38. protected $DB_PWD = DB_PWD;
  39.  
  40. protected $session_id = null;
  41.  
  42. protected $time = null;
  43.  
  44. protected $timeout = 15;
  45.  
  46. protected $link = null;
  47.  
  48. protected $stmt = null;
  49.  
  50. function __construct ()
  51. {
  52. $this->session_id = session_id();
  53. $this->time = time();
  54. $this->link = mysqli_connect($this->DB_HOST, $this->DB_USER,
  55. $this->DB_PWD, $this->DB_NAME);
  56. }
  57.  
  58. /**
  59.   * Gets current online users
  60.   */
  61. function get_online_users ()
  62. {
  63. $this->delete_update_onlineusers();
  64. $this->insert_onlineusers();
  65. $this->stmt = mysqli_query($this->link,
  66. 'SELECT session_id FROM online_users');
  67. return mysqli_num_rows($this->stmt);
  68. }
  69.  
  70. private function already_registred ()
  71. {
  72. $this->stmt = mysqli_query($this->link,
  73. "SELECT session_id FROM online_users WHERE session_id='$this->session_id'");
  74. if (! $this->stmt || mysqli_num_rows($this->stmt) <= 0)
  75. return false;
  76. return true;
  77. }
  78.  
  79. private function insert_onlineusers ()
  80. {
  81. if (! $this->already_registred()) {
  82. "INSERT INTO online_users VALUES('$this->session_id',$this->time)");
  83. }
  84. }
  85.  
  86. private function delete_update_onlineusers ()
  87. {
  88. $timeout = $this->time - ($this->timeout * 60);
  89. "DELETE FROM online_users WHERE last_activity<=$timeout");
  90. "UPDATE online_users SET last_activity=$this->time WHERE session_id='$this->session_id'");
  91. }
  92.  
  93. /**
  94.   * Set timeout in minutes.
  95.   *
  96.   * @param int $timeout
  97.   */
  98. function set_timeout ($timeout)
  99. {
  100. $this->timeout = ((int) $timeout);
  101. }
  102. }
  103.  
  104. ?>
  105.  
  106. Example usage:
  107.  
  108. $usersOnline = new SA_USERSONLINE();
  109. echo 'Online users: ', $usersOnline->get_online_users();
  110.  
  111. Also, You can customize session timeout(in minutes):
  112. $usersOnline->set_timeout(15);
  113.  
  114. NOTE: If database connection is already established, You should remove lines 34 and 35:
  115.  
  116. $this->link = mysqli_connect($this->DB_HOST, $this->DB_USER,
  117. $this->DB_PWD, $this->DB_NAME);

URL: http://www.softafzar.net/thread1717.html/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.