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
  • 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: 1473
  • 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
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

Functional Code Updated below...

Code written in classes, see my followup post hooch, should be copy paste... tested to :D
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

I am trying to fix up your code.
There was some obvious errors that even I could fix...yay me.
But I am getting an error that I cannot fix.
1st here's the updated code
PHP Code: [ Select ]
<?php
 
// All you need to know  Still requires some work on the "returns" and the sql queries needed.
 
// But then again some work should be done by yourself too...  
 
// good luck.
 
 
 
function handle_session($session_iid){  
 
     
 
    // Set stuff to work with //  
 
    $raw_time = mktime();  
 
    $timestamp = $raw_time['hour'].$raw_time['minute'].$raw_time['second'];  
 
    $cur_day = date(d);  
 
    $user_ip = isset($_SESSION['user_ip']) ? $_SESSION['user_ip'] : $_SERVER['REMOTE_ADDR'];  
 
     
 
// Old session clean up so we keep valid data //
 
    $sql = 'select * from session';  
 
    $result = mysql_query($sql);  
 
    while($row = mysql_fetch_array($result)){
 
 
 
        if($row['session_di'] == $cur_day){  
 
            $val_session_time = (($row['session_ti'] - $timestamp) < 1500) ? true :  
 
        }else{  
 
            $am = "600000" - $row['session_ti'];                          
 
            $val_session_time = (($am + $timestamp) < 1500) ?  true : false;            
 
        }  
 
         
 
    // Found old session! Delete the B#$ch //          
 
        if($val_session_time == false){  
 
        $sql = 'delete from session where session_id ='.$row['session_id'];
 
        if($result = mysql_query($sql){
 
        next;
 
            }
 
        }  
 
    }  
 
    // Check Current Session //
 
    if(isset($session_iid) ){  
 
           // Find the session in the table
 
           $sql = 'select * from session where session_id ='.$session_iid;  
 
           $result = mysql_query($sql);  
 
           $row = mysql_fetch_array($result);  
 
           
 
        // validate session age //          
 
           if($row['session_di'] == $cur_day){  
 
                   $val_session_time = (($row['session_ti'] - $timestamp)) < 1500) ? true :  
 
           }else{  
 
                   $am = "600000" - $row['session_ti'];  
 
                   $val_session_time = (($am + $timestamp) < 1500) ?  true : false;            
 
           }  
 
                     
 
           if($val_session_time == true){    
 
               $sql = 'update session set session_ti = '.$timestamp.', session_di = '.$cur_day.' where session_id = '.$session_iid;
 
               mysql_query($sql);
 
               return($session_iid);  
 
           }else{  
 
          // Remove the session and create a new one as guest. using these sql statements //
 
             $sql = 'delete from session where session_id ='.$session_iid;
 
             $sql = 'insert into session(session_ip, session_ti, session_di) values("'.$user_ip.'","'.$timestamp.'","'.$cur_day.'")';
 
         $sql = 'select session_id from session where session_ip = '.$user_ip;              
 
 
 
 
 
             return ($row['session_id']);  
 
           }  
 
     }else{  
 
         // We have a new user  (guest) so create a new session.//  
 
         $sql = 'insert into session(session_ip, session_ti, session_di) values("'.$user_ip.'","'.$timestamp.'","'.$cur_day.'")';
 
     $sql = 'select session_id from session where session_ip = '.$user_ip;              
 
         return($row['session_id']);  
 
    }  
 
}  
 
 
 
Function get_sessions(){
 
    $sql = 'count session_id as total,
 
               count distinct($session_us) as registered
 
               from session
 
               where session_id is not null';
 
    $result = mysql_query($sql);
 
    $row = mysql_fetch_array($result);
 
     
 
    $guests = $row['total'] - $row['registered'];
 
    $online['guests'] = $guests;
 
    $online['registered'] = $row['registered'];
 
    return($online);
 
}
 
 
 
 
 
 
 
 
 
// Start of the page / website  //
 
session_start();  
 
$_SESSION['session_id'] = handle_session($_SESSION['session_id']);
 
$who_online = get_sessions();
 
?>
 
 
 
<html>
 
  <table width="40">
 
    <tr>
 
        <td>guests</td><td> :</td><td><?php echo "$who_online['guests']"; ?></td>
 
    </tr>
 
    <tr>
 
        <td>Registered</td><td> :</td><td><?php echo "$who_online['registered']"; ?></td>
 
        </tr>
 
  </table>
 
