Ok, I'm still working on this script and hasn't been able to get it right fully yet. I'm am very new to PHP too.
[Do not code anything below until you read through it all 🙂 ]
Ok, when your users log in, they should be sent to a confirmation page where their username/pass is varified. That page should also set the session.
Put this code on that page.
<?php
/* declare some relevant variables */
$DBhost = "localhost"; //change this
$DBuser = "root"; //change this
$DBpass = "dbpassword"; //change this
$DBname = "database"; //change this
$cutout="600"; //seconds before timeout
$page=$_SERVER['REQUEST_URI'];
$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=time();
$timestamp=time();
if (empty($_SESSION['username'])) { $username=(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("$dbname");
$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='$username', hostname='$hostname', timestamp='$timestamp', page='$page', thetime='$thetime' WHERE lastip='$lastip'");
$result2=mysql_query($sql);
}
else
{
// insert users online
$timestamp = time();
$mysql = mysql_select_db( "$DBname" );
$sql="insert into users_online values('id','$username','$lastip','$hostname','$timestamp','$page','$thetime')";
$result2=mysql_query($sql);
}
?>
Ok, where you want to display who's on, (the main page or whatever) but this code
<?php
///////////////////////////////////
// DB Connect
///////////////////////////////////
$username = "root"; //change
$password = ""; //change
$hostname = "localhost"; //change
$dbname = ""; //change
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("$dbname",$dbh)
or die("Could not select database");
//----------------------------------
///////////////////////////////////
// Show user info from database
///////////////////////////////////
$result = mysql_query("SELECT ol_user FROM users_online Order by Timestamp ASC");
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
echo "$row[ol_user]";
echo ", ";
}
//----------------------------------
mysql_close($dbh);
?>
This is the table setup in MySQL. If you run PHPMyAdmin, you can create a database and run the SQL query from there:
CREATE TABLE users_online (
id varchar(10) NOT NULL default '',
ol_user text NOT NULL,
hostname text NOT NULL,
timestamp varchar(25) NOT NULL default '',
page text NOT NULL,
thetime time NOT NULL default '00:00:00',
lastip text NOT NULL
) TYPE=MyISAM;
That should do it. Again, this script has some bugs that I'm working on fixing. If you fix them before I do, let me know 😉
Good luck 🙂