473,806 Members | 2,845 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Know the users online ?

150 New Member
i need to know the users on line or off line, that is my code:

[PHP]
$time = time();
$timech = time()-7;
$user = $_SERVER['REMOTE_ADDR'];
$session = $_SESSION['chat'];

$host = "localhost" ;
$username = "root";
$password = "";
$dbname = "chat";

$connect = mysql_connect(" $host", "$username" , "$password" );
mysql_select_db ("$dbname");
$online = mysql_query("SE LECT * FROM online");
while($row = mysql_fetch_arr ay($online)){
if($row['session']==$session){
$uexi = true;
}
}
if($uexi == true){mysql_que ry("UPDATE online SET time='$time',us er='$user' WHERE session = '$ses'");}else{ mysql_query("IN SERT INTO online (session, time, user) VALUES ('$ses', '$time' ,'$user')");}

mysql_query("DE LETE FROM online WHERE time<$timech");
[/PHP]

i used this code in my little chat room but i want to use another technique because i think that code will effect in the site simplicity, in my chat room I'm using Ajax to check every second if the user on line or not, i mean that this code every one will enter the site will check if any one if his session time expired or not this code will take long time to users having slow connection.
May 21 '08 #1
2 1797
Atli
5,058 Recognized Expert Expert
Ok, first of all:
Expand|Select|Wrap|Line Numbers
  1. $online = mysql_query("SELECT * FROM online");
  2. while($row = mysql_fetch_array($online)){
  3.   if($row['session']==$session){
  4.      $uexi = true;
  5.   }
  6. }
  7.  
You are fetching the entire table to check a single row.
This is about the most resource-wasting thing you can do!

Try using the WHERE clause to have MySQL search for the row instead. It will reduce the time and resources needed for this by a LOT
Expand|Select|Wrap|Line Numbers
  1. $online = mysql_query("SELECT * FROM online WHERE session = {$session}");
  2. if(mysql_num_rows($online) > 0) {
  3.   $uexi = true;
  4. }
  5.  
May 21 '08 #2
TheServant
1,168 Recognized Expert Top Contributor
... in my chat room I'm using Ajax to check every second if the user on line or not, i mean that this code every one will enter the site will check if any one if his session time expired or not this code will take long time to users having slow connection.
What about checking less often? Every second connecting to the database could be somewhat resource intensive, inparticular if you have 100 users chatting.100 connections a second pulling whole rows?! Might I also recommend instead of SELECT *, choosing the variables you need (assuming you don't need them all):
[PHP]SELECT user_name, user_time, user_age FROM table1 WHERE online="1"[/PHP]

Just a thought. Oh and what is
$time = time();
$timech = time()-7;
for? If you are doing this for all your pages, why don't you offset your server time? You need to change you htaccess file.
May 21 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2261
by: Keith | last post by:
Is it possible (and if so how), to show a 'users online' counter on my site (VB ASP)? I have seen lots of site that say, xxx members online and xxx guests online. How can I do this?
5
1417
by: Steph | last post by:
Hi, I would like to be able to know who is actually connected on the website i am creating. The users are stored in a sql server table (to enter, they have to be identified on the login.aspx page...) I guess i have to use application variables and global.asax, but i don't know exactly how to do this...
4
12234
by: Kittikun | last post by:
I am currently using the BACKUP DATABASE method to backup my database. Everything works fine, except for users. I created various users with sp_addlogin to access this database and they are located in the xlogins table. The problem is for example, if I backup, remove some users and do a restore, i won't have them back because i just backup the database. So the question is, how can I backup specific users and restore them ? Do I have to...
1
1541
by: Jeff | last post by:
Hey asp.net 2.0 I want to populate a GridView with a list of the online users, but I'm not sure how to get the collection of online users: I tryed using this code: MembershipUserCollection onlineUsers = Membership... but I didn't find a method in the Membership class which provide a collection users!
2
5338
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: 1>make_buildinfo.obj : error LNK2019: unresolved external symbol __imp__RegQueryValueExA@24 referenced in function _make_buildinfo2 Ask on python-list@python.org . - Josiah
4
2813
by: ch.adilaziz | last post by:
HI all I just wana know that is there is any TSQL Statement which will give me the list of all user groups,names of user in each group and thier Permssions in any database/server. Actually I have to create a report in wich i have to show that how many groups we have any whi is the member of wich group with such permissions.I ckeck through Interprise Manager but i guess it will take alot of time to note every user group then users and...
19
2876
by: Wessidemike | last post by:
Guys I'm new to web development and especially new to databases. I have no programing background but I am computer literate in other aspects. I'm trying to start a small online business dealing with online book sales. I'm looking possibly for a premade php database that will do these things. 1. users need to be able to post books and information to a website. 2 other users need to be able to see the posted books and get in contact with...
2
1573
by: Justin | last post by:
I have 2 sql server 2000 database that participate in log shipping. Server A is the source database, which log ships to a database on Server B. The database on Server B is in a warm stand by/read only mode. Is there a way that I can add users to the database on Server B, without having to add the users to the database in server A? I am trying to use the database on Server B as a reporting only database and add more users to have...
1
2076
by: someoneelseisnew | last post by:
i am trying to put together the frame work for a social networking site. no small feat :) i've been reading up n database structure, but there's one thing i wasn't able to find. how do sites like facebook show online users? is it simply a field in the user table that shows if they are online? how is it not slowing down the db to check a user table with over 100000 uses to see who is online? or is there one table for online users that...
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10371
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10110
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3850
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.