Problem with my Building script[solved]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Foofighter
Posts: 121
Joined: Sun Mar 04, 2012 1:52 pm

Problem with my Building script[solved]

Post 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']."'");
}
}
} 
Last edited by Foofighter on Mon Mar 19, 2012 8:39 pm, edited 1 time in total.
User avatar
Foofighter
Posts: 121
Joined: Sun Mar 04, 2012 1:52 pm

Re: Problem with my Building script

Post by Foofighter »

up, can anyone help me pls

greetz Foo
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Problem with my Building script

Post 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.
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”