display users online

  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

Hello all.
I was hoping someone could write me up a script that shows users online.
The script will be used in a popup. I want to show only the names of a users database.
Thank you for your time and help.

Hooch
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • onlyican.com
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1589
  • Loc: Hants, UK

Post 3+ Months Ago

On the same level.

Anyone know of a tutorial to say Who Is Online, like in PHPBB

I am building a forum from scratch
  • Truce
  • Guru
  • Guru
  • Truce
  • Posts: 1477
  • Loc: Washington DC

Post 3+ Months Ago

You're going to need to learn a lot about how sessions work to do that. PHPBB, along with most similar installations, determine's who is online by a reasonable time since the last page load. For example, when I visit Ozzu and go to the programming forum, that's registered in the database along with that time. When you access the same page, it pulls out anybody who visited the page in the last x minutes and displays their username. There are other ways of tracking it down even more with AJAX posting back to the server every x seconds or so, but that's not really neccesary unless you're doing something like a chat.

If you want someone to actually develop the user login script, and a script to show who is online and what page their on, PM me or email me for help.
  • PHP_Guru
  • Graduate
  • Graduate
  • User avatar
  • Posts: 179
  • Loc: Clermont, FL

Post 3+ Months Ago

You could also insert a row into a database for each identical IP address of the users who are on the site (that timeout after X amount of time(), I imagine about 300 seconds (5 minutes). For each movement on that IP address, record the users http location ($_SERVER[REQUEST_URI]). That way, you can use mysql_num_rows() to display how many ips are on your site, within a 5 minute timeframe, and be able to see their last known location.
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

Hi Truce.
What I want is a list of who is logged in.
  • xN3uRoNx
  • Beginner
  • Beginner
  • User avatar
  • Posts: 60

Post 3+ Months Ago

Hey, this is very simple, and i'm here to help! :D

1st off, add this into your members table, or users (whatever you use.)

Online - Varchar(255) default: No

- Next, you're going to create a new file named online.php, or usersonline.php, whatever your heart desires to name it :lol:

Code: [ Select ]
<b>Online Users:</b><BR>

<table border='0' style='border-collapse: collapse' width='100%' id='table1'>
<tr>
<td width='33%' bgcolor='<?=$bgcolor?>' align=''><b>Username</b></td>
</tr>
</table>
<table border='0' style='border-collapse: collapse' bgcolor="black" width='100%' id='table2'>
<tr>
<td width="100%"> <table border='1' style='border-collapse: collapse' width='100%' id='table2'>
<?php include("database_connect.php");

// add a mysql query to gather all the users whos online = Yes.
echo "<ol>";
$result = mysql_query("SELECT * FROM members WHERE online = 'Yes' ");
while ( $online = mysql_fetch_array($result) ) {
  $counter++;

  // have it show up to 99999999999999 on one page.
  
  if ($counter > 99999999999999) { break; }
  if ( $counter % 2 == 0 ) {
    $bgcolor="#bgcolorofyourchoice";
  } else {
    $bgcolor="#bgcolorofyourchoice";
  }
?>
<tr>
<td width='33%' bgcolor='<?=$bgcolor?>' align=''><a href="link=<?=$online['id']?>"><?=$online['username']?></a></td>
</tr>
<?php
} echo "</ol>";
?>
</table></td>
</tr>
</table>
  1. <b>Online Users:</b><BR>
  2. <table border='0' style='border-collapse: collapse' width='100%' id='table1'>
  3. <tr>
  4. <td width='33%' bgcolor='<?=$bgcolor?>' align=''><b>Username</b></td>
  5. </tr>
  6. </table>
  7. <table border='0' style='border-collapse: collapse' bgcolor="black" width='100%' id='table2'>
  8. <tr>
  9. <td width="100%"> <table border='1' style='border-collapse: collapse' width='100%' id='table2'>
  10. <?php include("database_connect.php");
  11. // add a mysql query to gather all the users whos online = Yes.
  12. echo "<ol>";
  13. $result = mysql_query("SELECT * FROM members WHERE online = 'Yes' ");
  14. while ( $online = mysql_fetch_array($result) ) {
  15.   $counter++;
  16.   // have it show up to 99999999999999 on one page.
  17.   
  18.   if ($counter > 99999999999999) { break; }
  19.   if ( $counter % 2 == 0 ) {
  20.     $bgcolor="#bgcolorofyourchoice";
  21.   } else {
  22.     $bgcolor="#bgcolorofyourchoice";
  23.   }
  24. ?>
  25. <tr>
  26. <td width='33%' bgcolor='<?=$bgcolor?>' align=''><a href="link=<?=$online['id']?>"><?=$online['username']?></a></td>
  27. </tr>
  28. <?php
  29. } echo "</ol>";
  30. ?>
  31. </table></td>
  32. </tr>
  33. </table>


- Thirdly, your going to need to add a mysql_query to your login page, and your logout page. :P

Code: [ Select ]
mysql_query("update members set online = 'No' where id = '$view[id]'") or die ("error: " . mysql_error()); // will update it when user logs out.


Code: [ Select ]
mysql_query("update members set online = 'Yes' where id = '$view[id]'") or die ("error: " . mysql_error()); // will update it when user logs in.



thats basically it, accept you chage everything so it will fit your coding needs/sql :) oh, and the view[id] needs to be changed to whatever you use for that aswell. :) Good luck, I hope this helps! (I tried to explain the best I could.)

