Hi Guys,

I need to write a simple PHP for the following.

Logged in as: Shabbaranks
Last visited: 22 Hours Ago at 4:47 pm
Private Messages: Unread 2, Total 2.

Any ideas?

Which bit do you want help on? The time elapsed? Should it be updated regularly without page reload or only on reload? The reason I ask is that the latter is simple, but the former could use some COMET with the sleep() command. This is dependent on your version of PHP and wether you can use ob_flush() and flush() commands.

Name: That's simple, just use your session variable. ($_SESSION or something similiar.
Last logged in: Make a row in users named time(), update it each time they log in *after* you set a variable for current time + run a query to get the one in the database, otherwise it'll update with the time on login and then just re-grab that.
PMs: If you have a table named `pms`, you should have at least the following columns: to_id, from_id, title, message, read. Make read tinyint, and if it is set to 0 (when someone submits a PM, set it to 0) it means unread, and when someone opens a PM, set the value to 1. Count all the 0's where the to_id is $_SESSION for the Unreads, and then use mysql_num_rows for the total.

Typed this up quickly to give you an idea. It isn't, obviously, finished.

<?php

//You need to have a working login and PM system, as well as a "last logged in" column for each user.
//For our purposes, we'll assume you do.
//Alright? Reply if you need help.

//Get current time.
$login = time();
$getuser = mysql_query("SELECT * FROM `users` WHERE `users`.`id` = '$MyId'");
$userinfo = mysql_fetch_assoc($getuser);
$lastlogin = $userinfo['lastlogin'] - $login;

echo "Logged in as: " . $userinfo['username'];
echo "Last logged in: " . $lastlogin;
echo "PMs: ".$unread." unread, ".$read." read";

?>

You can, of course, manipulate time().

This article has been dead for over six months. Start a new discussion instead.