Firefox PHP

<b>[code] Who's Online</b>

Posted by <b>Vulpes</b> 
Hello,

I've installed this hack (with Jules' adaptation) but the whoisonline page always shows no one to be online, and if I load callscript.php in my browser I get the error "Fatal error: Call to a member function on a non-object .... in callscript.php on line 20". Line 20 is as follows:

$q->query($DB, $ssql);

Does anyone have any idea what this is about? I'm really looking forward to being able to use this hack but I'm very new to php and mysql -- thanks in advance for any and all assistance.

Whoops, fixed it myself mere moments after posting -- I'd forgotten to put ' require "./common.php";' in callscript.php. No wonder. Thanks anyway!
Re: A bug?
February 22, 2002 11:10AM
Yeah, well? :)
Re: A bug?
March 06, 2002 06:24PM
Please?
A hint? a fix? something... :)
I've read all the posts regarding the Who's online script but being a PHP beginner (meaning I have got NO idea what I'm doing) it'd be great if someone could post the new script with an installation help file.
What do we have to do to get it running?

Any help is greatly appreciated.
Thank you
Jamie
curious_cat
Re: Who's online script &gt; need help!
March 24, 2002 04:48PM
This is a step by step summary of all the posts... I used it and it works fine...



STEP 1.
Edit common.php @ the end of the function calls added the lines:


function get_location($PHP_SELF,$REQUEST_URI,$title,$ForumName){
switch(basename($PHP_SELF)){
case "post.php":
$pagename="Posting Now Topic in <b><a href=\"$REQUEST_URI\">".$ForumName."</a></b>";
break;
case "whoisonline.php":
$pagename="Viewing this page";
break;
case "index.php":
$pagename="Viewing: <b><a href=\"$REQUEST_URI\">".$title."</a></b>";
break;
case "list.php":
$pagename="Reading: <b><a href=\"$REQUEST_URI\">".$ForumName."</a></b> Forum";
break;
case "read.php":
$readtitle=$title;
$readtitle=str_replace("-"," ",$readtitle);
$pagename="Reading: <b><a href=\"$REQUEST_URI\">".$readtitle."</a></b> in <b>".$ForumName."</b> Forum";
break;
}
return $pagename;
}
function get_whosonline($IP, $username, $pagename) {
global $DB,$q;
//Kill Old guys
$sql="delete from whosonline where ts < DATE_SUB(NOW(), INTERVAL 3 MINUTE) and ip!='record'";
$q->query($DB, $sql);
//Replace each entry with new one...if it doest exist it creates new one
$sql="replace INTO whosonline (ip, username, forum) VALUES('$IP', '$username', '$pagename')";
$q->query($DB, $sql);
//count them
$sql="SELECT count(*) as total FROM whosonline where ip!='record'";
$q->query($DB, $sql);
$users=$q->field("total", 0);

//check record table
$sql="SELECT username FROM whosonline WHERE ip='record'";
$q->query($DB, $sql);
$record=$q->getrow();
if($record[username] < $users){
$sql="UPDATE whosonline set username='$users' WHERE ip='record'";
$q->query($DB, $sql);
}
return $users;
}


STEP 2.
Using phpmyadmin ran the following:

CREATE TABLE whosonline (
ip char(15) NOT NULL default '',
ts timestamp(14) NOT NULL,
username char(255) NOT NULL default '',
forum char(250) NOT NULL default '',
PRIMARY KEY (ip),
KEY ts (ts),
KEY forum (forum)
) TYPE=MyISAM;

INSERT INTO whosonline VALUES ('record', 20011118030340, '1', 'Whatever');


STEP 3.
Create a file called "callscript.php". Added the following

<?PHP
if(!empty($phorum_auth)){

$whoisuser="<a href=\"profile.php?id=$phorum_user[id]\" class=\"topmed\">$phorum_user[name]</a>";

}else{

$name_cookie="phorum_name";
if(isset($$name_cookie) && empty($author)){
$whoisuser=$$name_cookie;
}
elseif(!isset($user)){
$whoisuser="Guest";
}
}
$IP=$REMOTE_ADDR;

?>
<br><a href="whoisonline.php?f=<?php echo $f; ?>"class='topmed'>Who's Online</a>

<?PHP
$ssql="SELECT username FROM whosonline where ip='record'";
$q->query($DB, $ssql);
$record=$q->getrow();
$pagename=get_location($PHP_SELF,$REQUEST_URI,$title,$ForumName);
echo "<br> <font color =white> ".get_whosonline($IP, $whoisuser, $pagename)." now/".$record['username']; ?> Max
</font>




STEP 4.
Create a file called "whoisonline.php". Added the following

<?php

require "./common.php";
if(file_exists("$include_path/header_$ForumConfigSuffix.$ext")){
include "$include_path/header_$ForumConfigSuffix.$ext";
}
else{
include "$include_path/header.$ext";
}
if($f!=0){
$table_width=$ForumTableWidth;
$table_header_color=$cell_color_2;
$table_header_font_color=$ForumTableHeaderFontColor;
$table_body_color_1=$ForumTableBodyColor1;
$table_body_font_color_1=$ForumTableBodyFontColor1;
$nav_color=$ForumNavColor;
}
else{
$table_width=$default_table_width;
$table_header_color=$cell_color_2;
$table_header_font_color=$default_table_header_font_color;
$table_body_color_1=$default_table_body_color_1;
$table_body_font_color_1=$default_table_body_font_color_1;
$nav_color=$default_nav_color;
}
$title = "Who's Online";
$ForumName="Who's Online";

?>
<table cellpadding=2 cellspacing=1 border=0 width="100%"><tr><td>
<table border=0 cellpadding=0 cellspacing=0 width="100%"><tr><td>
<table cellpadding=3 cellspacing=1 border=0 width="100%">
<TR <?PHP echo bgcolor($table_header_color); ?> ALIGN="LEFT">
<TD><FONT color="<?PHP echo $table_header_font_color; ?>" nowrap>Name</FONT></TD>
<TD><FONT color="<?PHP echo $table_header_font_color; ?>" width="100%">Place On Website</FONT></TD>
</TR>
<?php
$sql = "SELECT * FROM whosonline where ip!='record'";
$q->query($DB,$sql);
$rec=$q->getrow();
while(is_array($rec)){
echo "<TR bgcolor='$table_body_color_1' ALIGN=\"LEFT\">";
echo "<td nowrap bgcolor=$cell_color_1>$rec[username]</td><td width=\"100%\">$rec[forum]</td>";
echo "</tr>";
$rec=$q->getrow();
}


?>
</TABLE>
</TD></TR>
</TABLE>
</td></tr>
</table>
<?PHP
if(file_exists("$include_path/footer_$ForumConfigSuffix.php")){
include "$include_path/footer_$ForumConfigSuffix.php";
}
else{
include "$include_path/footer.php";
}
?>


STEP 5.
Edit the ./include.header.php file. Afer the <br> tag added the following


<table width="100%" cellspacing="0" cellpadding="0" border="0">
<TR>
<td width=100% colspan=3 align=right <?php include "callscript.php"; ?>
</TD>
</TR>
</TABLE>


That's it.
Thank you curious_cat, for the complete installation script.
I'm afraid I still have some questions about step 2.
Where can I get PHPMyAdmin and is it easy to run?
I'm sorry for asking so many questions but I don't know much about PHP yet. On the other hand if anyone of you ever needs help with PhotoShop, let me know :-) That's where I'm good at.
Thanks, Jamie
You can get phpmyadmin at [phpwizard.net]