again, good luck in creating this, and I hope this works for you!. If you have a question, just ask :)
  • Vincent
  • Expert
  • Expert
  • User avatar
  • Posts: 721
  • Loc: Brisbane, Australia

Post 3+ Months Ago

we're probably going to make a sticky out of this subject some time because there is a large number of people who come to this site asking for the same thing and basically the same thing is said over and over again.

what we need is a tutorial that tells you what options there are to do this and how you would attempt to do this each time, with a bit of code provided to give the programmer a bit of a head start for their own way of doing it
  • xN3uRoNx
  • Beginner
  • Beginner
  • User avatar
  • Posts: 60

Post 3+ Months Ago

Vincent, agreed. I just gave a quick basis of this code, and the most simplest way of doing it, as I could think of many other ways of coding this :D :wink: but, this code works just fine for me, and it gives a simple edit to the look :)
  • onlyican.com
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1589
  • Loc: Hants, UK

Post 3+ Months Ago

One problem I see, if the person closes the window instead of logging out, they will always be logged in.
  • xN3uRoNx
  • Beginner
  • Beginner
  • User avatar
  • Posts: 60

Post 3+ Months Ago

onlyican.com wrote:
One problem I see, if the person closes the window instead of logging out, they will always be logged in.


thats why you would include your header page, or head page which has all that info in it :wink:
  • onlyican.com
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1589
  • Loc: Hants, UK

Post 3+ Months Ago

huh!
  • xN3uRoNx
  • Beginner
  • Beginner
  • User avatar
  • Posts: 60

Post 3+ Months Ago

<?php include("page.php"); ?>

^^that page will have all the info about if the user is logged in or not :wink:
  • onlyican.com
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1589
  • Loc: Hants, UK

Post 3+ Months Ago

ok, from my understanding
when somone loggs in, we update the table to say online

when someone logs out, we update the table to say offline

What happens if someone does not log out, they close there browser,

I never log out, I always close by browser.

How can we tell that the user has closed the browser or still online?
  • xN3uRoNx
  • Beginner
  • Beginner
  • User avatar
  • Posts: 60

Post 3+ Months Ago

You could read the timestamp of the users most recent page hit, and if it has been 15 minutes of inactivity, that user gets logged out. Than, it will be overwritten with the current time. (the new timestamp.) :)
  • onlyican.com
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1589
  • Loc: Hants, UK

Post 3+ Months Ago

I have already posted that in a prev topic today

My way of doing,

NOTE: NOT TESTED

When a user uses a page, they go into a db, noted at that time.

When someone loads a page a query reads select all users where last_online greater than 15 mins ago.

(code will be a bit longer)
  • xN3uRoNx
  • Beginner
  • Beginner
  • User avatar
  • Posts: 60

Post 3+ Months Ago

Add this into "login.php" or whatever file you use for logging in.
Code: [ Select ]
<?php
$current_time = time(); // get the current time
$_SESSION['loginTime']=$current_time; // login time
$_SESSION['lastActivity']=$current_time;
?>
  1. <?php
  2. $current_time = time(); // get the current time
  3. $_SESSION['loginTime']=$current_time; // login time
  4. $_SESSION['lastActivity']=$current_time;
  5. ?>



