runnerjp Posted June 1, 2008 Share Posted June 1, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/ Share on other sites More sharing options...
paulman888888 Posted June 1, 2008 Share Posted June 1, 2008 simple. Add another colum called ONOFF. when user logins on set ONOFF to 1 when user logins off set ONOFF to 0. Now get your php script to show all users with ONOFF equaling 1. Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-554733 Share on other sites More sharing options...
runnerjp Posted June 1, 2008 Author Share Posted June 1, 2008 sorry i dont think you understood what i ment... i know how to do that... but my problem is recording a user who is not online and has just closed the browser down and has not logged off they will still be as logged on but not if that makes sence Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-554766 Share on other sites More sharing options...
DeanWhitehouse Posted June 1, 2008 Share Posted June 1, 2008 Check for there session, if they close the browser the session wont be present, and set users offline after a certain amount of inactivity Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-554768 Share on other sites More sharing options...
runnerjp Posted June 1, 2008 Author Share Posted June 1, 2008 ahhh thats a good idea!... ok i have never come accross this before but how would i find the users indervidual session lol this would be the logged in session $_SESSION['logged_in'] Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-554772 Share on other sites More sharing options...
joquius Posted June 1, 2008 Share Posted June 1, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-554773 Share on other sites More sharing options...
runnerjp Posted June 1, 2008 Author Share Posted June 1, 2008 ahhh thats a very good idea... so update there activity on every thing the invloves adding to the database Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-554778 Share on other sites More sharing options...
DeanWhitehouse Posted June 1, 2008 Share Posted June 1, 2008 well on everypage,so you could just put it in the header.php (or something similar) and then everypage they go to it will update the db with when they went there Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-554782 Share on other sites More sharing options...
runnerjp Posted June 1, 2008 Author Share Posted June 1, 2008 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';} Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-554785 Share on other sites More sharing options...
runnerjp Posted June 1, 2008 Author Share Posted June 1, 2008 bmp Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-554988 Share on other sites More sharing options...
runnerjp Posted June 1, 2008 Author Share Posted June 1, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-555003 Share on other sites More sharing options...
Wolphie Posted June 1, 2008 Share Posted June 1, 2008 Your query is wrong. The UPDATE statement should be as follows UPDATE `db_name` . `tbl_name` SET `col_name` = 'my_value' WHERE `user_id` = '3' Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-555007 Share on other sites More sharing options...
runnerjp Posted June 1, 2008 Author Share Posted June 1, 2008 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?? Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-555008 Share on other sites More sharing options...
Wolphie Posted June 1, 2008 Share Posted June 1, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-555013 Share on other sites More sharing options...
runnerjp Posted June 1, 2008 Author Share Posted June 1, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-555019 Share on other sites More sharing options...
runnerjp Posted June 1, 2008 Author Share Posted June 1, 2008 ok i looked at ur script and it confused me a little lol... so how would i get members/index.php?page=mainforums as its .php with extra Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-555053 Share on other sites More sharing options...
runnerjp Posted June 1, 2008 Author Share Posted June 1, 2008 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)); }?> Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-555059 Share on other sites More sharing options...
runnerjp Posted June 1, 2008 Author Share Posted June 1, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-555064 Share on other sites More sharing options...
runnerjp Posted June 1, 2008 Author Share Posted June 1, 2008 bmp Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-555135 Share on other sites More sharing options...
.josh Posted June 1, 2008 Share Posted June 1, 2008 just make a condition. query to see if it's there. If it is, then update. If not, then insert. Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-555136 Share on other sites More sharing options...
runnerjp Posted June 1, 2008 Author Share Posted June 1, 2008 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()); }};?> Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-555165 Share on other sites More sharing options...
dsaba Posted June 1, 2008 Share Posted June 1, 2008 Here's a non-database solution: http://phpsnips.com/tempSnip.php?id=9 Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-555167 Share on other sites More sharing options...
runnerjp Posted June 1, 2008 Author Share Posted June 1, 2008 i wud rather do it by db tho Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-555183 Share on other sites More sharing options...
.josh Posted June 1, 2008 Share Posted June 1, 2008 pseudo code: // change table, column and variables to yours $rs = mysql_query("select userid from table where userid = '$userid'"); if ($rs) { // update } else { // insert } Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-555204 Share on other sites More sharing options...
runnerjp Posted June 2, 2008 Author Share Posted June 2, 2008 hey the code didnt work i just got a blank page... would it need to be $rs == 1 Quote Link to comment https://forums.phpfreaks.com/topic/108224-seeing-if-users-are-online/#findComment-555383 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.