943,677 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 9408
  • PHP RSS
Sep 28th, 2008
0

How to display online users

Expand Post »
Is there anyone with the knowledge on how to Show online users with php??
i have seen in many sites,now i need someone who can explain to do it.
(if there are users who are online to be displayed)
Please explain how to get started))))
Last edited by mrcniceguy; Sep 28th, 2008 at 2:51 pm. Reason: forgot
Similar Threads
Reputation Points: 17
Solved Threads: 8
Posting Whiz in Training
mrcniceguy is offline Offline
278 posts
since Mar 2008
Sep 28th, 2008
0

Re: How to display online users

Well, I'm not expert with PHP, but I would suggest creating an SQL table called onlineUsers or somthing like that. Then when users login a PHP script adds them to the table. When they logout, simply remove them from the list.

Something like:
php Syntax (Toggle Plain Text)
  1. <?php
  2. //Log the user in
  3. $con = mysql_connect("localhost","username","password");
  4. mysql_query("INSERT INTO onlineUsers (username) VALUE ('$username')") //Assuming the user's username is stored in $username
  5. ?>
And on Logout:
php Syntax (Toggle Plain Text)
  1. <?php
  2. //Clear Cookies, etc.
  3. $con = mysql_connect("localhost","username","password");
  4. mysql_query("DELETE FROM onlineUsers WHERE username = '$username'") //Assuming the user's username is stored in $username
  5. ?>
Last edited by FlashCreations; Sep 28th, 2008 at 4:01 pm. Reason: Added some code
Reputation Points: 47
Solved Threads: 47
Posting Whiz
FlashCreations is offline Offline
393 posts
since Sep 2008
Sep 28th, 2008
0

Re: How to display online users

This is great idea,i`m working on it.
Reputation Points: 17
Solved Threads: 8
Posting Whiz in Training
mrcniceguy is offline Offline
278 posts
since Mar 2008
Sep 28th, 2008
0

Re: How to display online users

Feel free to PM me if you need any help!!!
Reputation Points: 47
Solved Threads: 47
Posting Whiz
FlashCreations is offline Offline
393 posts
since Sep 2008
Sep 28th, 2008
0

Re: How to display online users

when someone is login his datas are been sent to ONLINE TABLE.
THE problem comes when login Out,the query is not deleting his datas.
Check the code below,is the one i`m using for login out.It was working but after i added this DELETE is no longer.
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. session_start();
  3. $user=$_SESSION['user'];
  4. if(session_is_registered('user')){
  5. $del="DELETE FROM online WHERE user='$user'";
  6. $true=mysql_query($del);
  7.  
  8.  
  9. if($true){
  10. echo"member offline";
  11.  
  12.  
  13. }else{
  14. echo"failed to delete";
  15. }
  16.  
  17.  
  18.  
  19.  
  20. session_unset();
  21. session_destroy();
  22.  
  23.  
  24. //include"index.php";
  25.  
  26.  
  27. //exit();
  28. }else{
  29. include"index.php";
  30.  
  31. }
  32.  
  33.  
  34. ?>
Reputation Points: 17
Solved Threads: 8
Posting Whiz in Training
mrcniceguy is offline Offline
278 posts
since Mar 2008
Sep 28th, 2008
1

Re: How to display online users

Ok then, try this:
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. session_start();
  3.  
  4. if(session_is_registered('user'))
  5. {
  6. $user=$_SESSION['user'];
  7. $del="DELETE FROM online WHERE user='$user'";
  8. $result=mysql_query($del);
  9.  
  10. if($result==true)
  11. {
  12. echo "<h3>Member Offline</h3>\n";
  13. }
  14. else
  15. {
  16. echo "<h3>Failed to Logoff</h3>\n";
  17. }
  18.  
  19. //Remove the Session Vars
  20. $_SESSION = array();
  21. if (isset($_COOKIE[session_name()])) {
  22. setcookie(session_name(), '', time()-42000, '/');
  23. }
  24. session_destroy();
  25.  
  26.  
  27. //include"index.php";
  28.  
  29. }
  30. else
  31. {
  32. include "index.php";
  33. }
  34. ?>
Reputation Points: 47
Solved Threads: 47
Posting Whiz
FlashCreations is offline Offline
393 posts
since Sep 2008
Sep 29th, 2008
0

Re: How to display online users

best way to store session and count how many session ............
Reputation Points: 15
Solved Threads: 21
Posting Whiz in Training
nikesh.yadav is offline Offline
219 posts
since Feb 2008
Sep 29th, 2008
0

Re: How to display online users

The simplest way to do this would be to read the number of active sessions as nikesh.yadav said.

I have added the code I use on my sites to give an example:
php Syntax (Toggle Plain Text)
  1. <?
  2. $number=0;
  3. // define the directory to save sessions in
  4. session_save_path("session_directory");
  5. session_start();
  6. // set the session as active
  7. $_SESSION['active']="yes";
  8. $directory = opendir("sessions");
  9. while($item = readdir($directory))
  10. {
  11. if(($item != ".") && ($item != ".."))
  12. {
  13. // count the number os sessions active within the last x sexonds
  14. if(fileatime('sessions/' . $item) > time()-120)
  15. {
  16. // add 1 to the session count
  17. $number++;
  18. }
  19. }
  20. }
  21. closedir($directory);
  22. session_unset();
  23. // return the value
  24. return ($number=='1') ? "There is " . $number . " user online now." : "There are " . $number . " users online now.";
  25. ?>
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Sep 30th, 2008
0

Re: How to display online users

thankx i understand about counting the sessions and storing them.
But the problem comes when i want to display them. bcoz i have a link called members when u click it all members ar displayed.
So how i`m i going to show who is online and offline at the same page????
Reputation Points: 17
Solved Threads: 8
Posting Whiz in Training
mrcniceguy is offline Offline
278 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Date list
Next Thread in PHP Forum Timeline: dynamically built webpage for multiple audio and text points.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC