Jump to content

[SOLVED] Users Online - Possible Chat?


AshleyByrom

Recommended Posts

Okay well I already have a database with a table (users).

 

The table looks like this:

 

picture4feo.png

 

I would like a way, if possible to create a little users online script (Which would just basically echo their first and last name) and then if possible a little chat script.

 

I know the chat script might be asking for a bit much... but hey, it's worth a shot.

 

Thanks!! :)

Link to comment
https://forums.phpfreaks.com/topic/166773-solved-users-online-possible-chat/
Share on other sites

You should create a table for active users and active guests.. Have a look at the code below, this is something i used ..

 

<?php
include('config.php'); // absolute path to your config page

$query = "SELECT * FROM users";
$result = mysql_query($query);
$u = mysql_num_rows($result);

if(!$u)
{
	echo 'No data available';
}
else
    {
$time = time(); // set the time
$time_out = 300; // timeout
$guest_time_out = $time-300; // timeout set to second
$member = $_SESSION['username']; // seting a variable for the session
$ip = $_SERVER['REMOTE_ADDR']; // variable for the ip adresss

// this will check if the current guy is a guest or a logged user
//if he is not logged user execute these code
if(empty($member)){
// select from the active guest table to see if there is a record
//if there is a record, we update the row else we insert the ip in our table
	$sql = "SELECT * FROM active_guests WHERE ip = '$ip'";
	$result = mysql_query($sql,$link);
	$num = mysql_num_rows($result);
	//if now rows was found, insert our guest in the active_guests table
	if($num == 0)
	{
		$q = "INSERT INTO active_guests(ip,timestamp)VALUES('$ip','$time')";
		$res = mysql_query($q);
	}
	else
	{
		//if the ip was already there, we update the row
		$q = "UPDATE active_guests SET timestamp = '$time' WHERE ip = '$ip'";
		$res = mysql_query($q);
	}
	}

else{ 
//if the user is logged in, we delete the ip of the member in the active guest table so the logged
//doing so, the logged in user won't be consided as a guest anymore
$del = "DELETE FROM active_guests WHERE ip = '$ip'";
$delresult = mysql_query($del);
//now we'll check if the user was already there
$sql = "SELECT * FROM active_users WHERE username = '$member'";
	$result = mysql_query($sql);
	$num = mysql_num_rows($result);
	//if the user was already there,we'll update else we'll insert
	//so if no rows was found, we'll surely want to add him. right??
if($num == 0)
{
$sql = "INSERT INTO active_users(username,timestamp)VALUES('$member','$time')";
	$result = mysql_query($sql);
}else{
// else don't let him go, update the row for him
$sql = "UPDATE active_users SET timestamp = '$time' WHERE username = '$member'";
	$result = mysql_query($sql);

}
}
// Note that $guest_time_out is the number of seconds a guest is considered as guest
$sql4="DELETE FROM active_guests WHERE timestamp < $guest_time_out";
$result4=mysql_query($sql4) or die (mysql_error());
//grab result for visitors or guest



$cnx = "SELECT * FROM active_guests";
	$resultat = mysql_query($cnx);
	$visitors = mysql_num_rows($resultat);

	if($visitors == 1 || $visitors == 0)
	{
		echo "<br />Active Guest : <span style='font-weight:bold;'>". $visitors ."</span><br />";
	}
	else
	{
		echo "<br />Active Guest(s) : <span style='font-weight:bold;'>". $visitors ."</span>
		<br />";
	}
	//grab result for our logged in users
$grab = "SELECT * FROM active_users";
$result = mysql_query($grab);
$count = mysql_num_rows($result);


	if($count == 1)
	{
		echo "Member Online : <span style='font-weight:bold;'>".$count. "</span><br />";
	}
	else
	{
		echo "Member(s) Online : <span style='font-weight:bold;'>".$count. "</span><br />";
	}
}

?>

 

It is commented everywhere, so you should find your way around

I could but i placed the script in a members only area? Ill create a demo account now...

 

http://byrom.freehostia.com

 

It isn't fully finished yet... work in progress...

 

Company: demo

username: demo

password: demo

 

BTW: It is hosted on a free server so it may be pritty slow... with me being 15... I can't buy hosting just yet...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.