</html>
 
 
  1. <?php
  2.  
  3. // All you need to know  Still requires some work on the "returns" and the sql queries needed.
  4.  
  5. // But then again some work should be done by yourself too...  
  6.  
  7. // good luck.
  8.  
  9.  
  10.  
  11. function handle_session($session_iid){  
  12.  
  13.      
  14.  
  15.     // Set stuff to work with //  
  16.  
  17.     $raw_time = mktime();  
  18.  
  19.     $timestamp = $raw_time['hour'].$raw_time['minute'].$raw_time['second'];  
  20.  
  21.     $cur_day = date(d);  
  22.  
  23.     $user_ip = isset($_SESSION['user_ip']) ? $_SESSION['user_ip'] : $_SERVER['REMOTE_ADDR'];  
  24.  
  25.      
  26.  
  27. // Old session clean up so we keep valid data //
  28.  
  29.     $sql = 'select * from session';  
  30.  
  31.     $result = mysql_query($sql);  
  32.  
  33.     while($row = mysql_fetch_array($result)){
  34.  
  35.  
  36.  
  37.         if($row['session_di'] == $cur_day){  
  38.  
  39.             $val_session_time = (($row['session_ti'] - $timestamp) < 1500) ? true :  
  40.  
  41.         }else{  
  42.  
  43.             $am = "600000" - $row['session_ti'];                          
  44.  
  45.             $val_session_time = (($am + $timestamp) < 1500) ?  true : false;            
  46.  
  47.         }  
  48.  
  49.          
  50.  
  51.     // Found old session! Delete the B#$ch //          
  52.  
  53.         if($val_session_time == false){  
  54.  
  55.         $sql = 'delete from session where session_id ='.$row['session_id'];
  56.  
  57.         if($result = mysql_query($sql){
  58.  
  59.         next;
  60.  
  61.             }
  62.  
  63.         }  
  64.  
  65.     }  
  66.  
  67.     // Check Current Session //
  68.  
  69.     if(isset($session_iid) ){  
  70.  
  71.            // Find the session in the table
  72.  
  73.            $sql = 'select * from session where session_id ='.$session_iid;  
  74.  
  75.            $result = mysql_query($sql);  
  76.  
  77.            $row = mysql_fetch_array($result);  
  78.  
  79.            
  80.  
  81.         // validate session age //          
  82.  
  83.            if($row['session_di'] == $cur_day){  
  84.  
  85.                    $val_session_time = (($row['session_ti'] - $timestamp)) < 1500) ? true :  
  86.  
  87.            }else{  
  88.  
  89.                    $am = "600000" - $row['session_ti'];  
  90.  
  91.                    $val_session_time = (($am + $timestamp) < 1500) ?  true : false;            
  92.  
  93.            }  
  94.  
  95.                      
  96.  
  97.            if($val_session_time == true){    
  98.  
  99.                $sql = 'update session set session_ti = '.$timestamp.', session_di = '.$cur_day.' where session_id = '.$session_iid;
  100.  
  101.                mysql_query($sql);
  102.  
  103.                return($session_iid);  
  104.  
  105.            }else{  
  106.  
  107.           // Remove the session and create a new one as guest. using these sql statements //
  108.  
  109.              $sql = 'delete from session where session_id ='.$session_iid;
  110.  
  111.              $sql = 'insert into session(session_ip, session_ti, session_di) values("'.$user_ip.'","'.$timestamp.'","'.$cur_day.'")';
  112.  
  113.          $sql = 'select session_id from session where session_ip = '.$user_ip;              
  114.  
  115.  
  116.  
  117.  
  118.  
  119.              return ($row['session_id']);  
  120.  
  121.            }  
  122.  
  123.      }else{  
  124.  
  125.          // We have a new user  (guest) so create a new session.//  
  126.  
  127.          $sql = 'insert into session(session_ip, session_ti, session_di) values("'.$user_ip.'","'.$timestamp.'","'.$cur_day.'")';
  128.  
  129.      $sql = 'select session_id from session where session_ip = '.$user_ip;              
  130.  
  131.          return($row['session_id']);  
  132.  
  133.     }  
  134.  
  135. }  
  136.  
  137.  
  138.  
  139. Function get_sessions(){
  140.  
  141.     $sql = 'count session_id as total,
  142.  
  143.                count distinct($session_us) as registered
  144.  
  145.                from session
  146.  
  147.                where session_id is not null';
  148.  
  149.     $result = mysql_query($sql);
  150.  
  151.     $row = mysql_fetch_array($result);
  152.  
  153.      
  154.  
  155.     $guests = $row['total'] - $row['registered'];
  156.  
  157.     $online['guests'] = $guests;
  158.  
  159.     $online['registered'] = $row['registered'];
  160.  
  161.     return($online);
  162.  
  163. }
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. // Start of the page / website  //
  174.  
  175. session_start();  
  176.  
  177. $_SESSION['session_id'] = handle_session($_SESSION['session_id']);
  178.  
  179. $who_online = get_sessions();
  180.  
  181. ?>
  182.  
  183.  
  184.  
  185. <html>
  186.  
  187.   <table width="40">
  188.  
  189.     <tr>
  190.  
  191.         <td>guests</td><td> :</td><td><?php echo "$who_online['guests']"; ?></td>
  192.  
  193.     </tr>
  194.  
  195.     <tr>
  196.  
  197.         <td>Registered</td><td> :</td><td><?php echo "$who_online['registered']"; ?></td>
  198.  
  199.         </tr>
  200.  
  201.   </table>
  202.  
  203. </html>
  204.  
  205.  

Now the error...
Parse error: parse error, unexpected '}' in /home/online_OZZU.php on line 22

Line 22 is the 1st
}else{
command

Here's hoping I can get this working.
Thanks for all the input may
  • meman
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3431
  • Loc: London Town , Apples and pears and all that crap

Post 3+ Months Ago

It looks like the line before }else{ ends with a : when it should end with a ;

The same it true for the next }else{ statment.
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

meman wrote:
It looks like the line before }else{ ends with a : when it should end with a ;

The same it true for the next }else{ statment.



Parse error: parse error, unexpected ';' in /home/online_OZZU.php on line 22

?
  • meman
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3431
  • Loc: London Town , Apples and pears and all that crap

Post 3+ Months Ago

hmmm.. I don't know.
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

Functional code below....
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

may wrote:
first of all, not obvious errors, like i stated not complete code. :)


