display users online

  • xN3uRoNx
  • Beginner
  • Beginner
  • User avatar
  • Joined: Mar 11, 2006
  • Posts: 60
  • Status: Offline

Post March 11th, 2006, 12:09 pm

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:
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post March 11th, 2006, 12:09 pm

  • onlyican.com
  • Mastermind
  • Mastermind
  • User avatar
  • Joined: Nov 20, 2005
  • Posts: 1589
  • Loc: Hants, UK
  • Status: Offline

Post March 11th, 2006, 12:11 pm

that saved me an hour

cheers.

Guys, we have a brain
Heal your mind, and the body will follow
  • xN3uRoNx
  • Beginner
  • Beginner
  • User avatar
  • Joined: Mar 11, 2006
  • Posts: 60
  • Status: Offline

Post March 11th, 2006, 12:13 pm

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
  • No Avatar
  • Joined: Nov 08, 2003
  • Posts: 70
  • Status: Offline

Post March 15th, 2006, 1:48 pm

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
  • Joined: Mar 11, 2006
  • Posts: 60
  • Status: Offline

Post March 15th, 2006, 1:54 pm

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
  • No Avatar
  • Joined: Nov 08, 2003
  • Posts: 70
  • Status: Offline

Post March 15th, 2006, 2:01 pm

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
  • No Avatar
  • Joined: Nov 08, 2003
  • Posts: 70
  • Status: Offline

Post March 15th, 2006, 2:05 pm

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
  • Joined: Mar 11, 2006
  • Posts: 60
  • Status: Offline

Post March 15th, 2006, 2:26 pm

1st off, is "members" your table where you store everyones login information?
  • HoocH
  • Student
  • Student
  • No Avatar
  • Joined: Nov 08, 2003
  • Posts: 70
  • Status: Offline

Post March 15th, 2006, 2:28 pm

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
  • No Avatar
  • Joined: Nov 08, 2003
  • Posts: 70
  • Status: Offline

Post March 15th, 2006, 2:41 pm

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
  • Joined: Mar 11, 2006
  • Posts: 60
  • Status: Offline

Post March 15th, 2006, 2:56 pm

I'm just trying to follow along myself. But doesn't php nuke already have a "display users online" function?
  • HoocH
  • Student
  • Student
  • No Avatar
  • Joined: Nov 08, 2003
  • Posts: 70
  • Status: Offline

Post March 15th, 2006, 3:02 pm

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
  • No Avatar
  • Joined: Nov 08, 2003
  • Posts: 70
  • Status: Offline

Post April 27th, 2006, 8:19 am

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
  • Joined: Dec 25, 2004
  • Posts: 328
  • Loc: Holland [NL]
  • Status: Offline

Post April 27th, 2006, 6:10 pm

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
  • Joined: Dec 25, 2004
  • Posts: 328
  • Loc: Holland [NL]
  • Status: Offline

Post May 4th, 2006, 2:55 am

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
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post May 4th, 2006, 2:55 am

Post Information

  • Total Posts in this topic: 47 posts
  • Users browsing this forum: No registered users and 169 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
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.