Jump to content

Recommended Posts

ok i have it so when you log in it sets the variable online to 1 ,

and when you log out its supposed to go to 2.

but the problem is it doesn't go to 2 when you log out

here is the log out page

<?
require 'config.php';
session_start();
$onlineid = 2;
$offine = mysql_query("UPDATE `users` SET `online` = '{$onlineid}' WHERE id = '{$_SESSION['id']}'") OR die(mysql_error());
$_SESSION = array();
header("Refresh:2; url=default.html");
echo("You have Successfully Logged Out");
?>

any ideas why its not working

Link to comment
https://forums.phpfreaks.com/topic/51396-solved-simple-user-online-function/
Share on other sites

The problem with this method is some users <b>don't</b> logout at all. Instead of setting that number to 1 and switch it to 2, you need to make a timestamp in the database update everytime they click. Then on the online users page you just do a query that selects the users last active within the past x amount of time and display them. If you need me to go into detail on this I will, or someone else most likely will.

Okay, you need to start off with putting a row in your table where all their user information is kept called something like "last_active" and make it a "timestamp".

 

I am assuming you have a header page that contains your layout for every page and whatever variables and what not that you need. This is where you need to update that timestamp so that it will update every time they click.

 

Here is what your code should look like for your online users page, obviously you will need to change it up to match your needs. This will display all the users online that have been active in the last two minutes, you can change that as well.

 

<?php

$query = mysql_query("SELECT * FROM `users` WHERE `last_active` > (NOW() - INTERVAL 10 MINUTE)")or die(mysql_error());

while ($row = mysql_fetch_assoc($query)){
   echo $row['username'].'<br>';
}

?>

 

Oh, here would be your code on your header page to update their last_active field.

 

mysql_query("UPDATE users SET last_active = NOW() WHERE userID='$id'");

 

Hope this helps =]

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.