The installation is VERY easy, just edit the conf file (put your username, databasename, password and mysql server if different than localhost).

They did a great job there to make it newbie proof :)
Thanks a million times curious_cat.
I can't believe it really worked. But it did! Yipee!!!
Thanks
Jamie
curious_cat
Re: [code] Who's Online
March 26, 2002 07:04PM
Hey don't thank me I just compiled everything together in a nice format. Jues, Vulpes and the like deserve the credit. Heck I don't even know how to code php :)
New Problem
March 29, 2002 11:16AM
I am not sure if I just cant remember how to get this working but I am at it again.

I receive the folowing error message:


Who's Online
Fatal error: Call to a member function on a non-object in /home/kholguin/www/phorum-3.3/common.php on line 315


Now here is the problem: I have the who's online stuff working on a certain web page and I am trying to move it into the header location (basically moving it around the web page). It works in it's original location but not when I place it in a "include" object for the header...

Any ideas what causes the above error? It looks like it is in the "get_location" function of common.php....

Thanks,
Jules
Re: New Problem
March 30, 2002 07:51AM
check your paths...

note if your site is only forums, then it would fit in great in the header like it does here on this site...otherwise you'll have to do some editing =P

this script requires the common.php in order to function properly...
Re: [code] Who's Online
June 04, 2002 08:17AM
Hi,
good job Vulpes :-)
I tryed it with phorum 3.3.2c and it work fine.

