473,698 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Who's online?" always = 0

117 New Member
Ok i found this a while back and i am trying to mod it so it works but it keeps not updating the users online and kills my other code?

The first step in creating your "who's online" counter is to setup a table in your database. Here's the table that I used:

Code:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE trv_online (
  2.   session_id varchar(255) NOT NULL default '',
  3.   activity datetime NOT NULL default '0000-00-00 00:00:00',
  4.   member enum('y','n') default 'n',
  5.   ip_address varchar(255) NOT NULL default '',
  6.   refurl varchar(255) NOT NULL default '',
  7.   user_agent varchar(255) default NULL,
  8.   PRIMARY KEY  (session_id),
  9.   KEY session_id (session_id)
  10. ) TYPE=MyISAM;

The first script we'll dig into is going to be included as a file at the top of every php script on your website. Hopefully you have a global header file that you can simply do this only one time. Here's the script:

Code:
Expand|Select|Wrap|Line Numbers
  1.    if(!session_is_registered('online')){
  2.     @mysql_query("INSERT INTO trv_online (session_id, activity, ip_address, refurl, user_agent)   
  3.             VALUES
  4.             ('".session_id()."', now(), '{$_SERVER['REMOTE_ADDR']}', '{$_SERVER['HTTP_REFERER']}', '{$_SERVER['HTTP_USER_AGENT']}')");
  5.     session_register('online');
  6. } else {
  7.     if(session_is_registered('user_id')){
  8.         @mysql_query("UPDATE trv_online SET activity=now(), member='y' WHERE session_id='".session_id()."'");
  9.     }
  10. }
Place in body:
Code:
Expand|Select|Wrap|Line Numbers
  1. <?
  2. // This file is included into your website
  3. // Preferably a MySQL connection has been established already
  4.  
  5. $limit_time = time() - 300; // 5 Minute time out. 60 * 5 = 300
  6. $sql = mysql_query("SELECT * FROM trv_online WHERE UNIX_TIMESTAMP(activity) >= $limit_time AND member='n' GROUP BY ip_address") or die (mysql_error());
  7. $sql_member = mysql_query("SELECT * FROM trv_online WHERE UNIX_TIMESTAMP(activity) >= $limit_time AND member='y' GROUP BY ip_address") or die (mysql_error());
  8. $visits = mysql_num_rows($sql);
  9. $members = mysql_num_rows($sql_member);
  10.  
  11. echo "People Online:<br />";
  12. echo "Guests Online: $visits<br />";
  13. echo "Members Online: $members<br />";
  14. ?>
Well i got it working but it dont update when i login?
It says 14 guest now, and i am logged in so it sould ream me as member.....

And how do i make a cron job, the tutorial says it lowrs site badwith and it runs better updating records?

Expand|Select|Wrap|Line Numbers
  1. if(session_is_registered('online')){       
  2.     @mysql_query("UPDATE trv_online SET activity=now() WHERE session_id='".session_id()."'");
  3. }
  4. // Database connection information here
  5.  
  6. $maxtime = time() -100;
  7. $sql = mysql_query("DELETE FROM trv_online WHERE UNIX_TIMESTAMP(activity) < '$maxtime'");
  8. $rows = mysql_affected_rows();
Aug 23 '07 #1
10 2299
Breana
117 New Member
Any got an idea on this?
Aug 24 '07 #2
Breana
117 New Member
Bump, i need help :)
Aug 25 '07 #3
etiainen
40 New Member
does it register you as a member after a few clicks?
What I mean is, does it only break when you login or everytime it executes?
Aug 25 '07 #4
Breana
117 New Member
Wel it updates the guest because it keeps going up and down when i login on the laptop and my pc but if i login it wont show...
always Members online: 0
Aug 26 '07 #5
pbmods
5,821 Recognized Expert Expert
Heya, Breana.

Changed thread title to better describe the problem.

You're getting a '0' response because your query generates no results. This probably means that the `member` field never gets set to 'y'.
Aug 26 '07 #6
etiainen
40 New Member
Hm, this could be it:

