Forum Topic: Php: Users Online

(1,474 views • 31 replies)

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/8/06 08:09 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

Php: Main

In this tutorial, you will learn the basic way of showing how many users are currently browsing a webpage like this. This is useful because it can be a good "bragging" feature, and also tell you the time when most users are online. It is just one simple code that I will be explaining, but it does involve sql, so Ill give the table code below (It's only one table :)).

CREATE TABLE `users_online` (
`last_visit` int(11) NOT NULL default '0',
`ip` text NOT NULL
)

Just enter that into phpmyadmin or something and we are ready to start coding!
-----------------
The Code
Since this feature only needs one file/script to run, I will just simple give the code, then explain it. So, the code is below. Just place this on any file, because filename doesn't matter.

<?php
//Step 1
require 'connect.php';

//Step 2
$time=time();
$offset=60;
$most_time=$time-$offset;
$ip=$_SERVER['REMOTE_ADDR'];

//Step 3
$query="SELECT ip FROM users_online WHERE ip='$ip'";
$result=mysql_query($query);

//Step 4
if(mysql_num_rows($result)==0){
$query="INSERT INTO users_online VALUES('$time','$ip')";
mysql_query($query);
}else{
$query="UPDATE users_online SET last_visit='$time' WHERE ip='$ip'";
mysql_query($query);
}

//Step 5
$query="DELETE FROM users_online WHERE last_visit<=$most_time";
mysql_query($query);

//Step 6
$query="SELECT * FROM users_online WHERE last_visit>$most_time";
$result=mysql_query($query);

//Step 7
$users_online=mysql_num_rows($result);
echo "Users Online: $users_online";

//Step 8
mysql_close();
?>

-----------------
The Explanation
Now that you have the code in front of you, I will explain it step-by-step.

Step 1- The first step is simple. We just include the connection file that connect to the mysql database.

Step 2- In this step we declare some variables. The $time variable has the value of the seconds passed since January 1, 1970. The $offset variable can be any number you like. The default is 60 seconds. This variable is how long the user can be unactive before they are considered offline. The $most_time variable is the current time, minus the offset. This variable is used to determine whether or not to delete the user from the users_online table, or update them on that table with the current time. The $ip variable is simple the user's IP.

Step 3- In this step, we select the ip column from the users_online table where the ip column equals the user's IP.

Step 4- In this step, we check if the query returned any results. If it didn't, we insert the time and the user's IP into the users_online table. If the query did return results, instead of inserting the user into the table again or doing nothing, we update the last_visit column in the users_online table to the current time where the ip column equals the user's IP.

Step 5- In this step, we delete all rows in the users_online table where the last_visit column is less than or equal to the $most_time variable. In other words, we delete all rows where the IP column hasn't been updated in $offset seconds.

Step 6- In this step, we select all rows from the users_online table where the last_visit column is greater than the $most_time variable. In other words, we rows that have been updated in the past $offset seconds.

Step 7- In this step, we declare a variable called $users_online, and give it the value of the number of results returned when we selected rows from the users_online table. We then output that variable onto the page.

Step 8- In this step, we close the connection to the database for security reasons.
-----------------
There you have it! A complete script that can show you how many users are online. If you wish, you can put this code on all your web pages, to show you how many users are currently browsing your site. And, this tutorial doesn't require crons, the code itself deletes inactive users. Please post here questions, comments, or corrections.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

JeremysFilms

Reply To Post Reply & Quote

Posted at: 7/8/06 08:18 PM

JeremysFilms NEUTRAL LEVEL 17

Sign-Up: 02/18/05

Posts: 1,522

<?
echo $newTut521767;
?>

Output:

Sexy.


None

WoogieNoogie

Reply To Post Reply & Quote

Posted at: 7/9/06 12:41 AM

WoogieNoogie LIGHT LEVEL 14

Sign-Up: 06/26/05

Posts: 3,284

You know, someone could build a forum just using PHP: Main, I'd bet. This just adds to the list of amazing tutorials.


Questioning

bubblefizz1

Reply To Post Reply & Quote

Posted at: 9/27/06 05:34 PM

bubblefizz1 NEUTRAL LEVEL 01

Sign-Up: 09/27/06

Posts: 27

hi im not sure about step 1
connect.php?

do you make this file or not. can you explain what i need to do here please?


None

WoogieNoogie

Reply To Post Reply & Quote

Posted at: 9/27/06 05:56 PM

WoogieNoogie LIGHT LEVEL 14

Sign-Up: 06/26/05

Posts: 3,284

At 9/27/06 05:34 PM, bubblefizz1 wrote: do you make this file or not. can you explain what i need to do here please?

Yes, you make the file "connect.php", and you put your information for connecting to your MySQL database into it. It would look something like this...

<?php
mysql_connect("localhost", "myuser", "mypass");
mysql_select_db("mydatabase");
?>


None

bubblefizz1

Reply To Post Reply & Quote

Posted at: 9/27/06 06:25 PM

bubblefizz1 NEUTRAL LEVEL 01

Sign-Up: 09/27/06

Posts: 27

ive made the table, i made an index.php with the code on it, now ive made the connect.php with the code
and this is what i got:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'p28jazz'@'localhost' (using password: YES) in /home/p28jazz/public_html/connect.php on line 2

Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user 'p28jazz'@'localhost' (using password: NO) in /home/p28jazz/public_html/connect.php on line 3

Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /home/p28jazz/public_html/connect.php on line 3

Warning: mysql_query() [function.mysql-query]: Access denied for user 'p28jazz'@'localhost' (using password: NO) in /home/p28jazz/public_html/index.php on line 13

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/p28jazz/public_html/index.php on line 13

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/p28jazz/public_html/index.php on line 16

Warning: mysql_query() [function.mysql-query]: Access denied for user 'p28jazz'@'localhost' (using password: NO) in /home/p28jazz/public_html/index.php on line 18

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/p28jazz/public_html/index.php on line 18

Warning: mysql_query() [function.mysql-query]: Access denied for user 'p28jazz'@'localhost' (using password: NO) in /home/p28jazz/public_html/index.php on line 26

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/p28jazz/public_html/index.php on line 26

Warning: mysql_query() [function.mysql-query]: Access denied for user 'p28jazz'@'localhost' (using password: NO) in /home/p28jazz/public_html/index.php on line 30

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/p28jazz/public_html/index.php on line 30

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/p28jazz/public_html/index.php on line 33
Users Online:
Warning: mysql_close(): no MySQL-Link resource supplied in /home/p28jazz/public_html/index.php on line 37


None

Momo-the-Monkey

Reply To Post Reply & Quote

Posted at: 9/27/06 06:51 PM

Momo-the-Monkey EVIL LEVEL 35

Sign-Up: 10/15/05

Posts: 3,251

because you need to replace "myuser" and "mypass" and "mydatabase" with your information that your host gave you, or that you set up.....

Randosity is something you should see...
You should also see Gir's Soundboard...
[ PHP: Main | Music ]

BBS Signature

None

henke37

Reply To Post Reply & Quote

Posted at: 9/28/06 04:34 AM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,674

This looks like a great use for a replace query. With it you can skipt the select and allways update the table.

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


None

bubblefizz1

Reply To Post Reply & Quote

Posted at: 9/28/06 06:03 AM

bubblefizz1 NEUTRAL LEVEL 01

Sign-Up: 09/27/06

Posts: 27

At 9/27/06 06:51 PM, Momo-the-Monkey wrote: because you need to replace "myuser" and "mypass" and "mydatabase" with your information that your host gave you, or that you set up.....

i did change it to mine!
im thinking that its my phpmyadmin thats not right.
i have a database on my host with phpmyadmin, im trying to use that but sofar nothings worked im obviously doing something wrong in there.

do i HAVE to use the my servers databases or can i have my own?
as you can tell im new to php and databases


None

Momo-the-Monkey

Reply To Post Reply & Quote

Posted at: 9/28/06 06:11 AM

Momo-the-Monkey EVIL LEVEL 35

Sign-Up: 10/15/05

Posts: 3,251

At 9/28/06 06:03 AM, bubblefizz1 wrote:
At 9/27/06 06:51 PM, Momo-the-Monkey wrote: because you need to replace "myuser" and "mypass" and "mydatabase" with your information that your host gave you, or that you set up.....
i did change it to mine!
im thinking that its my phpmyadmin thats not right.
i have a database on my host with phpmyadmin, im trying to use that but sofar nothings worked im obviously doing something wrong in there.

do i HAVE to use the my servers databases or can i have my own?
as you can tell im new to php and databases

Well, looking at those errors, it says your connection is wrong, with the wrong password. Let me see the whole page you are using, and i'll help you with it. Just so it's easier...

Don't worry, I'm not going to hack into your database, just let me see the whole code so I can help you fix it.

Either waste your time copy and pasting it to NG, or copy and paste the whole thing into http://www.pastebin.ca

Randosity is something you should see...
You should also see Gir's Soundboard...
[ PHP: Main | Music ]

BBS Signature

None

bubblefizz1

Reply To Post Reply & Quote

Posted at: 9/28/06 07:47 AM

bubblefizz1 NEUTRAL LEVEL 01

Sign-Up: 09/27/06

Posts: 27

i tried again and now i have this:
Parse error: syntax error, unexpected T_STRING in /home/p28jazz/public_html/index.php on line 4

this is the whole scripts on my page:

<?php
//Step 1
require 'require "<?php
$DBHost = 'localhost';
$DBUser = 'p28jazz';
$DBPass = '*******';
$DBName = 'p28jazz_phpb1';
mysql_connect("$DBHost", "$DBUser", "$DBPass")or die(mysql_error());
mysql_select_db("$DBName")or die(mysql_error());
?>";';

//Step 2
$time=time();
$offset=60;
$most_time=$time-$offset;
$ip=$_SERVER['REMOTE_ADDR'];

//Step 3
$query="SELECT ip FROM users_online WHERE ip='$ip'";
$result=mysql_query($query);

//Step 4
if(mysql_num_rows($result)==0){
$query="INSERT INTO users_online VALUES('$time','$ip')";
mysql_query($query);
}else{
$query="UPDATE users_online SET last_visit='$time' WHERE ip='$ip'";
mysql_query($query);
}

//Step 5
$query="DELETE FROM users_online WHERE last_visit<=$most_time";
mysql_query($query);

//Step 6
$query="SELECT * FROM users_online WHERE last_visit>$most_time";
$result=mysql_query($query);

//Step 7
$users_online=mysql_num_rows($result);
echo "Users Online: $users_online";

//Step 8
mysql_close();
?>


None

bubblefizz1

Reply To Post Reply & Quote

Posted at: 9/28/06 11:45 AM

bubblefizz1 NEUTRAL LEVEL 01

Sign-Up: 09/27/06

Posts: 27

ok guys, i need help im confused as hell LOL

ive just downloaded wamp5 and i want to do all my testing etc...in there
can i still test these scripts without having to upload them?

ive tried 3 php tutorials and i havnt got 1 of them right yet, i dont want to use databases yet as i cannot figuer it out, so i want to test small easy scripts like this one!
please help


None

Jordan

Reply To Post Reply & Quote

Posted at: 9/28/06 12:05 PM

Jordan DARK LEVEL 14

Sign-Up: 04/23/06

Posts: 2,833

At 9/28/06 07:47 AM, bubblefizz1 wrote: require 'require "<?php
$DBHost = 'localhost';
$DBUser = 'p28jazz';
$DBPass = '*******';
$DBName = 'p28jazz_phpb1';
mysql_connect("$DBHost", "$DBUser", "$DBPass")or die(mysql_error());
mysql_select_db("$DBName")or die(mysql_error());
?>";';

Change that to just:
$DBHost = 'localhost';
$DBUser = 'p28jazz';
$DBPass = '*******';
$DBName = 'p28jazz_phpb1';
mysql_connect("$DBHost", "$DBUser", "$DBPass")or die(mysql_error());
mysql_select_db("$DBName")or die(mysql_error());


None

bubblefizz1

Reply To Post Reply & Quote

Posted at: 9/28/06 12:52 PM

bubblefizz1 NEUTRAL LEVEL 01

Sign-Up: 09/27/06

Posts: 27

like this?? : because this doesnt work either

<?php
//Step 1
$DBHost = 'localhost';
$DBUser = 'p28jazz';
$DBPass = '******';
$DBName = 'p28jazz_phpb1';
mysql_connect("$DBHost", "$DBUser", "$DBPass")or die(mysql_error());
mysql_select_db("$DBName")or die(mysql_error());
//Step 2
$time=time();
$offset=60;
$most_time=$time-$offset;
$ip=$_SERVER['REMOTE_ADDR'];


None

Jessii

Reply To Post Reply & Quote

Posted at: 9/28/06 01:05 PM

Jessii DARK LEVEL 36

Sign-Up: 02/10/05

Posts: 9,186

Are you sure that "localhost" is your host name? I know it's not the host for all servers (mine isn't localhost).


