Page 1 of 2
users online question...
Posted: Sun Aug 10, 2003 5:15 am
by skateis2s
Posted: Sun Aug 10, 2003 5:25 am
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.
Posted: Sun Aug 10, 2003 5:26 am
by skateis2s
I'll be sure to check that out... I do use sessions with my register/login script. Any other idea?
Posted: Sun Aug 10, 2003 6:54 am
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)
Posted: Sun Aug 10, 2003 7:00 am
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.
Posted: Sun Aug 10, 2003 7:01 am
by Seth_[php.pl]
I've found a scipt that probably will help you

Here it is:
http://codewalkers.com/seecode/75.html
Posted: Sun Aug 10, 2003 7:07 am
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...
Posted: Sun Aug 10, 2003 7:09 am
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
Posted: Sun Aug 10, 2003 7:14 am
by JAM
Agrees...
Where's the fun int cutn-n'-pasting? =)
Posted: Sun Aug 10, 2003 7:16 am
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

Posted: Sun Aug 10, 2003 7:20 am
by JAM
Advice on learning?
For sure this is something I use all the time:
Quick Reference Tips
Posted: Sun Aug 10, 2003 7:22 am
by skateis2s
I meant how to do show the guest and members online...

Posted: Sun Aug 10, 2003 7:36 am
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.
Posted: Sun Aug 10, 2003 7:53 am
by skateis2s
okay so than I would need to make a useronline script that detects members and users... =[
Any idea on how to do that?
Posted: Sun Aug 10, 2003 8:05 am
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
}
?>