[PHP]
if(!session_is_ registered('onl ine')){
@mysql_query("I NSERT INTO trv_online (session_id, activity, ip_address, refurl, user_agent)
VALUES
('".session_id( )."', now(), '{$_SERVER['REMOTE_ADDR']}', '{$_SERVER['HTTP_REFERER']}', '{$_SERVER['HTTP_USER_AGEN T']}')");
session_registe r('online');
} else {
if(session_is_r egistered('user _id')){
@mysql_query("U PDATE trv_online SET activity=now(), member='y' WHERE session_id='".s ession_id()."'" );
}
}
[/PHP]

In this snippet of yours you have this structure:
if session not registered
.......register it
else
.......if user_id registered in session
..............m ark it as an online member (set that thing='y')


Take a look at this... when you login first time you have no session and it registers it, but in that same pass it does not mark you as an online member 'cause it's in the else branch...


Another problem might be that your session auto start isn't set to true, so you must call session_start() in your script before referencing $_SESSION...
Aug 26 '07 #7
Breana
117 New Member
Ok, so i changed it to this: and placed it in the very top where i have it refrence session start...

[PHP] session_start() ;
include("./config.php");
include("./dbx.php");
include("./common.php");

if(session_is_r egistered('user _id')){
@mysql_query("U PDATE trv_online SET activity=now(), member='y' WHERE session_id='".s ession_id()."'" );
} else {
if(!session_is_ registered('onl ine')){
@mysql_query("I NSERT INTO trv_online (session_id, activity, ip_address, refurl, user_agent)
VALUES
('".session_id( )."', now(), '{$_SERVER['REMOTE_ADDR']}', '{$_SERVER['HTTP_REFERER']}', '{$_SERVER['HTTP_USER_AGEN T']}')");
session_registe r('online');
}
}
if(session_is_r egistered('onli ne')){
@mysql_query("U PDATE trv_online SET activity=now() WHERE session_id='".s ession_id()."'" );
}[/PHP]

I just need a simply way for it to update if a member logs in.
Aug 26 '07 #8
etiainen
40 New Member
Ok, this structure looks fine to me...
Is it working?
Aug 26 '07 #9
Breana
117 New Member
Hey yeah i got it working, i changed 2 lines and put the code in a new php and it does work thanks for the help.

I had some code in the head killing it lol i dident know exit; ment it would not load past that i thought it ment exit thet function then load a new code :P

I know now lol.
Aug 27 '07 #10

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

Similar topics

27
5077
by: Curious Angel | last post by:
I have a resume in PDF format and I want anyone who LEFT-OR-RIGHT clicks the link to force the file to be saved, and in any event _not_ opened. Since the PDF will be in his cache in any event, I would just as soon place the employer in control of what directory he wishes to save it in, and there are two salient reasons for this: 1. I want him to OWN the document 2. I want him to FIND the document, quickly, on his hard drive In any...
87
5212
by: ziliath | last post by:
I recently tried out the Google "top coder" contest, as a C++ coder. I noticed immediately that they expected me to know STL. To which I say, what the fuck?! I may be missing something, but at what point when learning C++ was I supposed to have picked up STL? I ask because at every job I've had, and every job interview for that matter, whenever STL comes up it's not I, but a manager who says effectively yuck, we don't use that crap.
43
2825
by: Zeng | last post by:
It's so messy w/o the "friend" relationship. Does anyone know why it was not supported in C#. It's almost about as bad as it doesn't support the inheritance hierarchy and method reference (calling tree) browsing that is supported in C++. I don't know how some could write a large scale object-oriented application w/o those. If you have overcome these limitations in C#, please share your thoughts and ideas. Thanks!
4
1449
by: tperri | last post by:
I've got a user table with a field called Online, and when a user logs in, I set a value in this column to indicate they are online. And in the same fashion, when they click the logout button, I update this same field to indicate they are offline. However, how do I handle this when a user just X's out of the browser without logging off? I've set break points in the global.asax.cs file but the Session_End and Application_End functions...
10
3091
by: Parasyke | last post by:
I have a form that I choose from a list of database names and a list within a textbox comes up with the computer ID number and an associated (from a table) user name. Is there a way from looking at this code to tell the network ID name? I hope not to have to write the code with the "Dev Ashish" API code, which I'm sure is great but I can't figure out how to implement it in a form like mine. Please look at the below code and tell if I can...
1
6493
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
26
2781
by: anonieko | last post by:
In the past I always used "" everywhere for empty string in my code without a problem. Now, do you think I should use String.Empty instead of "" (at all times) ? Let me know your thoughts.
2
928
by: =?Utf-8?B?bGpsaW93YQ==?= | last post by:
When I try to open my Money 2007, the Online Policy Statement pops up. I tell it to "Accept" and then the program shuts down and I cannot get in. It won't even be 2 years for me until Dec. Anyone have a solution as to how I can open the program or do I need to re-install?
1
1634
by: samadams_2006 | last post by:
Hello, I haven't done much programming in .asp and was wondering if someone could provide me with some sample code to do the following task... I have a Web Service called: http://abc.def.com:8088/wsABC.asmx It has a Web Method called:
0
8673
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
8601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9021
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
8892
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
7716
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...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2327
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.