None

bubblefizz1

Reply To Post Reply & Quote

Posted at: 9/28/06 01:12 PM

bubblefizz1 NEUTRAL LEVEL 01

Sign-Up: 09/27/06

Posts: 27

whoooo ive done it!!!!
but....ive done it on my local server on my pc and not on my host!


None

bubblefizz1

Reply To Post Reply & Quote

Posted at: 9/28/06 01:43 PM

bubblefizz1 NEUTRAL LEVEL 01

Sign-Up: 09/27/06

Posts: 27

it seems im having problems with my database on the server, im using my host phpmyadmin, but i am only allowed 1 database and thats being used for a forum.
does this mean i cant do anymore??
as i did make the table for this script on my database for the forum, or do i HAVE to make a NEW database for new projects??
if this is the case i need to upgraid


None

Loccie

Reply To Post Reply & Quote

Posted at: 9/28/06 01:55 PM

Loccie NEUTRAL LEVEL 16

Sign-Up: 02/27/04

Posts: 1,059

At 9/28/06 01:43 PM, bubblefizz1 wrote: it seems im having problems with my database on the server, im using my host phpmyadmin, but i am only allowed 1 database and thats being used for a forum.
does this mean i cant do anymore??
as i did make the table for this script on my database for the forum, or do i HAVE to make a NEW database for new projects??
if this is the case i need to upgraid

