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: -
CREATE TABLE trv_online (
-
session_id varchar(255) NOT NULL default '',
-
activity datetime NOT NULL default '0000-00-00 00:00:00',
-
member enum('y','n') default 'n',
-
ip_address varchar(255) NOT NULL default '',
-
refurl varchar(255) NOT NULL default '',
-
user_agent varchar(255) default NULL,
-
PRIMARY KEY (session_id),
-
KEY session_id (session_id)
-
) 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: -
if(!session_is_registered('online')){
-
@mysql_query("INSERT INTO trv_online (session_id, activity, ip_address, refurl, user_agent)
-
VALUES
-
('".session_id()."', now(), '{$_SERVER['REMOTE_ADDR']}', '{$_SERVER['HTTP_REFERER']}', '{$_SERVER['HTTP_USER_AGENT']}')");
-
session_register('online');
-
} else {
-
if(session_is_registered('user_id')){
-
@mysql_query("UPDATE trv_online SET activity=now(), member='y' WHERE session_id='".session_id()."'");
-
}
-
}
Place in body:
Code: -
<?
-
// This file is included into your website
-
// Preferably a MySQL connection has been established already
-
-
$limit_time = time() - 300; // 5 Minute time out. 60 * 5 = 300
-
$sql = mysql_query("SELECT * FROM trv_online WHERE UNIX_TIMESTAMP(activity) >= $limit_time AND member='n' GROUP BY ip_address") or die (mysql_error());
-
$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());
-
$visits = mysql_num_rows($sql);
-
$members = mysql_num_rows($sql_member);
-
-
echo "People Online:<br />";
-
echo "Guests Online: $visits<br />";
-
echo "Members Online: $members<br />";
-
?>
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? - if(session_is_registered('online')){
-
@mysql_query("UPDATE trv_online SET activity=now() WHERE session_id='".session_id()."'");
-
}
-
// Database connection information here
-
-
$maxtime = time() -100;
-
$sql = mysql_query("DELETE FROM trv_online WHERE UNIX_TIMESTAMP(activity) < '$maxtime'");
-
$rows = mysql_affected_rows();
10 2299
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?
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
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'.
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...
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.
Ok, this structure looks fine to me...
Is it working?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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.
|
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!
|
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...
|
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...
| |
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? ...
|
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.
|
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?
|
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:
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |