PHP - How Many Users Online Script Help
I looked for a script that shows the number of users online on the site i am making, and found this. I don't know if it is for just showing if the user is online on that one page like a profile page, or if it is for all users in the database. Also, when using the code, it shows "Users Online: 1" then when I reload the page, it says "2' and again, then "3" even though no users have logged in. Thank you.
the script Code: [Select] <?php ///////////////////////////////////////////////////////////////////////////////////////// ////IS USER ONLINE SCRIPT PART//// $session=session_id(); $time=time(); $time_check=$time-600; //SET TIME 10 Minute //CONNECT TO DB //connect info here // Connect to server and select databse mysql_connect("$host1", "$username1", "$password1")or die("cannot connect to server"); mysql_select_db("$db_name1")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name1 WHERE session='$session'"; $result=mysql_query($sql)or die(mysql_error()); $count=mysql_num_rows($result); if($count=="0"){ $sql1="INSERT INTO $tbl_name1(session, time)VALUES('$session', '$time')"; $result1=mysql_query($sql1)or die(mysql_error()); } else { "$sql2=UPDATE $tbl_name1 SET time='$time' WHERE session = '$session'"; $result2=mysql_query($sql2)or die(mysql_error()); } $sql3="SELECT * FROM $tbl_name1"; $result3=mysql_query($sql3)or die(mysql_error()); $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 ///////////////////////////////////////////////////////////////////////////////////////////// ?> Similar TutorialsHello. I have recently been building a user system and trying to code an "Online Users" script for it however it is not working out for me, so I thought I'd come and ask here. I need a script that adds them to the table 'online' when they login and then remove them when they logout. That part is simple to do however I am using sessions and I am trying to figure out a way to check if they are inactive like every 10 minutes, and if they are delete their row from the db. It'd be appreciated if someone could set up a code for this. I've seen countless forum software/cms which have such feature, I understand how they count the total users online (as theirs a row which says if a user is online or not (boolean), and then its all counted using mysql_num_rows() to get the total) However I don't get how they'd display the number of Guests? (as I don't think they'd log them woul'dnt that be messy/tedious?) What would the best way be to show when a user is online? Detecting when they close the browser/tab, not just log out? I am trying to display current online registered users and online guests. A user is defined as online by the online column in the database. if set to 1 they are online, if set to 0 they are offline. so when a user logs in i set this column to 1 to show they are online. but when a user is inactive i set it to 0. i do this using: Code: [Select] if($user->last_active_time < time()-1800) { $link->query("UPDATE ".TBL_PREFIX."users SET u_online = 0 WHERE u_username = '".$user->user_name."'") or die(print_link_error()); } else { $link->query("UPDATE ".TBL_PREFIX."users SET u_online = 1 WHERE u_username = '".$user->user_name."'") or die(print_link_error()); } $user->last_active_time is set on every page load to the current time stamp(UNIX) however they are sometimes displayed as online or offline when they arent. Also for displaying guests i use: Code: [Select] $session_time = time()-300; $query = $link->query("SELECT g_ip FROM ".TBL_PREFIX."guests WHERE g_ip = '".$_SERVER['REMOTE_ADDR']."'") or die(print_link_error()); $num_rows = $query->rowCount(); if($num_rows == 0) { if(!$user->user_name) { $link->query("INSERT INTO ".TBL_PREFIX."guests (g_ip, g_time) VALUES ('".$_SERVER['REMOTE_ADDR']."', '".$config['time_now']."')") or die(print_link_error()); } } if the IP address of the current visitor is not in the database and they are not logged in then it adds their ip to the database. but sometimes more guests are displayed then there actually are(tested this on local server so i know how many are online). Are there any better ways i could be doing this? Hi, I'm trying to code a page which displays the amount of users currently viewing a page. But how do I decrease the count if a user closes the page? Thanks The following code is what I have already done, but I have just realised that the way I have done this will not enable me to display the online users in alphabetical order, I do not know a way how to do this. Any help or suggestions? Thanks $friend_query = mysql_query("SELECT * FROM friend_request WHERE user='{$user_id}'"); $friend_id_array = ""; while($row = mysql_fetch_assoc($friend_query)) { $friend_id = $row['friend_id']; $more_query = mysql_query("SELECT * FROM friend_request WHERE friend_id='{$user_id}'"); while($row_more = mysql_fetch_assoc($more_query)) { $more_friend_id = $row_more['user']; //all friends in an array $friend_id_array = $friend_id_array.$friend_id."/".$more_friend_id; $friend_id_array = explode('/', $friend_id_array); $friend_count = count($friend_id_array); //how many of the friends are online $online_count = 0; for($i=0;$i<$friend_count;$i++) { $query_online = mysql_query("SELECT loggedin, fname, mname, lname FROM users WHERE id='{$friend_id_array[$i]}'"); //get loggedin and names $row = mysql_fetch_assoc($query_online); $loggedin = $row['loggedin']; if($loggedin == "1") //if logged in { $online_count++; // final number off people online } } } } I have a website where users can add their friends. What I am trying to achieve is to show every user that who are their friends that are currently online. If any one knows a link to any script or tutorial that explains this, Please help. Thanks, Faisal Hey everyone, I'm currently working on a friends online script and i have a slight problem that i need help with. Basically the code first searches "TBL_Friends" to see if you have any friends added. If it returns results it then turns your friends ID's into a variable. It then searches "TBL_Users_Online" to see if any body is logged based on the friend's ID it returned before. The first bit of the code works and it retrieves all the friends i got added. The second half is odd, if i have one or two friends added it will show that one is online. If i have more then three friends added it returns no results. I know my code is a bit sloppy and probably not the best way of writing it, im still learning PHP. Anyways this is the code, any help is appreciated. Code: [Select] <?php $FriendsOnline = mysql_query("SELECT Sender_ID FROM TBL_User_Friends WHERE Reciever_ID = $UserID"); while($fo=mysql_fetch_array($FriendsOnline)) { $FriendsOnlineID = $fo[Sender_ID]; $FriendsOnlineNumber = mysql_query("SELECT * FROM TBL_Users_Online WHERE User_ID = $FriendsOnlineID"); $FriendsNumber = mysql_num_rows($FriendsOnlineNumber); echo $FriendsNumber; } ?> $SenderID = Friends ID $Reciever_ID = User ID $UserID = User ID Hi, I just want to see what way you guys think is best. On this little community I'm building I have decided to implement a function to see who were active within the last 15 minutes. I made a table (just user and timestamp) to register the last activity of any logged on user. Then I have a variable to take off 15 minutes from that but I can't get them to compare. Googling the issue I found people are solving this in very different ways. I wanted to see what phpfreaks recommend as the next step. Here is some code (that doesn't work properly - no results found as I compare to different timestamps): Code: [Select] include_once'header.php'; $now=time(); $now=(date("Y-m-d H:i:s")); //$mins = mktime(0,$now-15,0, date("Y"), date("m"),date("d")); $mins = time(); $mins15 = $mins-(60*15); $mins15 = (date("Y-m-d H:i:s", $mins15)); $online="SELECT * FROM user_online" WHERE last_activity > mins15; $result = mysql_query($online); if (!$result) die ("Database access failed: " . mysql_error()); $rows = mysql_num_rows($result); echo <<< _END <div id='statusbar'> <h4>Online now: $rows</h4> I've been looking for a simple script that would connect to a game server to see if it is still online. I've found many, but not one of them work. They will permanently send back the text "Online", or "Offline". This is the simplest code I have found: <?php $ip = "66.79.190.40"; $port = "27960"; if (! $sock = @fsockopen($ip, $port, $num, $error, 5)) echo '<B><FONT COLOR=red>Offline</b></FONT>'; else{ echo '<B><FONT COLOR=lime>Online</b></FONT>'; fclose($sock); } ?> I'm also looking into getting it to pull the map name and player-list, but I should be able to figure that out on my own once I get this working. If it helps, this script is supposed to connect to the original Quake games(One, two, and three). I almost posted this in the freelancing forum, because I am looking for it to be created, but I first needed more info on what a coder would need to know about the project. I have a script that is designed for a single person, and I want a coder to design it to become an online service with user registration, etc. I'm weary of posting publicly the details about what the script does, but is something like that needed in order for a php programmer to take on a job? Could something like that be left out, until actually giving the coder the job? Do I have to give at least a vague idea of what the script does? I don't understand enough about what a programmer would need to know, but at the same time, I don't want to reveal my idea either. Can somebody help me on how much I am going to need to reveal? Thanks. Hi All, I've searched long and hard accross the web for an answer to this and finnally given in and requesting help. Here's what i have, i have a database setup and working fine. What i would like to do is for an administrator to be able to update my users details. It may sound odd, why don't you let your users update their own details? Well the administrators are dispatchers if you like, and my users are the 'dispatchees', for want of a better word. So i would like my administrators to be able to dispatch my users with routes and my users be able to see the routes that have been dispatched to them. I've setup a login area and a page that pulls there routes off the database, depending on their login details, i.e. jack will see his routes and jill will see her's independantly. This works by me editing the appropriate columns/rows of my database using phpmyadmin. What i'd like now is for administrators (who are directed to a seperate page, with more controls) to be able to do the same as me (updating the database) but by using a php form/script. I'd like to be able to select the routes from a second table on the same database if possible, to try and keep everything tidy. So my dispatcher would select Route001 from a drop down list, this would fill in the text fields next to the route field with From To, so my dispatcher would know what route001 actually is from/ too, choose a username (now being driven from my other table) and hit dispatch. My user would login to their area, hit view dispatched routes and it would display Route 001 with the correct information. The login area was a downloaded script i modified to suit and is called Login-Redirect_v1.31_FULL Many thanks in advance, hope you can sort of understand what i want Josh PHP/MySQL ability:Novice Hello
I am trying to work out how many regular users I have to my site and how long those users tend to be users..
So, I have a table that logs every time a user visits my site and logs in, it stores the date / time as a unix timestamp and it logs their user id.
I started by getting the id's of any user who logs in more than 5 times in a specified period, but now I want to extend that...
SELECT userID as user, count(userID) as logins FROM login_history where timestamp > UNIX_TIMESTAMP('2014-06-01 00:00:00') and timestamp < UNIX_TIMESTAMP('2014-07-01 00:00:00') group by user having logins > 5; I just discovered that I have a major security flaw with my website. Anyone who logs in to the website can easily access other users information as well as delete and edit other users information just by changing the ID variable in the address bar. I have user ID Session started on these pages but still people can do anything they like with other users information just by editing the address bar. For example if your logged in in the address bar of www.mywebsite.com/delete_mystuff.php?id=5 and change the "5" say to a "9" then you will have access to user#9 information. Every important page that I have has this code: Code: [Select] session_start(); if (!isset($_SESSION['user_id'])) { // Start defining the URL. $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); // Check for a trailing slash. if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) { $url = substr ($url, 0, -1); // Chop off the slash. } // Add the page. $url .= '/index.php'; ob_end_clean(); // Delete the buffer. header("Location: $url"); exit(); // Quit the script. } else { //Else If Logged In Run The Script if((isset($_GET['id'])) && (is_numeric($_GET['id']))) { $id = (int) $_GET['id']; } elseif ((isset($_POST['id'])) && (is_numeric($_POST['id']))) { $id = (int) $_POST['id']; } else { echo ' No valid ID found, passed in url or form element'; exit(); } What am I doing wrong? Please help if you know how to correct this. Many thanks in advance. im creating a members website and i want to show how many people are logging, in my database i have a col named online every time somone logs in there online goes from 0 to 1 and when thay log out it goes back to 0. i need to know how to show the total people that have 1 in there online part of the data base iv tryed this code and its not working <?php $result = mysql_query("SELECT online FROM `members` WHERE online='1'"); $row = mysql_fetch_row($result); echo $row; ?> this works in the sql console in phpmyadmin Hi there, i am working on a mobile site can u pls help me with a script which shows guests online on my site with their country flag next to its phone model i want online quiz in php mysql ok well i have this is online script that starts at the login page where it sets a session. well it echos that that person is online even if they are not, i will have all of the code only for the online script so it will be in peaces. ok so here is the login page where the session is started login.php Code: [Select] <?php //ok so if they submit the page and its all right and they login , here is the session that is set for the person, again its just the piece of the script . $_SESSION['logedin'] = $_POST['email']; mysql_query("UPDATE myMembers SET last_activity=now() WHERE id='$id'"); ?> now here is where i call on the session and see if there online , the profile.php is set up to where it sees if it is your profile or not so $logoptions_id is the id that they are logged in as. profile.php Code: [Select] <?php // this is on top of the page , where the session is called and if they are logged in it updates the database where there id is. if( isset($_SESSION['logedin']) ) { mysql_query("UPDATE myMembers SET last_activity=now() WHERE id='$logOptions_id'");// there is where $logOptions_id comes in. } // now this is further down the page(script) where we see if they are logged in or not. $age= 60; //set a variable called age, assign an integer of 60 to it. if( isset($_SESSION['logedin']) ) { $q = 'SELECT id=`$id`, DATE_FORMAT(`last_activity`,"%a, %b %e %T") as `last_activity`,UNIX_TIMESTAMP(`last_activity`) as `last_activity_stamp`FROM `mymembers`WHERE `$id` <> \''.($_SESSION['logedin']).'\''; $isonlinecheck = mysql_query($q); $row = mysql_fetch_assoc($isonlinecheck); if (($row['last_activity_stamp'] + $age)< time()){ $isonline = "is <font color='green'>online!</font>";} else { $isonline = "is<font color='red'> offline!</font>"; } } ?> i wana thank all who helps! your all greatly appreciated And we're back online... again! It may have taken 3-4 days, but we're back.
|