Shoutbox

Users Online Script - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Users Online Script (/showthread.php?tid=52348)

Users Online Script by absorbation on 10-28-2005 at 03:44 PM

I need a quick script for the amount of usersonline :)

code:
<?php
// Configuration
$dbhost = "localhost";
$dbuser = "[to-add]"; // MySQL Username
$dbpass = "[to-add]"; // MySQL Password
$dbname = "[to-add]"; // Database Name
$timeoutseconds = 1200; // length of session, 20 minutes is the standard

$timestamp=time();
$timeout=$timestamp-$timeoutseconds;
$ip = substr($REMOTE_ADDR, 0, strrpos($REMOTE_ADDR,"."));

// Connect to MySQL Database
@mysql_connect($dbhost,$dbuser,$dbpass);
@mysql_select_db($dbname) or die("No db");

// Add this user to database
$loopcap = 0;
while ($loopcap<3 && @mysql_query("insert into useronline values('$timestamp','$ip','$PHP_SELF')"))
{ // in case of collision
$timestamp = $timestamp+$ip{0}; $loopcap++;
}

// Delete users that have been online for more then "$timeoutseconds" seconds
@mysql_query("delete from useronline where timestamp<$timeout");

// Select users online
$result = @mysql_query("select distinct ip from useronline");
$user = @mysql_num_rows($result);

mysql_free_result($result);
@mysql_close();

// Show all users online
if ($user==1) {echo $user.' user online';} else {echo $user.' users online';}
?>



I was using that but my new hosts server does not use it right.

It can be mysql stored or txt file stored I do not care, if anyone could make thid quick script I would appciate it :)
RE: Users Online Scipt by Fourjays on 10-28-2005 at 03:58 PM

Idr where I read it, but today I was reading up on making custom session handlers using PHP and MySQL, and they mentioned that one of their most useful purposes is for something like a "Users Online" script.

When a person visits your site, it stores a session in the database. It will remain there until they close their browser, at which point it unsets the session. Then 5 minutes after they have closed their browser, it deletes the session from the database. You would call the number of users online, by counting the number of sessions in the database.

http://www.zend.com/zend/spotlight/code-gallery-w...pen=1&anc=0&view=1
A page on custom sessions.


RE: Users Online Scipt by absorbation on 10-28-2005 at 04:00 PM

Yeh that's where my server messes up it seems it cannot store the ip address.

However the mybb one works :S Which is confusing so there must be mulitple ways of storing an ip address :P


RE: Users Online Scipt by WDZ on 10-28-2005 at 04:06 PM

Try changing

$REMOTE_ADDR

to

$_SERVER['REMOTE_ADDR']


RE: Users Online Script by absorbation on 10-28-2005 at 04:10 PM

http://www.msgstuff.com/useronline.php

Ok let's test :P thanks wdz, i'll see if it works ;)

edit: i wov you :kiss: