users online question...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
skateis2s
Forum Commoner
Posts: 37
Joined: Fri Aug 08, 2003 7:22 am
Location: colorado

users online question...

Post by skateis2s »

Hey I have a question, I need to display how many guest there are, how many registered users there are also...

I wrote the register script, but I dunno where to start with showing online users, any ideas on where to lead me? any help would be great! Thanks

:D :D :lol: 8O
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

I think that a good start would be to search the evilwalrus.com site for a code snippet of other useronline-code, and tweak that to match your prefered ideas. (Adding uid in the useronline table that points to the id in your memeber table etc.)

Using sessions you can easely find out who's a guest and who's a memeber as well as finding out when and how the user moves on/of site.

Happy coding.
User avatar
skateis2s
Forum Commoner
Posts: 37
Joined: Fri Aug 08, 2003 7:22 am
Location: colorado

Post by skateis2s »

I'll be sure to check that out... I do use sessions with my register/login script. Any other idea?
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post by toms100 »

on each page include a script which puts into a database the logged inusers last move. ie insert the time of the last page.
then on the main page (or whereever you want the users online) select from db and show only those in the last 15 minutes (or what ever)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Sound allright...

A word tho, if youre using some sort of template's or have your own 'engine' you only need to require/include the page once.

Code: Select all

// example of an index.php
<?
    require 'dbsettings.php';
    require 'sessions.php';
    require 'useronline'; // here
    ...do stuff...
    require 'myhtml.php';
?>
Okey, perhaps a bad example. But if you use for example index.php?section=MyCode kind of links on the entire site, you only need to require the onliners-counter code once. On each page will be overkill.
Last edited by JAM on Sun Aug 10, 2003 7:01 am, edited 1 time in total.
User avatar
Seth_[php.pl]
Forum Commoner
Posts: 30
Joined: Sun Aug 10, 2003 5:25 am
Location: Warsaw / Poland

Post by Seth_[php.pl] »

I've found a scipt that probably will help you :)
Here it is: http://codewalkers.com/seecode/75.html
User avatar
skateis2s
Forum Commoner
Posts: 37
Joined: Fri Aug 08, 2003 7:22 am
Location: colorado

Post by skateis2s »

okay thankyou seth, jam your idea makes sense to me kind of...

the script seth sent is very confusing to me, and i have my own auth script i wrote so ya... thanks seth

jam your idea kind of makes sense, im trying to think...

hmmmm like I want to be able to display the guest and the members online, Im trying to think how to do it, I have a user online script that im trying to configure to it, I just cant seem to do it...
<?php
//fill in some basic info
$server = "localhost";
$db_user = "xpcnbxc_csp";
$db_pass = "csp";
$database = "xpcnbxc_csp";
$timeoutseconds = 300;

//get the time
$timestamp = time();
$timeout = $timestamp-$timeoutseconds;

//connect to database
mysql_connect($server, $db_user, $db_pass);

if($logged_in_user) {
//insert the values
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','$REMOTE_ADDR','$PHP_SELF')");
if(!($insert)) {
print "Useronline Insert Failed > ";
}

//delete values when they leave
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
if(!($delete)) {
print "Useronline Delete Failed > ";
}

//grab the results
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'");
if(!($result)) {
print "Useronline Select Error > ";
}

//number of rows = the number of people online
$user = mysql_num_rows($result);


//spit out the results
mysql_close();
if($user == 1) {
print("$user member online\n");
} else {
print("$user members online\n");
}

}
?>
you can view what ive done here
http://is2s.net/csp its a registration script and stuff...
Last edited by skateis2s on Sun Aug 10, 2003 7:12 am, edited 1 time in total.
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post by toms100 »

dejavu :p
often if you are begging to learn php, try not to just copy and paste other scripts, but understand them and how they work and write your own from scratch. you will learn alot quicker
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Agrees...

Where's the fun int cutn-n'-pasting? =)
User avatar
skateis2s
Forum Commoner
Posts: 37
Joined: Fri Aug 08, 2003 7:22 am
Location: colorado

Post by skateis2s »

definetely guys, I read the script big time to see If I can understand it and rewrite it! I love learning PHP, any advice on how to do this or help, cause this is something I really want to learn! thanks 8) 8O
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Advice on learning?

For sure this is something I use all the time: Quick Reference Tips
User avatar
skateis2s
Forum Commoner
Posts: 37
Joined: Fri Aug 08, 2003 7:22 am
Location: colorado

Post by skateis2s »

I meant how to do show the guest and members online... :oops:
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Oh, hehe... Sorry...

Well, a tip could be (if you didn't allready think of it that is), that you make the script return an array...

Code: Select all

<?php

// index.php
 ...
 require 'useronline.php';
 $foo = useronline();
 // belov echo can be easely mover around your site/page...
 echo 'There are '.$foo['members'].' members and '.$foo['guests'].' 
guests online...';
 ...

// useronline.php
  ...
  function useronline() {
    ...
    $online['members'] = $memberresult;
    $online['guests'] = $guestresult;
    return $online;
  }
  ...
?>
...rather than echo'ing it out directly. Then anywere on the site/pages you can echo them out and move them around. Or something... Just tips.
User avatar
skateis2s
Forum Commoner
Posts: 37
Joined: Fri Aug 08, 2003 7:22 am
Location: colorado

Post by skateis2s »

okay so than I would need to make a useronline script that detects members and users... =[ :cry:

Any idea on how to do that?
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post by toms100 »

toms100 wrote:on each page include a script which puts into a database the logged inusers last move. ie insert the time of the last page.
then on the main page (or whereever you want the users online) select from db and show only those in the last 15 minutes (or what ever)
like this ;)
simplified:
make a file, action.php.
in it:

Code: Select all

<?php
$sql = "update db set (lastmove='".time()."') where username='".$_SESSION['username']."')";
// run teh sql
?>
include that in all your pages.
then to show users online in the last 10 mins:

Code: Select all

<?php

$sql = "select * from db order by lastmove DESC";
$result = mysql_query($sql);
$t=1
$timenow = time();
$intervalmins = 10; /// set the timelimit
while ($user = mysql_fetch_array($result) && $t=1) {
if (($timenow - $user['lastmove'] ) < ($intervalmins * 60)) {
echo $user['username']."<BR>";
} else {
$t=0; //stop looping
}


?>
Post Reply