Page 1 of 1

Problem with my Building script[solved]

Posted: Sun Mar 11, 2012 7:07 pm
by Foofighter
Hi guys, this script should increase the level of my building by 1 if i click the button and the time for every level is atm 5sec - later i will replace this with a var. But the problem is the building is increasing its level on its own this means:
1) i just reload the page after the 5 sec - now i see my button and the level is increased also the time in the DB is set to 0
2)after realoding a second time - the building starts to increase its level without me clicking the button..
Hers the Code:

Code: Select all

Metallmine: <?php echo ausgabeplanets('ress_building1'); ?><br>

<?php  if (ausgabeplanets('ress_building1_time') > 0)
{
     ?> Restbauzeit: <?php $rest=ausgabeplanets('ress_building1_time') - time(); echo $rest; ?> <?php  
} 
else     
        { ?> 
        
                 <form action="#" method="post">
                    <input type="submit" value="Metallmine ausbauen" name="bau" >
                </form>
              
               <?php echo "Testtext1"; ?>
<?php   } ?>
Here are my functions:

Code: Select all

<?php
include ("connect.php");
include ("calculations.php");

//DB anbindung

//Funktion für Datenausgabe von planets tabelle
function ausgabeplanets($var)
{
    $abfrageplanets = mysql_query('SELECT ' . $var . " FROM planets WHERE id = '".$_SESSION['uid']."'");
    $row = mysql_fetch_object($abfrageplanets);
    return $row->$var;
}
//----------------------------------------------------------------------------------------------------------
$zeit = time();

function gebbau()
{
    if(ausgabeplanets('ress_building1_time') == 0)
        {    
        
        $zeit=time() + 5;
        mysql_query("UPDATE planets SET ress_building1_time = $zeit WHERE id ='".$_SESSION['uid']."'");
        }

if (isset($_POST['bau']))
    {    
    echo gebbau();
    }
}
//----------------------------------------------------------------------------------------------------------
//Function überprüft ob gebäude fertig ist
function gebfertig_rohstoff1()
{
if(ausgabeplanets('ress_building1_time')>0)
//Prüfen ob es eine Zeit gibt spricht weil sonst 1940 sek anzahl stehen würde
{

$rest = ausgabeplanets('ress_building1_time') - time();
    
if($rest < 0) 
{
mysql_query("UPDATE planets SET ress_building1 = ress_building1 + 1 , ress_building1_time = 0 WHERE id ='".$_SESSION['uid']."'");
}
}
} 

Re: Problem with my Building script

Posted: Wed Mar 14, 2012 7:47 pm
by Foofighter
up, can anyone help me pls

greetz Foo

Re: Problem with my Building script

Posted: Wed Mar 14, 2012 8:59 pm
by Jackolantern
I would believe this is happening because your script is simply checking a time-related field in the database and then reacting accordingly. If you reload the page, it is going to check that field again and react accordingly again. This can be a problem in PBBGs.

The answer is usually one of the following: Add a field in the database that keeps track of how many times the record has been checked to use in reload-catching logic or add a field that keeps track of how many times the countdown has fully occurred so that you can compare it on reloads. Or, on the previous page's script, create a long random number to pass forward to the next script. You store that random number as something like "lastNumber", and then compare it in the beginning of the building script. If the page is reloaded, the current random number will match the lastNumber stored, so you know it is a reload and not to process it. If it is different, you know they went back through the process again, and it is safe to process.

That last method is not a good option if it is something the player would want to do, because it would be exploitable (since users can really send any number they want, even by POST), although you may be able to set it up through SESSIONS instead. I, however, would still probably go with one of the other 2 methods. The last method is more for keeping players for accidentally buying extras in a shop if they refresh, or something like that.