Jump to content

Recommended Posts

hey guys im lookig for some ideas to tell if a user is online or not...

 

i have my db set up with users so means i have login and logout ect

 

i thought about making a field online and when the log in it sets it to 1 and when the click logout it sets it back to 0... but then i thought how often do i click logout lol and its not many times... so i can i find out if a user has just left without logging out??

 

it must be able to be done as its done here!

Link to comment
https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/
Share on other sites

Create a table, for user activity with the `user_id` and `activity` fields, each time a user does something on the site update the value of `activity` to time().

 

Then just do

 

"SELECT COUNT(`user_id`) FROM `user_activity` WHERE `activity` + 1800 > ".time();

 

You can set 1800 (seconds since active) to whatever you want.

so if i just put onto of everypage

 

$id = $_SESSION['user_id'];

$time = time();

$update = "UPDATE users SET activity='$time' WHERE id='$id' ";

 

that would do it then where i want to pull the users activity do this

 

if{

"SELECT COUNT(`id`) FROM `users` WHERE `activity` + 1800 > ".time(); echo 'online'} else { echo 'offline';}

 

ok i have gone for this

 

$timestamp = time();
$timeout = $timestamp - 180;
$username= get_username($_SESSION['user_id']);

//Insert User
$insert = mysql_query("UPDATE useronline (timestamp, ip, file, user, user_id)
VALUES('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."','$username','".$_SESSION['user_id']."')")  
or die("Error in who's online insert query!");
echo $insert;

 

but i get my Error in who's online insert query and it doesnt echo insert

ok i have got it working ish... there are a few things lol

 

ok here is my script

<?php
$timestamp = time();
$timeout = $timestamp - 180;
$username= get_username($_SESSION['user_id']);

//Insert User
$insert = mysql_query("UPDATE `useronline` SET `timestamp`='$timestamp', `ip`='".$_SERVER['REMOTE_ADDR']."', `file`='".$_SERVER['PHP_SELF']."',`user`='$username',`user_id`='".$_SESSION['user_id']."'") 
or die(mysql_error()); 
?>

 

ok so first what do i use other then update to insert the data if its not allready there ?? then update it if it is ??

 

 

my next question is '".$_SERVER['PHP_SELF']."'  i have a problem in the fact i use

<?php $page = $_GET['page'];
                    if (ereg('[A-Za-z0-9]',$page) ) {
                        if (file_exists('include/'.$page.'.php')) {
                            include('include/'.$page.'.php');
                        } else {
                            include('include/main.php');
                        }
                   } else {
                            include('include/main.php');
                    }?>

to get the pages so because its on my index.php all i get is index.php so what can i do??

 

 

 

To insert data that doesn't already exist use the INSERT statement

 

INSERT INTO `db_name` . `tbl_name` ( `col_1`, `col_2` ) VALUES ( 'val_1', 'val_2' )

 

Update is used to update data that already exists.

 

 

If what I think you're trying to do is right, which is getting the filename from the URL use

 

<?php
$filename = $_SERVER['SCRIPT_NAME']; // filename.php
$filename = basename($_SERVER['SCRIPT_NAME'], '.php'); // This removes .php from the file. Same applies for any file type.
?>

 

Oh and a word of advice, do NOT use any GET, POST or REQUEST methods to retrieve the filename, otherwise it's extremely easy to spoof.

 

<?php
error_reporting(E_ALL);
$permitted = array('index', 'users', 'articles'); // Array of permitted file names
$name = basename($_SERVER['SCRIPT_NAME'], '.php'); // Name of file excluding .php
$prefix = 'cache/'; // Directory
$suffix = '.html'; // File format to be saved in

$filename = (in_array($name, $permitted)) ? $prefix . $name . $suffix : die('Caching failed!'); // Determine whether the file name is permitted

if(!file_exists('cache')) mkdir('cache'); // Create the directory if it doesn't exist

if(file_exists($filename) && ((time() - filemtime($filename)) < (5 * 60))) { // If the file exists, and is within a 5 minute timeout
  include $filename;
  print '<!-- THIS FILE HAS BEEN CACHED! -->';
  exit; // Include the cached file and exit to prevent further execution
}
else ob_start(); // Start output buffer
?>
<html>
  <head>
    <title>Cache Test</title>
  </head>
  <body>
    <p>My HTML goes here!</p>
  </body>
</html>
<?php
if($fp = fopen($filename, 'w')) {
  fwrite($fp, ob_get_contents()); // Write output buffer's contents to the file
  fclose($fp);
  ob_end_flush();
}
?>

 

That's my caching code, which caches pages. It kind of follows the same concept, so take a look.

hey cheers but that didnt work

 

members/index.php?page=mainforums

is an example of 1 of my urls... and it just brought back index in my db

 

 

any other ways of doing this??

 

 

also if im just using the insert statment... how could i go about updating records that are also all ready there?

ok ok i have found away

<?php
function selfURL() { 
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s; 
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); 
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']; } 
function strleft($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); }?>

ok going to keep people uptodate where i am with this

 

so i now have

<?php $id = $_SESSION['user_id'];  
$puser = get_username($id);
$timestamp = time();
$timeout = $timestamp - 180;
$username= get_username($_SESSION['user_id']);
function selfURL() { 
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s; 
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); 
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']; } 
function strleft($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); }
$filename = (selfURL());
//Insert User
$insert = mysql_query("insert `useronline` SET `timestamp`='$timestamp', `ip`='".$_SERVER['REMOTE_ADDR']."', `file`='$filename',`user`='$username',`user_id`='".$_SESSION['user_id']."'") 
or die(mysql_error()); 

 

ok onto the next problems lol

 

1ST = how can i overwrite existing results in my db.. i know i could use update but what if the result is not there in the 1st place?

2ND= how can i find out if an indervidual user is online?

 

 

 

 

what would i put in the if area tho??

 

 <?php mysql_query("GET `user_id`='".$_SESSION['user_id']."'") or die(mysql_error()); 
  
   if ??? {

  
mysql_query("insert `useronline` SET `timestamp`='$timestamp', `ip`='".$_SERVER['REMOTE_ADDR']."', `file`='$filename',`user`='$username',`user_id`='".$_SESSION['user_id']."'") 
or die(mysql_error()); }

else

{ mysql_query("update `useronline` SET `timestamp`='$timestamp', `ip`='".$_SERVER['REMOTE_ADDR']."', `file`='$filename',`user`='$username',`user_id`='".$_SESSION['user_id']."' WHERE `user_id`='".$_SESSION['user_id']."'") 
or die(mysql_error()); }};?>

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.