I was poking fun at myself...since I have no clue when it comes to php.
No offense to you and your kindness in helping me.

Now back to business.. I made it to the html part. Here's my latest error
(I did fix a cpl more along the way)
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/online_OZZU.php on line 98
Line 98 is...
Code: [ Select ]
    <td>guests</td><td> :</td><td><?php echo "$who_online['guests']"; ?></td>

Again thank you for any help..Hooch
P.S I'll post the finished code once I (well I hope to) finish.
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

Admin, can you remove all my "blank" posts? thx..

greets may,
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

Finished the code by adding a mysql class and pouring the session stuff in a class also, before more people start "fixing" incomplete code :P (not a personal note btw)

Hopes this solves your problem hooch, if you have any questions then just request tutorials on rabiddog its tutorial site, found in its "nick" ;)

but on the side, I see loads of questions from your part.. what are you creating anyways? Is it a school project or something?


PHP Code: [ Select ]
 
/**/
 
/*
 
 *   Session handling classes written for dear Hooch
 
 *   1 Mysql connection class (also see <!-- w --><span class="postlink">http://www.rabiddog.co.za</span><!-- w -->)      
 
 *   1 Session_handling class
 
 */
 
 
 
/* 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);
 
 */
 
 
 
class mysql{
 
   
 
   var $username;
 
   var $password;
 
   var $hostname;
 
   var $database;
 
   var $error;
 
   
 
   //Get vars from user (somewhere) or just here (savest)//
 
   function set_vars($host, $user, $pass, $db){
 
      $this->hostname = (!empty($host)) ? $host : "127.0.0.1";
 
      $this->username = (!empty($user)) ? $user : "someuser";
 
      $this->password = (!empty($pass)) ? $pass : "somepassword";
 
      $this->database = (!empty($db)) ? $db : "somedatabase";
 
   }
 
   
 
   function unset_vars(){
 
      unset($this->hostname);
 
      unset($this->username);
 
      unset($this->password);
 
      unset($this->database);
 
   }
 
   
 
   //Initialise the database connection//
 
   function init_con(){
 
      if($con_str = mysql_connect($this->hostname, $this->username, $this->password) ){
 
         if(mysql_select_db($this->database, $con_str)){
 
            return($con_str);
 
         }else{
 
            return(false);
 
         }
 
      }else{
 
         return(false);
 
      }
 
   }
 
   
 
   function close_con($con_str){
 
      if(!empty($con_str) ){
 
         mysql_close($con_str);
 
      }
 
   }
 
   
 
   function query($sql){
 
      $con_str = $this->init_con();
 
      if(is_resource($con_str)){
 
         $result = mysql_query($sql);
 
         if(is_resource($result)){
 
            $this->close_con($con_str);
 
            return($result);
 
         }else{
 
            $this->close_con($con_str);
 
              return(false);
 
         }
 
      }else{
 
         return(false);
 
      }  
 
   }
 
}
 
 
 
class Session{
 
   
 
   var $timestamp;
 
   var $cur_day;
 
   var $user_ip;
 
   var $max_time;
 
   
 
   function get_data(){
 
         
 
         $this->timestamp = date('His');  
 
         $this->cur_day = date('d');  
 
         $this->user_ip = isset($_SESSION['user_ip']) ? $_SESSION['user_ip'] : $_SERVER['REMOTE_ADDR'];
 
         $this->max_time = '500'; //5 mins MMSS/
 
   }
 
   
 
   function val_session($day, $time){
 
     
 
      if($day == $this->cur_day){  
 
            $val_session_time = (($this->timestamp - $time) < $this->max_time) ? true : false;
 
        }else{  
 
            $am = "600000" - $time;                          
 
            $val_session_time = (($am + $this->timestamp) < $this->max_time) ?  true : false;            
 
        }
 
        return($val_session_time);
 
     }
 
     
 
     function create_session(){
 
      $oMYSQL = new mysql;
 
      $oMYSQL->set_vars("","","","");
 
      $sql = 'insert into session(session_ip, session_ti, session_di) values("'.$this->user_ip.'","'.$this->timestamp.'","'.$this->cur_day.'")';
 
      $oMYSQL->query($sql);
 
         
 
      $sql = 'select session_id from session where session_ip = "'.$this->user_ip.'"';
 
      $result = $oMYSQL->query($sql);
 
      $row = mysql_fetch_array($result);
 
      return($row['session_id']);
 
     }
 
     
 
     function update_session($session_id){
 
      $oMYSQL = new mysql;
 
      $oMYSQL->set_vars("","","","");
 
      if(!empty($session_id)){
 
         $sql = 'update session
 
               set session_ti = "'.$this->timestamp.'", session_di = "'.$this->cur_day.'"
 
               where session_id = "'.$session_id.'"';
 
         if($oMYSQL->query($sql)){
 
            return(true);
 
         }else{
 
            return(false);
 
         }
 
      }
 
     }
 
     
 
     function delete_session($session_id){
 
      $oMYSQL = new mysql;
 
      $oMYSQL->set_vars("","","","");
 
      if(!empty($session_id)){
 
         $sql = 'delete from session where session_id = '.$session_id;
 
         if($oMYSQL->query($sql)){
 
            return(true);
 
         }else{
 
            return(false);
 
         }
 
      }
 
     }
 
     
 
