Jump to content

Show users online/offline with image


deepermethod

Recommended Posts

I have a personal website that show the number of users on my site as a whole (like = 12 members online).  The site has profile pages so I would like to show if a user is online or offline with an image. 

 

I have searched and searched here and haven't found what I was looking for.  Can I alter my current file to fit what I want?

 

<?php



//Prevent the included file from being called directly.

if(basename($_SERVER['PHP_SELF']) == "online.inc.php") {

   header("Location: /index.php");

   exit;

}



$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect.");

$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");



$timestamp = time();

$timeout = $timestamp - 180;

if(session_is_registered("valid_user")) {

$ol_user = "$valid_user";

} else {

$ol_user = "guest";

}

if(session_is_registered("valid_user")) {

$ol_id = "$member_id";

} else {

$ol_id = "0";

}



//Insert User

$insert = mysql_query("INSERT INTO $tbl_online (timestamp, ip, file, ol_user, ol_id)

VALUES('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."','$ol_user','$ol_id')")

or die("Error in who's online insert query!");

//Delete Users

$delete = mysql_query("DELETE FROM $tbl_online WHERE timestamp<$timeout")

or die("Error in who's online delete query!");

//Fetch Users Online

$result = mysql_query("SELECT DISTINCT ip FROM $tbl_online")

or die("Error in who's online result query!");

$users = mysql_num_rows($result);



if($users == 1) {

$ol_label = "user";

} else {

$ol_label = "users";

}

?>

<table align="center" cellpadding="0" cellspacing="0">

    <tr>

        <td class="online">

            <p><?php echo "<b>$valid_user</b> $ol_label"; ?> online</p>

        </td>

    </tr>

</table>

 

Thanks in advanced.

Link to comment
Share on other sites

We would need to see the profile page to help you there.

 

When you display each name you have to look it up against the database and see if they're online.

 

$result = mysql_query("SELECT ip FROM $tbl_online WHERE user_name = $user_name") or die("Error in user online result query!");

if(mysql_num_rows($result) > 0) {
     //display the online_image
}
else {
     //display the offline_image
}

Link to comment
Share on other sites

I am getting this error= "Error in who's online insert query!"  I am just learning php, so be patient please.

 

Here's the current code:

<?php



//Prevent the included file from being called directly.

if(basename($_SERVER['PHP_SELF']) == "online.inc.php") {

   header("Location: /index.php");

   exit;

}



$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect.");

$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");



$timestamp = time();

$timeout = $timestamp - 180;


//Insert User

$insert = mysql_query("INSERT INTO $tbl_online (id, timestamp, ip, file, ol_user, ol_id)

VALUES(\"$_SESSION[member_id]\",'$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."','$ol_user','$ol_id')")

or die("Error in who's online insert query!");

//Delete Users

$delete = mysql_query("DELETE FROM $tbl_online WHERE timestamp<$timeout")

or die("Error in who's online delete query!");

//Fetch Users Online

$result = mysql_query("SELECT DISTINCT ip FROM $tbl_online WHERE id = $_SESSION[member_id]") or die("Error in user online result query!");

if(mysql_num_rows($result) > 0) {
     echo"<img src='./gr_image/online.jpg'>";
}
else {
     echo"<img src='./gr_image/offline.jpg'>";
}
?>

Link to comment
Share on other sites

your insert query shows error...try mysql_error() to dispaly what kind of error..
...
$insert = mysql_query("INSERT INTO $tbl_online (id, timestamp, ip, file, ol_user, ol_id)

VALUES(\"$_SESSION[member_id]\",'$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."','$ol_user','$ol_id')")

or die(mysql_error());

Link to comment
Share on other sites

Try this, it's annoying trying to deal with escaping the strings, I like to just assign them to another variable.  Also, like zenag said, add mysql_error() on the end of your die function so it can give you a descriptive error message.

 

$mem_id = $_SESSION['member_id'];
$rem_add = $_SERVER['REMOTE_ADDR'];
$php_self = $_SERVER['PHP_SELF'];

$insert = mysql_query("INSERT INTO $tbl_online (id, timestamp, ip, file, ol_user, ol_id)
VALUES('$mem_id', '$timestamp', '$rem_add', '$php_self', '$ol_user', '$ol_id')")
or die("Error in who's online insert query!".mysql_error());

Link to comment
Share on other sites

Thanks maq and zenag.  I got the online image to show, initially. 

 

Now, a new error.  When I go to another page and then come back to where the online/offline image is I get this error:  "Error in who's online insert query!Duplicate entry '4' for key 1".

 

Any ideas?  Is my Delete Users query not working correctly?

 

<?php
//Prevent the included file from being called directly.
if(basename($_SERVER['PHP_SELF']) == "online.inc.php") {
   header("Location: /index.php");
   exit;

}
$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect.");

$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");

$timestamp = time();
$timeout = $timestamp - 180;
$mem_id = $_SESSION['member_id'];
$rem_add = $_SERVER['REMOTE_ADDR'];
$php_self = $_SERVER['PHP_SELF'];

//Insert User
$insert = mysql_query("INSERT INTO $tbl_online (id, timestamp, ip, file, ol_user, ol_id)
VALUES('$mem_id', '$timestamp', '$rem_add', '$php_self', '$ol_user', '$ol_id')")
or die("Error in who's online insert query!".mysql_error());

//Delete Users
$delete = mysql_query("DELETE FROM $tbl_online WHERE timestamp<$timeout")
or die("Error in who's online delete query!");

//Fetch Users Online
$result = mysql_query("SELECT DISTINCT ip FROM $tbl_online WHERE id = $_SESSION[member_id]") or die("Error in user online result query!");
if(mysql_num_rows($result) > 0) {
     echo"<img src='./gr_image/online.jpg'>";
}
else {
     echo"<img src='./gr_image/offline.jpg'>";
}
?>

Link to comment
Share on other sites

You should check to see if that user ID exists in the table already because if they haven't reached the time limit then you're trying to insert into an ID that's already there.

 

So:

 

1) Check to see if user is in the $tbl_online (by id).

2) If they are then UPDATE their time and w/e else you need to update.

3) If they aren't then you should insert them (proceed with your current code).

 

Hope this helps!

Link to comment
Share on other sites

Okay.  I'm stumped.  After doing some more searching, here's my current code (unfinished):

<?php
//Prevent the included file from being called directly.
if(basename($_SERVER['PHP_SELF']) == "online.inc.php") {
   header("Location: /index.php");
   exit;
}

$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect.");
$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");

$timestamp = time();
$timeout = $timestamp - 180;
$mem_id = $_SESSION['member_id'];
$rem_add = $_SERVER['REMOTE_ADDR'];
$php_self = $_SERVER['PHP_SELF'];


$result = mysql_query("SELECT DISTINCT ip FROM $tbl_online WHERE id = $_SESSION[member_id]") or die("Error in user online result query!");
if(mysql_num_rows($result) > 0) {
   $update = mysql_query("UPDATE $tbl_online WHERE timestamp<$timeout")or die("Error in who's online update query!".mysql_error());
} else {
   $insert = mysql_query(("INSERT INTO $tbl_online (id, timestamp, ip, file, ol_user, ol_id) VALUES('$mem_id', '$timestamp', '$rem_add', '$php_self', '$ol_user', '$ol_id')")
        or die("Error in who's online insert query!".mysql_error());
}

//Delete Users
$delete = mysql_query("DELETE FROM $tbl_online WHERE timestamp<$timeout")
or die("Error in who's online delete query!");

 

Now, how do I get it to echo/print the online or offline images?  Here's how I had it before:

//Fetch Users Online

$result = mysql_query("SELECT DISTINCT ip FROM $tbl_online WHERE id = $_SESSION[member_id]") or die("Error in user online result query!");

if(mysql_num_rows($result) > 0) {
     echo"<img src='./gr_image/online.jpg'>";
}
else {
     echo"<img src='./gr_image/offline.jpg'>";
}

 

Thanks in advance.

Link to comment
Share on other sites

 

modify as needed.........

 

 

In this tutorial create 1 file

1. user_online.php

 

Step

1. Create table "user_online" in mysql in database "test".

2. Create file user_online.php.

 

 

database........

CREATE TABLE `user_online` (
`session` char(100) NOT NULL default '',
`time` int(11) NOT NULL default '0'
) TYPE=MyISAM;

 

 

online.php

<?php

session_start();
$session=session_id();
$time=time();
$time_check=$time-600; //SET TIME 10 Minute

$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="user_online"; // Table name

// Connect to server and select databse
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name WHERE session='$session'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count=="0"){
$sql1="INSERT INTO $tbl_name(session, time)VALUES('$session', '$time')";
$result1=mysql_query($sql1);
}
else {
"$sql2=UPDATE $tbl_name SET time='$time' WHERE session = '$session'";
$result2=mysql_query($sql2);
}

$sql3="SELECT * FROM $tbl_name";
$result3=mysql_query($sql3);

$count_user_online=mysql_num_rows($result3);

echo "User online : $count_user_online ";

// if over 10 minute, delete session
$sql4="DELETE FROM $tbl_name WHERE time<$time_check";
$result4=mysql_query($sql4);

mysql_close();

// Open multiple browser page for result
?>

Link to comment
Share on other sites

you need to adapt the code that how your learn everythink on this page does what you want.........

 

have a go it easy the more you pratice the more your learn.........

 

sorry but your a programmer not a copy and past expert i hope.......

 

if your a copy and past exspert then use the freelance on here for paid or free contubuted code sorry......

Link to comment
Share on other sites

Put this where it goes on my provided code.......

 

if the code also needs adapting more tell us what dont work we shall help ok........

 

<?php

if(mysql_num_rows($result3)){

     $img="<img src='online.jpg' />";

}else{

$img="<img src='offline.jpg' />";	

}

echo $img;

// if over 10 minute, delete session
$sql4="DELETE FROM $tbl_name WHERE time<$time_check";
$result4=mysql_query($sql4);

?>

 

 

my code full formatted to show a image offline or online

 

<?php

session_start();
$session=session_id();
$time=time();
$time_check=$time-600; //SET TIME 10 Minute

$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="user_online"; // Table name

// Connect to server and select databse
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name WHERE session='$session'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count=="0"){
$sql1="INSERT INTO $tbl_name(session, time)VALUES('$session', '$time')";
$result1=mysql_query($sql1);
}
else {
"$sql2=UPDATE $tbl_name SET time='$time' WHERE session = '$session'";
$result2=mysql_query($sql2);
}

$sql3="SELECT * FROM $tbl_name";
$result3=mysql_query($sql3);

if(mysql_num_rows($result3)){

     $img="<br><img src='online.jpg' /><br>";

}else{

$img="<br><img src='offline.jpg' /><br>";	

}

echo $img;

// if over 10 minute, delete session
$sql4="DELETE FROM $tbl_name WHERE time<$time_check";
$result4=mysql_query($sql4);

mysql_close();

// Open multiple browser page for result
?>

Link to comment
Share on other sites

Well, I adjusted it to what I need and it initially worked (showed online image).  When I go to another page and back to the page with the online.php script it shows the offline image.

 

My code:

<?php



//Prevent the included file from being called directly.

if(basename($_SERVER['PHP_SELF']) == "online.inc.php") {

   header("Location: /index.php");

   exit;

}



$connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect.");

$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");



$timestamp = time();

$timeout = $timestamp - 180;
$mem_id = $_SESSION['member_id'];
$rem_add = $_SERVER['REMOTE_ADDR'];
$php_self = $_SERVER['PHP_SELF'];


$sql=("SELECT * FROM $tbl_online WHERE id = $_SESSION[member_id]") or die("Error in user online result query!");

$count=mysql_num_rows($result);

if($count=="0"){
$sql1=("INSERT INTO $tbl_online(id, timestamp, ip)VALUES('$mem_id', '$timestamp', '$rem_add')");
$result1=mysql_query($sql1);
}
else {
$sql2=("UPDATE `tbl_online` SET count = count + 1 WHERE id = '$mem_id'");
$result2=mysql_query($sql2);
}

$sql3=("SELECT * FROM $tbl_online");
$result3=mysql_query($sql3);

if(mysql_num_rows($result3)){

     echo"<img src='./gr_image/online.jpg'>";
}
else {
     echo"<img src='./gr_image/offline.jpg'>";
}

echo $img;

// if over 10 minute, delete session
$sql4=("DELETE FROM $tbl_online WHERE timestamp<$timeout");
$result4=mysql_query($sql4);

?>

Link to comment
Share on other sites

I scrapped my script altogether.  I used your script, including creating the new table.  All it shows is the offline image.  I have the  session_start(); at the top of each page and still only the offline image.  When I look in the table in my database there is nothing there, it isn't getting inserted some how.

 

Not looking for a script rewrite, just some advice.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.