"page.php" (that page that you include in every page, name it whatever.)
Code: [ Select ]
<?php
$timeout_min = 15; // 15 minutes of inactivity - You set it however you want.
$timeout_length = $timeout_min * 60;

if ($current_time - $_SESSION['lastActivity'] > $timeout_length) {
    $_SESSION = array();
    if (isset($_COOKIE[session_name()])) {
      unset($_COOKIE[session_name()]);
    }
    session_destroy();
    echo "15 Minutes of inactivity - Logging out.
exit;
} else $_SESSION['lastActivity'] = $current_time;
?>
  1. <?php
  2. $timeout_min = 15; // 15 minutes of inactivity - You set it however you want.
  3. $timeout_length = $timeout_min * 60;
  4. if ($current_time - $_SESSION['lastActivity'] > $timeout_length) {
  5.     $_SESSION = array();
  6.     if (isset($_COOKIE[session_name()])) {
  7.       unset($_COOKIE[session_name()]);
  8.     }
  9.     session_destroy();
  10.     echo "15 Minutes of inactivity - Logging out.
  11. exit;
  12. } else $_SESSION['lastActivity'] = $current_time;
  13. ?>


I've done it like that, and i've seen people do it like that over the web :) Hope that helped :wink:
  • onlyican.com
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1589
  • Loc: Hants, UK

Post 3+ Months Ago

that saved me an hour

cheers.

Guys, we have a brain
  • xN3uRoNx
  • Beginner
  • Beginner
  • User avatar
  • Posts: 60

Post 3+ Months Ago

onlyican.com wrote:
that saved me an hour

cheers.

Guys, we have a brain


haha, nah man. :D I've just learned from what i've seen in the past over the internet :D
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