     function clean_up_db(){
 
      $oMYSQL = new mysql;
 
      $oMYSQL->set_vars("","","","");
 
     
 
      $sql = 'select * from session';
 
     
 
      $result = $oMYSQL->query($sql);
 
      while($row = mysql_fetch_array($result)){
 
         
 
         $validate = $this->val_session($row['session_di'], $row['session_ti']);
 
         
 
         if($validate == false){
 
            if($this->delete_session($row['session_id'])){
 
               unset($validate);
 
            }else{
 
               // This is not nice.. but hey <img src="./images/smilies/icon_wink.gif" alt=";)" title="Wink" /> //
 
               unset($validate);
 
            }
 
         }else{
 
            unset($validate);
 
         }
 
      }
 
     }     
 
     
 
     function handle_session(){
 
      // Get the data //
 
      $this->get_data();
 
     
 
      // First cleanup the database //
 
      $this->clean_up_db();
 
     
 
      // Start the session prepare mysql //
 
      $oMYSQL = new mysql;
 
      $oMYSQL->set_vars("","","","");
 
     
 
      if(isset($_SESSION['session_id']) ){
 
         $sql = 'select * from session where session_id = "'.$_SESSION['session_id'].'"';
 
         $result = $oMYSQL->query($sql);
 
         $row = mysql_fetch_array($result);
 
         $validate = $this->val_session($row['session_di'], $row['session_ti']);
 
         
 
         if($validate == true){
 
            $this->update_session($_SESSION['session_id']);
 
         }else{
 
            // Might be used to strip the "login" then extend this part//
 
            // Now the "guest" will only get a new id, usually someone //
 
            // Is altered from "reg" status to "guest" status          //
 
            $this->delete_session($_SESSION['session_id']);
 
            $_SESSION['session_id'] = $this->create_session();
 
         }
 
      }else{
 
         $_SESSION['session_id'] = $this->create_session();
 
      }  
 
      }
 
     
 
     
 
      Function get_sessions(){
 
         $oMYSQL = new mysql;
 
      $oMYSQL->set_vars("","","","");
 
         $sql = 'select count(session_us) as registered,
 
            count(session_id) as total
 
            from session';
 
         if($result = $oMYSQL->query($sql)){
 
            $row = mysql_fetch_array($result);
 
      }
 
     
 
      if(empty($row['registered']) ){
 
         $guests = $row['total'];
 
      }else{
 
         $guests = $row['total'] - $row['registered'];
 
      }
 
         $online['guests'] = $guests;
 
         $online['registered'] = $row['registered'];
 
         
 
         return($online);
 
   }
 
}
 
   
 
session_start();    
 
$oSESSION = new Session;
 
$oSESSION->handle_session();
 
$online = $oSESSION->get_sessions();   
 
 
 
print ('session_id = '.$_SESSION['session_id']);
 
     
 