Just change the databasename in your code to the one from your server, you don't need a new one.


None

bubblefizz1

Reply To Post Reply & Quote

Posted at: 9/28/06 02:01 PM

bubblefizz1 NEUTRAL LEVEL 01

Sign-Up: 09/27/06

Posts: 27

well thename of my database im using (phpb1 )doesnt seem to work, and i was wondering if its because i made the user_online table in the only 1 database i have which is full of the forum tables, thats why my database is called phpb1

if im doing this right, then its still not working, but yet it works on my pc phpmyadmin, confused help???


None

bubblefizz1

Reply To Post Reply & Quote

Posted at: 9/28/06 06:35 PM

bubblefizz1 NEUTRAL LEVEL 01

Sign-Up: 09/27/06

Posts: 27

Hey folks i got it working!!! i had to get rid of my forum database though :(

and i think that its not showing the correct number of users online, it said 1 online when i know that there were 2 online!!!


None

Momo-the-Monkey

Reply To Post Reply & Quote

Posted at: 9/28/06 10:10 PM

Momo-the-Monkey EVIL LEVEL 35

Sign-Up: 10/15/05

Posts: 3,251

Why did you put require 'require"

before you php tag?

Instead of
<?php
//Step 1
require 'require "<?php
$DBHost = 'localhost';
$DBUser = 'p28jazz';
$DBPass = '*******';
$DBName = 'p28jazz_phpb1';
mysql_connect("$DBHost", "$DBUser", "$DBPass")or die(mysql_error());
mysql_select_db("$DBName")or die(mysql_error());
?>";';

try

<?php
$DBHost = 'localhost';
$DBUser = 'p28jazz';
$DBPass = '*******';
$DBName = 'p28jazz_phpb1';
mysql_connect("$DBHost", "$DBUser", "$DBPass")or die(mysql_error());
mysql_select_db("$DBName")or die(mysql_error());
?>

Randosity is something you should see...
You should also see Gir's Soundboard...
[ PHP: Main | Music ]

BBS Signature

None

bubblefizz1

Reply To Post Reply & Quote

Posted at: 9/29/06 07:44 AM

bubblefizz1 NEUTRAL LEVEL 01

Sign-Up: 09/27/06

Posts: 27

Ive got it working its ok now!!!!!


None

Mister-Mind

Reply To Post Reply & Quote

Posted at: 12/18/06 06:30 AM

Mister-Mind EVIL LEVEL 07

Sign-Up: 07/01/06

Posts: 2,196

At 7/8/06 08:18 PM, JeremysFilms wrote: <?
echo $newTut521767;
?>

Output:

Sexy.

So true!

I just read through it, very interesting stuff!


None

DrRobot

Reply To Post Reply & Quote

Posted at: 8/19/09 12:14 AM

DrRobot DARK LEVEL 17

Sign-Up: 03/12/06

Posts: 398

Sorry to bump and old topic, but how would i modify this to work with an existing user system?

Basically, I'm looking to output all the users that are signed in, and guests that are viewing the page.

"XX members are logged in:
member1, member2, member3 ...
XX guests are viewing."

I'd like to o something like that. I can modify/add any DB's that are needed, and add/remove any rows too.


None

urbn

Reply To Post Reply & Quote

Posted at: 8/19/09 04:21 AM

urbn FAB LEVEL 18

Sign-Up: 06/10/07

Posts: 2,302

At 8/19/09 01:19 AM, rockymeet wrote: Hello sir I am FriendyAnil

What about rockymeet?

BBS Signature

None

WoogieNoogie

Reply To Post Reply & Quote

Posted at: 8/20/09 09:46 PM

WoogieNoogie LIGHT LEVEL 14

Sign-Up: 06/26/05

Posts: 3,284

At 8/19/09 12:14 AM, DrRobot wrote: Basically, I'm looking to output all the users that are signed in, and guests that are viewing the page.
"XX members are logged in:

For that, you'll just need to set things like this tutorial has. Timestamps of last page loaded in the user's table in the database, then to a cross check count of how many have a timestamp within the last few minutes.

member1, member2, member3 ...

This is a simple list from a MySQL query about the actual users that have those same timestamps.

XX guests are viewing."

This one is a little more difficult...the only way I could think of, is if a page load happens, check if it's a user, and if it's not, put a timestamp somewhere with the guests' IP. Then count them.

I'd like to o something like that. I can modify/add any DB's that are needed, and add/remove any rows too.

Should just need to add a timestamp to the user stuff, and maybe the guest IP stuff.


None

bgraybr

Reply To Post Reply & Quote

Posted at: 8/20/09 11:00 PM

bgraybr DARK LEVEL 09

Sign-Up: 06/30/08

Posts: 2,167

Whats the point of the time thing? Couldn't you make the "users" table have a column called "online" and change the value to "1" when they log in and "0" when they log out? Then all you would have to do is select from users where online=1.

The following sentence is true. The previous sentence is false. O_o PARADOX!
Linux Users Club | Linketylinklinklinklink...


None

henke37

Reply To Post Reply & Quote

Posted at: 8/21/09 01:34 AM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,674

Instead of focusing on the users, focus on the sessions. Surely your forum design uses sessions somehow? Then just read that data instead of the users.

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


None

WoogieNoogie

Reply To Post Reply & Quote

Posted at: 8/21/09 04:14 AM

WoogieNoogie LIGHT LEVEL 14

Sign-Up: 06/26/05

Posts: 3,284

At 8/20/09 11:00 PM, bgraybr wrote: Whats the point of the time thing? Couldn't you make the "users" table have a column called "online" and change the value to "1" when they log in and "0" when they log out? Then all you would have to do is select from users where online=1.

People don't log out...they close their browsers. Without a full out applet, you're not going to be able to tell when someone logs out without them actually going through the log out process. You have to get the last time they loaded a page.


None

CronoMan

Reply To Post Reply & Quote

Posted at: 8/21/09 04:24 AM

CronoMan EVIL LEVEL 06

Sign-Up: 07/19/04

Posts: 2,990

At 8/21/09 01:34 AM, henke37 wrote: Instead of focusing on the users, focus on the sessions. Surely your forum design uses sessions somehow? Then just read that data instead of the users.

First intelligent reply to this thread :P

"no sound in ass"


All times are Eastern Standard Time (GMT -5) | Current Time: 06:13 AM

<< Back

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!