Some work around to make it work with postgresql:

1) in common.php:
[...]
//Kill Old guys
$sql="delete from whosonline where ts < NOW() - INTERVAL '3 MINUTES' and ip!='record'";
$q->query($DB, $sql);
//Replace each entry with new one...if it doest exist it creates new one
$sql="delete FROM whosonline where ip='$IP'; insert INTO whosonline (ip, username, forum) VALUES('$IP', '$username', '$pagename')";
$q->query($DB, $sql);
[...]
if((int)$record[username] < (int)$users){
$sql="UPDATE whosonline set username='$users', ts='now' WHERE ip='record'";
$q->query($DB, $sql);
[...]

2) dump:

CREATE TABLE "whosonline" (
"ip" character(15) DEFAULT '' NOT NULL,
"ts" timestamp with time zone DEFAULT 'now' NOT NULL,
"username" character(255) DEFAULT '' NOT NULL,
"forum" character(250) DEFAULT '' NOT NULL,
Constraint "whosonline_pkey" Primary Key ("ip")
);

CREATE INDEX ts_whosonline_key ON whosonline USING btree (ts);
CREATE INDEX forum_whosonline_key ON whosonline USING btree (forum);

INSERT INTO "whosonline" VALUES ('record','2001-11-18 03:03:40','1','Whatever');

And it's all, now a question:
"delete ...; insert ..." is not atomic, is best use transaction?
May be better "update ... or insert ..." ?

Thanks Vulpes and thanks curious_cat,
Gianluca.
Re: [code] Who's Online
June 05, 2002 12:07AM
my code uses replace, it does combine insert and update, i dont know if pgsql got that kind of function, since im not familiar with it.

Sancio
Re: [code] Who's Online
June 07, 2002 10:18AM
Vulpes wrote:

> my code uses replace, it does combine insert and update, i
> dont know if pgsql got that kind of function, since im not
> familiar with it.

I now:
REPLACE works exactly like INSERT, except that if an old record in the table has the same value as a new record on a unique index, the old record is deleted before the new record is inserted. See section 6.4.3 INSERT Syntax.

But pgsql don't support it :-(

But phorum seems execute $sql in a transaction and so it works fine:

$sql="DELETE FROM whosonline WHERE ip='$IP'; INSERT INTO whosonline (ip, username, forum) VALUES('$IP', '$username', '$pagename')";
$q->query($DB, $sql);

A alternative:

$sql="UPDATE whosonline SET ip='$IP', ts='now', username='$username', forum='$pagename' WHERE ip='$IP'";
$q->query($DB, $sql);
$sql="INSERT INTO whosonline (ip, username, forum) VALUES('$IP', '$username', '$pagename')";
$q->query($DB, $sql);

I modified whosonline table to have ip and username as primary key (and where in delete for pgsql), so users that use same proxy is logged distinct, but the problem is "Guest" users, is possible use php session (cookies,...) to trace users?

I'm sorry for my bad English,
Sancio.
Paolo
Re: [code] Who's Online
June 15, 2002 09:15AM
Thanks,
now it works perfectly on my phorum that uses postgresql 7.2.1 :-)))

Paolo.
Re: [code] Who's Online
July 22, 2002 03:42PM
great news

Re: [code] Who's Online
August 19, 2002 04:52PM
Someone pointed out a bug to me.

When the url is fetched for the link where a user is on the forum, and that user doesnt use cookies, it is possible to take over thier session, since the authentication information is send with the link and so in the whoisonline page.

Thanks to sab for finding this.

I recently dont have a fix. (Time is against me :( )
Re: [code] Who's Online
August 19, 2002 04:55PM
Hmmz, dont know if this is just a bug or a security issue but....


When clicking "edit my post" I get an edit window with the first post of this thread. I didnt try to edit it, so dont know if it just looks like im editing the first message, or it only displays it.
Re: [code] Who's Online
August 20, 2002 12:16PM
use this string instead of $REQUEST_URI

$url = ereg_replace("[&?]phorum_auth=[a-zA-Z0-9]+", "", $REQUEST_URI);

Sorry, you do not have permission to post/reply in this forum.