print_r('
 
 
 
<html>
 
 <table width="40">
 
   <tr>
 
       <td>guests</td><td> :</td><td>'.$online['guests'].'</td>
 
   </tr>
 
   <tr>
 
       <td>Registered</td><td> :</td><td>'.$online['registered'].'</td>
 
       </tr>
 
 </table>
 
</html>
 
 
 
');
 
 
 
 
  1.  
  2. /**/
  3.  
  4. /*
  5.  
  6.  *   Session handling classes written for dear Hooch
  7.  
  8.  *   1 Mysql connection class (also see <!-- w --><span class="postlink">http://www.rabiddog.co.za</span><!-- w -->)      
  9.  
  10.  *   1 Session_handling class
  11.  
  12.  */
  13.  
  14.  
  15.  
  16. /* create table session(
  17.  
  18.  *   session_id INT unsigned not null auto_increment primary key,
  19.  
  20.  *   session_ip varchar(15) not null,
  21.  
  22.  *   session_ti INT(6) not null,
  23.  
  24.  *   session_di INT(2) not null,
  25.  
  26.  *   session_us varchar(30) null);
  27.  
  28.  */
  29.  
  30.  
  31.  
  32. class mysql{
  33.  
  34.    
  35.  
  36.    var $username;
  37.  
  38.    var $password;
  39.  
  40.    var $hostname;
  41.  
  42.    var $database;
  43.  
  44.    var $error;
  45.  
  46.    
  47.  
  48.    //Get vars from user (somewhere) or just here (savest)//
  49.  
  50.    function set_vars($host, $user, $pass, $db){
  51.  
  52.       $this->hostname = (!empty($host)) ? $host : "127.0.0.1";
  53.  
  54.       $this->username = (!empty($user)) ? $user : "someuser";
  55.  
  56.       $this->password = (!empty($pass)) ? $pass : "somepassword";
  57.  
  58.       $this->database = (!empty($db)) ? $db : "somedatabase";
  59.  
  60.    }
  61.  
  62.    
  63.  
  64.    function unset_vars(){
  65.  
  66.       unset($this->hostname);
  67.  
  68.       unset($this->username);
  69.  
  70.       unset($this->password);
  71.  
  72.       unset($this->database);
  73.  
  74.    }
  75.  
  76.    
  77.  
  78.    //Initialise the database connection//
  79.  
  80.    function init_con(){
  81.  
  82.       if($con_str = mysql_connect($this->hostname, $this->username, $this->password) ){
  83.  
  84.          if(mysql_select_db($this->database, $con_str)){
  85.  
  86.             return($con_str);
  87.  
  88.          }else{
  89.  
  90.             return(false);
  91.  
  92.          }
  93.  
  94.       }else{
  95.  
  96.          return(false);
  97.  
  98.       }
  99.  
  100.    }
  101.  
  102.    
  103.  
  104.    function close_con($con_str){
  105.  
  106.       if(!empty($con_str) ){
  107.  
  108.          mysql_close($con_str);
  109.  
  110.       }
  111.  
  112.    }
  113.  
  114.    
  115.  
  116.    function query($sql){
  117.  
  118.       $con_str = $this->init_con();
  119.  
  120.       if(is_resource($con_str)){
  121.  
  122.          $result = mysql_query($sql);
  123.  
  124.          if(is_resource($result)){
  125.  
  126.             $this->close_con($con_str);
  127.  
  128.             return($result);
  129.  
  130.          }else{
  131.  
  132.             $this->close_con($con_str);
  133.  
  134.               return(false);
  135.  
  136.          }
  137.  
  138.       }else{
  139.  
  140.          return(false);
  141.  
  142.       }  
  143.  
  144.    }
  145.  
  146. }
  147.  
  148.  
  149.  
  150. class Session{
  151.  
  152.    
  153.  
  154.    var $timestamp;
  155.  
  156.    var $cur_day;
  157.  
  158.    var $user_ip;
  159.  
  160.    var $max_time;
  161.  
  162.    
  163.  
  164.    function get_data(){
  165.  
  166.          
  167.  
  168.          $this->timestamp = date('His');  
  169.  
  170.          $this->cur_day = date('d');  
  171.  
  172.          $this->user_ip = isset($_SESSION['user_ip']) ? $_SESSION['user_ip'] : $_SERVER['REMOTE_ADDR'];
  173.  
  174.          $this->max_time = '500'; //5 mins MMSS/
  175.  
  176.    }
  177.  
  178.    
  179.  
  180.    function val_session($day, $time){
  181.  
  182.      
  183.  
  184.       if($day == $this->cur_day){  
  185.  
  186.             $val_session_time = (($this->timestamp - $time) < $this->max_time) ? true : false;
  187.  
  188.         }else{  
  189.  
  190.             $am = "600000" - $time;                          
  191.  
  192.             $val_session_time = (($am + $this->timestamp) < $this->max_time) ?  true : false;            
  193.  
  194.         }
  195.  
  196.         return($val_session_time);
  197.  
  198.      }
  199.  
  200.      
  201.  
  202.      function create_session(){
  203.  
  204.       $oMYSQL = new mysql;
  205.  
  206.       $oMYSQL->set_vars("","","","");
  207.  
  208.       $sql = 'insert into session(session_ip, session_ti, session_di) values("'.$this->user_ip.'","'.$this->timestamp.'","'.$this->cur_day.'")';
  209.  
  210.       $oMYSQL->query($sql);
  211.  
  212.          
  213.  
  214.       $sql = 'select session_id from session where session_ip = "'.$this->user_ip.'"';
  215.  
  216.       $result = $oMYSQL->query($sql);
  217.  
  218.       $row = mysql_fetch_array($result);
  219.  
  220.       return($row['session_id']);
  221.  
  222.      }
  223.  
  224.      
  225.  
  226.      function update_session($session_id){
  227.  
  228.       $oMYSQL = new mysql;
  229.  
  230.       $oMYSQL->set_vars("","","","");
  231.  
  232.       if(!empty($session_id)){
  233.  
  234.          $sql = 'update session
  235.  
  236.                set session_ti = "'.$this->timestamp.'", session_di = "'.$this->cur_day.'"
  237.  
  238.                where session_id = "'.$session_id.'"';
  239.  
  240.          if($oMYSQL->query($sql)){
  241.  
  242.             return(true);
  243.  
  244.          }else{
  245.  
  246.             return(false);
  247.  
  248.          }
  249.  
  250.       }
  251.  
  252.      }
  253.  
  254.      
  255.  
  256.      function delete_session($session_id){
  257.  
  258.       $oMYSQL = new mysql;
  259.  
  260.       $oMYSQL->set_vars("","","","");
  261.  
  262.       if(!empty($session_id)){
  263.  
  264.          $sql = 'delete from session where session_id = '.$session_id;
  265.  
  266.          if($oMYSQL->query($sql)){
  267.  
  268.             return(true);
  269.  
  270.          }else{
  271.  
  272.             return(false);
  273.  
  274.          }
  275.  
  276.       }
  277.  
  278.      }
  279.  
  280.      
  281.  
  282.      function clean_up_db(){
  283.  
  284.       $oMYSQL = new mysql;
  285.  
  286.       $oMYSQL->set_vars("","","","");
  287.  
  288.      
  289.  
  290.       $sql = 'select * from session';
  291.  
  292.      
  293.  
  294.       $result = $oMYSQL->query($sql);
  295.  
  296.       while($row = mysql_fetch_array($result)){
  297.  
  298.          
  299.  
  300.          $validate = $this->val_session($row['session_di'], $row['session_ti']);
  301.  
  302.          
  303.  
  304.          if($validate == false){
  305.  
  306.             if($this->delete_session($row['session_id'])){
  307.  
  308.                unset($validate);
  309.  
  310.             }else{
  311.  
  312.                // This is not nice.. but hey <img src="./images/smilies/icon_wink.gif" alt=";)" title="Wink" /> //
  313.  
  314.                unset($validate);
  315.  
  316.             }
  317.  
  318.          }else{
  319.  
  320.             unset($validate);
  321.  
  322.          }
  323.  
  324.       }
  325.  
  326.      }     
  327.  
  328.      
  329.  
  330.      function handle_session(){
  331.  
  332.       // Get the data //
  333.  
  334.       $this->get_data();
  335.  
  336.      
  337.  
  338.       // First cleanup the database //
  339.  
  340.       $this->clean_up_db();
  341.  
  342.      
  343.  
  344.       // Start the session prepare mysql //
  345.  
  346.       $oMYSQL = new mysql;
  347.  
  348.       $oMYSQL->set_vars("","","","");
  349.  
  350.      
  351.  
  352.       if(isset($_SESSION['session_id']) ){
  353.  
  354.          $sql = 'select * from session where session_id = "'.$_SESSION['session_id'].'"';
  355.  
  356.          $result = $oMYSQL->query($sql);
  357.  
  358.          $row = mysql_fetch_array($result);
  359.  
  360.          $validate = $this->val_session($row['session_di'], $row['session_ti']);
  361.  
  362.          
  363.  
  364.          if($validate == true){
  365.  
  366.             $this->update_session($_SESSION['session_id']);
  367.  
  368.          }else{
  369.  
  370.             // Might be used to strip the "login" then extend this part//
  371.  
  372.             // Now the "guest" will only get a new id, usually someone //
  373.  
  374.             // Is altered from "reg" status to "guest" status          //
  375.  
  376.             $this->delete_session($_SESSION['session_id']);
  377.  
  378.             $_SESSION['session_id'] = $this->create_session();
  379.  
  380.          }
  381.  
  382.       }else{
  383.  
  384.          $_SESSION['session_id'] = $this->create_session();
  385.  
  386.       }  
  387.  
  388.       }
  389.  
  390.      
  391.  
  392.      
  393.  
  394.       Function get_sessions(){
  395.  
  396.          $oMYSQL = new mysql;
  397.  
  398.       $oMYSQL->set_vars("","","","");
  399.  
  400.          $sql = 'select count(session_us) as registered,
  401.  
  402.             count(session_id) as total
  403.  
  404.             from session';
  405.  
  406.          if($result = $oMYSQL->query($sql)){
  407.  
  408.             $row = mysql_fetch_array($result);
  409.  
  410.       }
  411.  
  412.      
  413.  
  414.       if(empty($row['registered']) ){
  415.  
  416.          $guests = $row['total'];
  417.  
  418.       }else{
  419.  
  420.          $guests = $row['total'] - $row['registered'];
  421.  
  422.       }
  423.  
  424.          $online['guests'] = $guests;
  425.  
  426.          $online['registered'] = $row['registered'];
  427.  
  428.          
  429.  
  430.          return($online);
  431.  
  432.    }
  433.  
  434. }
  435.  
  436.    
  437.  
  438. session_start();    
  439.  
  440. $oSESSION = new Session;
  441.  
  442. $oSESSION->handle_session();
  443.  
  444. $online = $oSESSION->get_sessions();   
  445.  
  446.  
  447.  
  448. print ('session_id = '.$_SESSION['session_id']);
  449.  
  450.      
  451.  
  452. print_r('
  453.  
  454.  
  455.  
  456. <html>
  457.  
  458.  <table width="40">
  459.  
  460.    <tr>
  461.  
  462.        <td>guests</td><td> :</td><td>'.$online['guests'].'</td>
  463.  
  464.    </tr>
  465.  
  466.    <tr>
  467.  
  468.        <td>Registered</td><td> :</td><td>'.$online['registered'].'</td>
  469.  
  470.        </tr>
  471.  
  472.  </table>
  473.  
  474. </html>
  475.  
  476.  
  477.  
  478. ');
  479.  
  480.  
  481.  
  482.  


I have tested this code, if you want to include the "registered" you should add something like "if isset($_SESSION['username']){ then update with session_us }. Now only the "guests" or viewers are functional. Also reduced the session time to 5 mins, this because 5min of idle time is a long while ;)

ow well, cu and good luck :D

good luck m8

ps. DO add valid mysql connection info in the top function (someuser, somepass, somedatabase, 127.0.0.1)
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

xN3uRoNx wrote:
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



i dont think its wise to handle session information in the users table, first how are you going to count all the "guests" on you page. Next there is allot more information about "a" session you might want to store during that session, say what page one is browsing at the moment, say how long he was browsing, say what actions he did (secure pages / logging) etc. Dont want to store all this info in your users table ;)

you can use all this info to make your page more attractive, or more accesible (100 users left on that page doint this... error?). Session data might be more usefull that one might think in a glace ;) or just flush it like the above example if you just would like to see the "current" users browsing :)