Hey xN3uRoNx
Thank you for your time.
I am trying to follow your instructions.
Here's the error I get when opening onlin.php
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mywebpage/online.php on line 24
Line 24 is...
while ( $online = mysql_fetch_array($result) ) {
I added the online variable to mysql. Here's hoow it looks in the table
`Online` varchar(255) NOT NULL default 'no',



Also...for connect_database.php
Here's my code I used..is this ok?

Code: [ Select ]
<?php
$dbhost = "localhost";
$dbuname = 'blah';
$dbpass = 'pw_here';
$dbname = 'blah';
?>
  1. <?php
  2. $dbhost = "localhost";
  3. $dbuname = 'blah';
  4. $dbpass = 'pw_here';
  5. $dbname = 'blah';
  6. ?>


Thanks for your help..Hooch
  • xN3uRoNx
  • Beginner
  • Beginner
  • User avatar
  • Posts: 60

Post 3+ Months Ago

for your connect_db.php, i usually do something like this:

PHP Code: [ Select ]
<?php
 
$dbhost="localhost";
 
$dbusername ="dbuser";
 
$dbpassword ="dbpassword";
 
$dbname="dbname";
 
$db=mysql_connect($dbhost, $dbusername, $dbpassword) or die (mysql_error());
 
mysql_select_db($dbname, $db) or die (mysql_error());
 
?>
  1. <?php
  2.  
  3. $dbhost="localhost";
  4.  
  5. $dbusername ="dbuser";
  6.  
  7. $dbpassword ="dbpassword";
  8.  
  9. $dbname="dbname";
  10.  
  11. $db=mysql_connect($dbhost, $dbusername, $dbpassword) or die (mysql_error());
  12.  
  13. mysql_select_db($dbname, $db) or die (mysql_error());
  14.  
  15. ?>


also, for that error your getting is this the whole line your getting it from?
PHP Code: [ Select ]
$result = mysql_query("SELECT * FROM members WHERE online = 'Yes' ");
 
while ( $online = mysql_fetch_array($result) ) {
  1. $result = mysql_query("SELECT * FROM members WHERE online = 'Yes' ");
  2.  
  3. while ( $online = mysql_fetch_array($result) ) {


Post your whole code please.
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

What I am trying to do is use this with an existing phpnuke website.

SO I have not set the login/logout code you posted earlier.
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

PHP Code: [ Select ]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
 
<head>
 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 
<title>Untitled Document</title>
 
</head>
 
 
 
<body>
 
<b>Online Users:</b><BR>
 
 
 
<table border='0' style='border-collapse: collapse' width='100%' id='table1'>
 
<tr>
 
<td width='33%' bgcolor='<?=$bgcolor?>' align=''><b>Username</b></td>
 
</tr>
 
</table>
 
<table border='0' style='border-collapse: collapse' bgcolor="black" width='100%' id='table2'>
 
<tr>
 
<td width="100%"> <table border='1' style='border-collapse: collapse' width='100%' id='table2'>
 
<?php include("database_connect.php");
 
 
 
// add a mysql query to gather all the users whos online = Yes.
 
echo "<ol>";
 
$result = mysql_query("SELECT * FROM members WHERE online = 'Yes' ");
 
while ( $Online = mysql_fetch_array($result) ) { //<<<<<<<<<<<<<<<Line 24
 
    $counter++;
 
 
 
    // have it show up to 99999999999999 on one page.
 
   
 
    if ($counter > 99999999999999) { break; }
 
    if ( $counter % 2 == 0 ) {
 
        $bgcolor="#fffff3";
 
    } else {
 
        $bgcolor="#fffff3";
 
    }
 
?>
 
<tr>
 
<td width='33%' bgcolor='<?=$bgcolor?>' align=''><a href="link=<?=$online['id']?>"><?=$online['username']?></a></td>
 
</tr>
 
<?php
 
} echo "</ol>";
 
?>
 
</table></td>
 
</tr>
 
</table>
 
</body>
 
</html>
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.  
  5. <head>
  6.  
  7. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  8.  
  9. <title>Untitled Document</title>
  10.  
  11. </head>
  12.  
  13.  
  14.  
  15. <body>
  16.  
  17. <b>Online Users:</b><BR>
  18.  
  19.  
  20.  
  21. <table border='0' style='border-collapse: collapse' width='100%' id='table1'>
  22.  
  23. <tr>
  24.  
  25. <td width='33%' bgcolor='<?=$bgcolor?>' align=''><b>Username</b></td>
  26.  
  27. </tr>
  28.  
  29. </table>
  30.  
  31. <table border='0' style='border-collapse: collapse' bgcolor="black" width='100%' id='table2'>
  32.  
  33. <tr>
  34.  
  35. <td width="100%"> <table border='1' style='border-collapse: collapse' width='100%' id='table2'>
  36.  
  37. <?php include("database_connect.php");
  38.  
  39.  
  40.  
  41. // add a mysql query to gather all the users whos online = Yes.
  42.  
  43. echo "<ol>";
  44.  
  45. $result = mysql_query("SELECT * FROM members WHERE online = 'Yes' ");
  46.  
  47. while ( $Online = mysql_fetch_array($result) ) { //<<<<<<<<<<<<<<<Line 24
  48.  
  49.     $counter++;
  50.  
  51.  
  52.  
  53.     // have it show up to 99999999999999 on one page.
  54.  
  55.    
  56.  
  57.     if ($counter > 99999999999999) { break; }
  58.  
  59.     if ( $counter % 2 == 0 ) {
  60.  
  61.         $bgcolor="#fffff3";
  62.  
  63.     } else {
  64.  
  65.         $bgcolor="#fffff3";
  66.  
  67.     }
  68.  
  69. ?>
  70.  
  71. <tr>
  72.  
  73. <td width='33%' bgcolor='<?=$bgcolor?>' align=''><a href="link=<?=$online['id']?>"><?=$online['username']?></a></td>
  74.  
  75. </tr>
  76.  
  77. <?php
  78.  
  79. } echo "</ol>";
  80.  
  81. ?>
  82.  
  83. </table></td>
  84.  
  85. </tr>
  86.  
  87. </table>
  88.  
  89. </body>
  90.  
  91. </html>
  • xN3uRoNx
  • Beginner
  • Beginner
  • User avatar
  • Posts: 60

Post 3+ Months Ago

1st off, is "members" your table where you store everyones login information?
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

the table is called nuke_users (the one where I entered the online sql)
Is this what you mean?

here's how I just changed it
PHP Code: [ Select ]
$result = mysql_query("SELECT * FROM nuke_users WHERE online = 'Yes' ");


But still same error
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

I'm thinking this is not a good way to do this for a phpnuke site.

Why I'm doing this is I am making a flash mp3 player/shoutbox.
I want to add an extra part to it where you can see who is logged into the site.
But I do not want to make a new users section just for the mp3 player/shoutbox.

Just say so if this is getting to crazy of a request.
  • xN3uRoNx
  • Beginner
  • Beginner
  • User avatar
  • Posts: 60

Post 3+ Months Ago

I'm just trying to follow along myself. But doesn't php nuke already have a "display users online" function?
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

yes...but I wanted a little window to pop up from my flash control panel to show who is online.
This way people can have the mp3 player running, and see who is online..without going to the site.
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

xN3uRoNx wrote:
oh, and the view[id] needs to be changed to whatever you use for that aswell.


Do you mean change "view" or "id"?
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

Code: [ Select ]

create table session(
session_id INT unsigned not null auto_increment primary key,
session_ip varchar(15) not null,
session_ti INT(6) not null,
session_di INT(2) not null
session_us varchar(30) null);
  1. create table session(
  2. session_id INT unsigned not null auto_increment primary key,
  3. session_ip varchar(15) not null,
  4. session_ti INT(6) not null,
  5. session_di INT(2) not null
  6. session_us varchar(30) null);


session_id : unique identifier.
session_ip : ip addres "###.###.###.###" of the current user
session_ti : time "HHMMSS" of the initialised session (update)
session_di : current day (to handle time) (00:00:00u)
session_us : for a username if logged_in?

PHP Code: [ Select ]
 Updated code a few posts further down this thread



Its late, maybe i will explain in more detail later on...

ps. Not tested, just started writing..
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

I received a few PM`s on this, so here the functional "idea" behind this script.


First we create a function wich will return us a "session" id that is based on the "row" id of the session table. We will set this value in the "$_SESSION vars so we can validate it.

*------ The Function -----------*


First we create a "cleanup" script that will remove all "old" sessions from the table so each "page refresh" will give us "up-to-date" information about all the sessions.

The Cleanup will be triggered based on a "timestamp" of +15Mins. If session is "older" then these 15 Mins the session will / should be removed from the database.


Next we should handle the "current" session. If $_SESSION was allready set then this is an "old user" for he allready triggered this function before. If this is the case, we check if the previous "page refresh" was within these "15 mins" else the session was lost and we notify the user (if logged in) else we just create a new session.

If the $_SESSION wasnt set, this means this user "triggered" this script for the first time. In this case we dont need to validate anything, this because we have a new guest browsing.

Code: [ Select ]
Ladder
+------+Call the function.
     |
     +Run the Cleanup Script to validate all sessions (key current time).
     | To remove all sessions of "left" users (closed browser or left website)
     |
     +Validate $_SESSION (isset)
            |
        ?Yes / No?
            |
            + Yes +
            |     |
            |     +Check Session age (+15mins)?
            |     |
            |     <15
            |     |  Update Timestamp -> Return(Row ID)
            |     |
            |     |
            |    15>
            |       If Logged in, Logout and "return logout" message
            |       Create new Session as Guest > Return (Row ID)
            |      
            |
            +NO+
               Create new Session as Guest > Return(Row ID)
  1. Ladder
  2. +------+Call the function.
  3.      |
  4.      +Run the Cleanup Script to validate all sessions (key current time).
  5.      | To remove all sessions of "left" users (closed browser or left website)
  6.      |
  7.      +Validate $_SESSION (isset)
  8.             |
  9.         ?Yes / No?
  10.             |
  11.             + Yes +
  12.             |     |
  13.             |     +Check Session age (+15mins)?
  14.             |     |
  15.             |     <15
  16.             |     |  Update Timestamp -> Return(Row ID)
  17.             |     |
  18.             |     |
  19.             |    15>
  20.             |       If Logged in, Logout and "return logout" message
  21.             |       Create new Session as Guest > Return (Row ID)
  22.             |      
  23.             |
  24.             +NO+
  25.                Create new Session as Guest > Return(Row ID)





-May
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 47 posts
  • Users browsing this forum: No registered users and 132 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.