PHP - Can anyone see the problem in this code?For HostingRefuge

BlackStorm

New member
Hey,
This is the code of the script for the time online hack, can anyone find the error which is giving division by zero?

There is a line to stop division by zero but it doesnt seem to be working.


<?php

error_reporting(7);

/* Total Time Online Hack v1.0
Created on Thursday August 22, 2002
Copyright g-force2k2
Notice :: Please do not edit (including addons) this hack. Thanks for respecting my work! */

$templatesused = "time,time_leader";

require('./global.php');

// +++++++ Time Online Hack Configuration [ Edit Below Only ] +++++++
$usersperpage = "10"; // Configure this to show the number per page on the leader board!
// +++++++ Time Online Hack Configuration [ Don't Edit Below This Line ] +++++++

if(trim($action) == "") {
$action = "view_leader";
}

// +++++++ Start Viewing Total Time Online Users +++++++
if($action == "view_leader") {
$perpage = $usersperpage;
$totalusers = $DB_site->query_first("
SELECT COUNT(*) AS users
FROM user");
$totalpages = ceil($totalusers[0] / $perpage);
if($page == "") {
$page = 1;
}
$startat = ($page-1)*$perpage;
$leadtime = $DB_site->query("
SELECT userid, username, joindate, timeonline
FROM user
WHERE userid!=0
ORDER by timeonline DESC LIMIT $startat,$perpage");
while($lead = $DB_site->fetch_array($leadtime)) {
$userid = $lead[0]; $username = $lead[1]; $joindate = vbdate($dateformat,$lead[2]); $timeonline = dotimeonline($lead[3]);
$daysreg = floor((time() - $lead[2]) / 86400);
$daysreg = iif($daysreg == 0,'1','$daysreg'); // checking to see if not registered for more then a day to prevent division by zero
$dotimeperday = floor($lead[3] / $daysreg);
$timeperday = dotimeonline($dotimeperday);
eval("\$time_leader.= \"".gettemplate("time_leader")."\";");
}
if($page!=1) {
$prv = $page - 1;
$firstpage = "<a href='timeonline.php?s=$session[sessionhash]&action=view_leader&perpage=$perpage&page=1'\">[ First Page ]</a>";
$prevpage = "<a href='timeonline.php?s=$session[sessionhash]&action=view_leader&perpage=$perpage&page=$prv'\">[ Previous Page ]</a>";
}
if ($page!=$totalpages) {
$nxt = $page+1;
$nextpage = "<a href='timeonline.php?s=$session[sessionhash]&action=view_leader&perpage=$perpage&page=$nxt'\">[ Next Page ]</a>";
$lastpage = "<a href='timeonline.php?s=$session[sessionhash]&action=view_leader&perpage=$perpage&page=$totalpages'\">[ Last Page ]</a>";
}
eval("dooutput(\"".gettemplate("time")."\");");
}

?>
 
This might be the problem:

$dotimeperday = floor($lead[3] / $daysreg);

Also, if $lead[3] is a null value, maybe it should be $leads[3] or $lead[1,2,4,etc]

They might have the array value incorrect in $lead[x]

Right abose that write :

Code:
print "Test: $lead[3] / $daysreg<br>\n";

Do the math and see if it is legal.

Not really sure what this floor command does, never used it but this is what PHP says:

floor

(PHP 3, PHP 4 )
floor -- Round fractions down
Description
float floor ( float value)

Returns the next lowest integer value by rounding down value if necessary. The return value of floor() is still of type float because the value range of float is usually bigger than that of integer.
 
John,

You can also "quiet" the warnings.

Code:
 $dotimeperday = @floor($lead[3] / $daysreg);

But, this does not fix the problem as a math error is being produced and causing the warning message.
 
Is there anything else I can do that will stop the error warning and give the correct result?

I posted a reply in the thread on vbulletin.org but no-one replied and also created a new thread and havent got a reply to that either.
Im useless when it comes to PHP, I can only do VERY simple things using it
 
John,

Ok, but what was the results of:

print "Test: $lead[3] / $daysreg<br>\n";

I need to know if $lead[3] and $daysreg has a value
 
John,

If you want to send me FTP access I can try to fix this for you. Don't have a lot of time now because I am at work.
 
Not sure if this would cause a divide by 0 error, but ...

$usersperpage = "10"; // Configure this to show the number per page on the leader board!
// +++++++ Time Online Hack Configuration [ Don't Edit Below This Line ] +++++++

if(trim($action) == "") {
$action = "view_leader";
}

// +++++++ Start Viewing Total Time Online Users +++++++
if($action == "view_leader") {
$perpage = $usersperpage;
$totalusers = $DB_site->query_first("
SELECT COUNT(*) AS users
FROM user");
$totalpages = ceil($totalusers[0] / $perpage);

Why are you setting $usersperpage to the STRING value of 10? Since this is eventually used in a math calculation, you should set it to 10 so the variable is numeric.

I've never tried a math problem with a "string" variable, but changing this to a number (removing the "s) is worth a shot.
 
math in strings are not a problem. Just sometimes you have to parse them correctly. IE might show 3.0123
 
This is a hack for vBulletin from www.vbulletin.org
I am a disaster at PHP, have never have time to learn it because I got into app programming first.
Doesnt matter about the date because it still hasnt been fixed ;)

hehe thanks for the reply!
 
Ok got this fixed :)

Thanks for the help, this was the problem:

$daysreg = floor((time() - $lead[2]) / 86400);
$daysreg = iif($daysreg == 0,'1','$daysreg'); // checking to see if not registered for more then a day to prevent division by zero
$dotimeperday = floor($lead[3] / $daysreg);

had to be changed to

$daysregs = floor((time() - $lead[2]) / 86400);
if($daysregs == 0) { // checking to see if not registered for more then a day to prevent division by zero
$daysreg = 1;
} else {
$daysreg = $daysregs;
}
$dotimeperday = floor($lead[3] / $daysreg);

Thanks
John
 
Back
Top