ow ps. if you only like to know if someone is online, then why would you need 255 chars to tell that? INT(1) would be enough... ;)
You can simply validate a bool value like :

0 being False
1 being true

will reduce allot of overhead on you tables..

-Greets
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

Yay for may!

I'll have to give he code a try as soon as I get home tonight.
Thank you and everyone else who has been helping me here.

You should put your name at the top of ths script, you deserve credit.
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

HoocH wrote:
You should put your name at the top of ths script, you deserve credit.


just create a link to http://www.rabiddog.co.za he helped me make this possible for you

and on reply to you question how to show the "online users"

update this function and insert the "additional" username in session_us. You shouldnt do it in the "create_session" because the session is allready started before someone logs in. If you use a portal, there is bound to be some user object wich holds the userdata, validate if it`s set if so, update the "session_us" column.

PHP Code: [ Select ]
 
// Additional code has been commented out! //
 
 
 
function update_session($session_id /*, $some_user_id*/ ){
 
         $oMYSQL = new mysql;
 
         $oMYSQL->set_vars("","","","");
 
 
 
         if(!empty($session_id)){
 
             $sql = 'update session
 
                   set session_ti = "'.$this->timestamp.'", session_di = "'.$this->cur_day.'" /*, session_us = "'.$some_user_id.'" */;
 
                   where session_id = "'.$session_id.'"';
 
             if($oMYSQL->query($sql)){
 
                 return(true);
 
             }else{
 
                 return(false);
 
             }
 
         }
 
     }
 
 
  1.  
  2. // Additional code has been commented out! //
  3.  
  4.  
  5.  
  6. function update_session($session_id /*, $some_user_id*/ ){
  7.  
  8.          $oMYSQL = new mysql;
  9.  
  10.          $oMYSQL->set_vars("","","","");
  11.  
  12.  
  13.  
  14.          if(!empty($session_id)){
  15.  
  16.              $sql = 'update session
  17.  
  18.                    set session_ti = "'.$this->timestamp.'", session_di = "'.$this->cur_day.'" /*, session_us = "'.$some_user_id.'" */;
  19.  
  20.                    where session_id = "'.$session_id.'"';
  21.  
  22.              if($oMYSQL->query($sql)){
  23.  
  24.                  return(true);
  25.  
  26.              }else{
  27.  
  28.                  return(false);
  29.  
  30.              }
  31.  
  32.          }
  33.  
  34.      }
  35.  
  36.  


and

PHP Code: [ Select ]
 
function handle_session(  /*$some_user_id */){
 
         // Get the data //
 
         $this->get_data();
 
         
 
         // First cleanup the database //
 
         $this->clean_up_db();
 
         
 
         // Start the session prepare mysql //
 
         $oMYSQL = new mysql;
 
         $oMYSQL->set_vars("","","","");
 
         
 
         if(isset($_SESSION['session_id']) ){
 
             $sql = 'select * from session where session_id = "'.$_SESSION['session_id'].'"';
 
             $result = $oMYSQL->query($sql);
 
             $row = mysql_fetch_array($result);
 
             $validate = $this->val_session($row['session_di'], $row['session_ti']);
 
             
 
             if($validate == true){
 
                 $this->update_session($_SESSION['session_id'] /* , $some_user_obj */);
 
             }else{
 
                 // Might be used to strip the "login" then extend this part//
 
                 // Now the "guest" will only get a new id, usually someone //
 
                 // Is altered from "reg" status to "guest" status          //
 
                 $this->delete_session($_SESSION['session_id']);
 
                 $_SESSION['session_id'] = $this->create_session();
 
             }
 
         }else{
 
             $_SESSION['session_id'] = $this->create_session();
 
         }  
 
      }  
 
 
  1.  
  2. function handle_session(  /*$some_user_id */){
  3.  
  4.          // Get the data //
  5.  
  6.          $this->get_data();
  7.  
  8.          
  9.  
  10.          // First cleanup the database //
  11.  
  12.          $this->clean_up_db();
  13.  
  14.          
  15.  
  16.          // Start the session prepare mysql //
  17.  
  18.          $oMYSQL = new mysql;
  19.  
  20.          $oMYSQL->set_vars("","","","");
  21.  
  22.          
  23.  
  24.          if(isset($_SESSION['session_id']) ){
  25.  
  26.              $sql = 'select * from session where session_id = "'.$_SESSION['session_id'].'"';
  27.  
  28.              $result = $oMYSQL->query($sql);
  29.  
  30.              $row = mysql_fetch_array($result);
  31.  
  32.              $validate = $this->val_session($row['session_di'], $row['session_ti']);
  33.  
  34.              
  35.  
  36.              if($validate == true){
  37.  
  38.                  $this->update_session($_SESSION['session_id'] /* , $some_user_obj */);
  39.  
  40.              }else{
  41.  
  42.                  // Might be used to strip the "login" then extend this part//
  43.  
  44.                  // Now the "guest" will only get a new id, usually someone //
  45.  
  46.                  // Is altered from "reg" status to "guest" status          //
  47.  
  48.                  $this->delete_session($_SESSION['session_id']);
  49.  
  50.                  $_SESSION['session_id'] = $this->create_session();
  51.  
  52.              }
  53.  
  54.          }else{
  55.  
  56.              $_SESSION['session_id'] = $this->create_session();
  57.  
  58.          }  
  59.  
  60.       }  
  61.  
  62.  


and

PHP Code: [ Select ]
 
 
 
/* $my_user_id (isset($some_user_obj['user_id']) ) ? $some_user_obj['username'] : ""; */
 
 
 
$oSESSION->handle_session( */ $my_user_id */);
 
 
 
 
  1.  
  2.  
  3.  
  4. /* $my_user_id (isset($some_user_obj['user_id']) ) ? $some_user_obj['username'] : ""; */
  5.  
  6.  
  7.  
  8. $oSESSION->handle_session( */ $my_user_id */);
  9.  
  10.  
  11.  
  12.  


hope you are keeping track of this. You need to supply the or "a" username wich can be updated to the session table. Net the username has to be send from the "handle_session" function inside the class to the "update_session" function also in that class. Now if you would like the "usernames" posted you should just "query" these where session_us is not null.

hope you get this ;)

good luck and greets May

ps. If you are actually using a portal (wich hasnt been named up to now) you should be verry carefull with using this code. The posibility is verry real that a session is allready being handled. This would implicate the "current" portal when we are setting "new" $_SESSION vars and might thus cause errors when browsing between you custom page and the "working" portal. And might even implicate the security of that portal.

If this is actually the case i advice you to study the source of the used portal and look if a session class or file is allready being used, and just use that file / class. Chance that would work even better then this "custom" script and wont cause all kinds of "duplicate" data in you database.

BTW Nicest way of doing this is just to pass the "key" of the user row into the session table and fetch the user data based on that key from the users table ;-)

This will allow you to fetch much more data on this user within "this" or "that" session, and make redirects possible to the "members list" when clicking one of the displayed "online" users or other fancy options ;)

-Greets
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

Well, I'm trying to follow here.
I have a database setup already.
Code: [ Select ]
$query = 'CREATE TABLE `users` (
id int(11) NOT NULL auto_increment,
username varchar(32) NOT NULL UNIQUE,
password varchar(32) NOT NULL,
firstname varchar(32) NOT NULL,
lastname varchar(32) NOT NULL,
email varchar(32) NOT NULL UNIQUE,
medium varchar(32) NOT NULL,
studio varchar(32) default NULL,
tours varchar(32) default NULL,
website varchar(32) default NULL,
address varchar(32) NOT NULL,
city varchar(32) NOT NULL,
province varchar(32) NOT NULL,
zip varchar(7) NOT NULL,
phone int(13) default NULL,
biography text NOT NULL,
PRIMARY KEY (`id`))
ENGINE=INNODB;';
  1. $query = 'CREATE TABLE `users` (
  2. id int(11) NOT NULL auto_increment,
  3. username varchar(32) NOT NULL UNIQUE,
  4. password varchar(32) NOT NULL,
  5. firstname varchar(32) NOT NULL,
  6. lastname varchar(32) NOT NULL,
  7. email varchar(32) NOT NULL UNIQUE,
  8. medium varchar(32) NOT NULL,
  9. studio varchar(32) default NULL,
  10. tours varchar(32) default NULL,
  11. website varchar(32) default NULL,
  12. address varchar(32) NOT NULL,
  13. city varchar(32) NOT NULL,
  14. province varchar(32) NOT NULL,
  15. zip varchar(7) NOT NULL,
  16. phone int(13) default NULL,
  17. biography text NOT NULL,
  18. PRIMARY KEY (`id`))
  19. ENGINE=INNODB;';

So I am using $username from my database inplace of "/*, $some_user_id*/" you kindly posted.
For "/* , $some_user_obj */" I am completely lost.
And ofcourse this is over my head too..
"/* $my_user_id (isset($some_user_obj['user_id']) ) ? $some_user_obj['username'] : ""; */ "

Man, you have been very helpful. But please don't feel you need to be so quick
on your replies, I'm sure you have better things to do. I am very patient.
Again thank you..Hooch
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

Could you explain this a little better?

Quote:
BTW Nicest way of doing this is just to pass the "key" of the user row into the session table and fetch the user data based on that key from the users table


Are you saying I can merge my existing users database?
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

Dont ever mention "merge" you are not denormalising you database and you arent a "data warehouse either" so no not "merge" more like "relate" the data together.

By defining the "user id" you are able to make an relation to the user table, this is not merging. It is also related to the "remark" you got based on "form normalisation" the "relative database" is designed for "relations" so use them as well..

Example :

Image

also easy to understand is this ;)

PHP Code: [ Select ]
 
$my_user_id = (isset($some_user_obj['user_id']) ) ? $some_user_obj['user_id'] : "";
 
 
  1.  
  2. $my_user_id = (isset($some_user_obj['user_id']) ) ? $some_user_obj['user_id'] : "";
  3.  
  4.  


and is actually the same as :

PHP Code: [ Select ]
 
 
 
if(isset($some_user_obj['user_id']) ){
 
      $my_user_id = $some_user_obj['user_id'];
 
}else{
 
      $my_user_id = "";
 
}
 
 
 
 
  1.  
  2.  
  3.  
  4. if(isset($some_user_obj['user_id']) ){
  5.  
  6.       $my_user_id = $some_user_obj['user_id'];
  7.  
  8. }else{
  9.  
  10.       $my_user_id = "";
  11.  
  12. }
  13.  
  14.  
  15.  
  16.  


Only difference is that i do the same on one rule.

Greets May
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

But what does $some_user_obj need to be replaced with?
Or does it need editing?
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

actually with a var containing the "user_id" of the user opening the page / continueing the session. If the user is logged on he "user_id" is know. So fetch it and just put it in there...

-May

Post Information

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

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