PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I'm writing a message board right now and I need help writing a script that shows how many users and guests (and displays the registered usernames) have been online in the past 5 minutes.
Can someone tell me how and what database I need to create with what tables and such? I use sessions on my forum, so when I user logs in, they are forwarded to a page that sets the session. I thought maybe that would be a good place where code can be put in to update a table with their login name and ip and such but since I'm very new to PHP I have no idea where to begin on this one.
<?php
$thetime=date("h:i:s A T");
$one=time();
$two=($one - $cutout);
$mysql = mysql_select_db( "$DBname" );
$sql = ("DELETE FROM users_online WHERE timestamp < '".$two."'");
$result=mysql_query($sql);
$lastip=($REMOTE_ADDR);
$hostname=(NA); $timestamp=time();
if (empty($_SESSION['valid_user'])) { $ol_user=(Guest); }
else { $ol_user=($valid_user); }
// if the user has just tried to log in
$db_conn = mysql_connect("localhost", "$DBuser", "$DBpass");
mysql_select_db("league");
$query = "select * from users_online where lastip='$lastip'";
$result007 = mysql_query($query, $db_conn);
if (mysql_num_rows($result007) >0 )
{ // then we update
$mysql = mysql_select_db( "$DBname" );
$sql=("UPDATE users_online SET ol_user='$ol_user', hostname='$hostname', timestamp='$timestamp', page='$page', thetime='$thetime' WHERE lastip='$lastip'");
$result2=mysql_query($sql);
}
else
{
// insert users online
$mysql = mysql_select_db( "$DBname" );
$sql="insert into users_online values('id','$ol_user','$lastip','$hostname','$timestamp','$page','$thetime')";
$result2=mysql_query($sql);
}
?>
Now $cutout is in my config file so I can set the time there.
if you use this or something like that you will have your database entries. then you need to build your whos online viewing. I use 2. One to show the total people online ( This is in a side box) then a web page to show where they are.
Thanks.
How many tables should I create in the database and what fields should they have? I'm new to PHP. I need a little more info than you gave me. Sorry.
Edited: Ok, I sort of got it now. How do I make it display users that have been on in the last 5 minutes ONLY?
Sorry. If you look at the insert into the database you see how many. In the case that i gave you anyway. I have 1 there with 7 fields. The hostname is not used yet but eventually will be.