Is there anyone with the knowledge on how to Show online users with php??
i have seen in many sites,now i need someone who can explain to do it.
(if there are users who are online to be displayed)
Please explain how to get started))))

Recommended Answers

All 18 Replies

Well, I'm not expert with PHP, but I would suggest creating an SQL table called onlineUsers or somthing like that. Then when users login a PHP script adds them to the table. When they logout, simply remove them from the list.

Something like:

<?php
//Log the user in
$con = mysql_connect("localhost","username","password");
mysql_query("INSERT INTO onlineUsers (username) VALUE ('$username')") //Assuming the user's username is stored in $username
?>

And on Logout:

<?php
//Clear Cookies, etc.
$con = mysql_connect("localhost","username","password");
mysql_query("DELETE FROM onlineUsers WHERE username = '$username'") //Assuming the user's username is stored in $username
?>

This is great idea,i`m working on it.

Feel free to PM me if you need any help!!!

when someone is login his datas are been sent to ONLINE TABLE.
THE problem comes when login Out,the query is not deleting his datas.
Check the code below,is the one i`m using for login out.It was working but after i added this DELETE is no longer.

<?php
session_start();
$user=$_SESSION['user'];
if(session_is_registered('user')){
$del="DELETE FROM online WHERE user='$user'";
$true=mysql_query($del);


if($true){
echo"member offline";


}else{
echo"failed to delete";
}




session_unset();
session_destroy();


//include"index.php";


//exit();
}else{
include"index.php";

}


?>

Ok then, try this:

<?php
session_start();

if(session_is_registered('user'))
{
     $user=$_SESSION['user'];
     $del="DELETE FROM online WHERE user='$user'";
     $result=mysql_query($del);

     if($result==true)
     {
          echo "<h3>Member Offline</h3>\n";
     }
     else
     {
         echo "<h3>Failed to Logoff</h3>\n";
     }
     
     //Remove the Session Vars
     $_SESSION = array();
     if (isset($_COOKIE[session_name()])) {
          setcookie(session_name(), '', time()-42000, '/');
     }
     session_destroy();


     //include"index.php";

}
else
{
    include "index.php";
}
?>
commented: Good Helping.... +2

best way to store session and count how many session ............

The simplest way to do this would be to read the number of active sessions as nikesh.yadav said.

I have added the code I use on my sites to give an example:

<?
$number=0;
// define the directory to save sessions in
session_save_path("session_directory");
session_start();
// set the session as active
$_SESSION['active']="yes";
$directory = opendir("sessions");
while($item = readdir($directory))
{
  if(($item != ".") && ($item != ".."))
  {
// count the number os sessions active within the last x sexonds
    if(fileatime('sessions/' . $item) > time()-120)
    {
// add 1 to the session count
      $number++;  
    }
  }
}
closedir($directory);
session_unset();
// return the value
return ($number=='1') ? "There is " . $number . " user online now." : "There are " . $number . " users online now."; 
?>

thankx i understand about counting the sessions and storing them.
But the problem comes when i want to display them. bcoz i have a link called members when u click it all members ar displayed.
So how i`m i going to show who is online and offline at the same page????

Try this on your logout page --

session_start(); 

mysql_query("DELETE FROM online_users WHERE username = '" . $_SESSION['username'] . "'
");

session_destroy();

I know this may sound obvious, but did you make sure that you executed your query BEFORE calling the session_destroy()?

wow, nevermind...just saw how old this thread was...

Hi all. Congratulation for the site. I'm tring to realize this in classic asp. I ask you all, there is a method to know where the user is really, that is waht is doing? where we can be obtain this information. I realized a simple user online application also in ASP.NET but this is realized in vBulletin board very well; this is what i want realize

*

<?php 
session_start();

if(isset($_POST['submit']))
{
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("chat", $con);
        $message=$_POST['message'];
        $sender=$_POST['sender'];
        mysql_query("INSERT INTO message(message, sender)VALUES('$message', '$sender')");
}

?>

*

i think you should do something like this

  • Make a Column Called "OnlineUsers"
  • When User Loads a Page Adds Their Name To The Database
  • If Their Name Is Already In Database Don't Do Anything
  • Use Events With Mysql So Every 5 Mins it Removes All The Rows In The Column
  • Use Other Code To Interact With It

FlashCreations. How would i create the db table to show the online users, and how would i echo it out showing which users are online in a div tag next to the chat i am using? If you can, can you show me it with all the php needed to make it work?

I was going to make a bunch of sarcastic, smart-alec, obnoxious comments,
it is probably easier to provide some help,
There are dozens of user management scripts that provide this functionality, many are public domain so you can reverse engineer the function into your chat service.
Many chat services provide this function, you just have to turn it on, turned off to minimize server load by default. RTM for your chat, or if it's not supported, Check the possibilities available likewise the code can be reversed into the chat service, sometimes it is just a single line to add. can't be more specific, as the existing code for the chat service is unknown
The code is working on your chat, as other users are getting their names displayed in the chat window when they post

hello
i need of help for my website.need of someone to change API code for a module

commented: Start a new discussion. Some may not find your post buried here. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.