tags:

views:

50

answers:

2

Hello i have this code:

<?php
include("/LIB/error.php");
session_start();
if (isset($_POST['Submit1'])) {
if (!isset($_SESSION['token'])) {goto dale;}

    if((time() - $_SESSION['token']) < 5) {
        error('Debes esperar 5 segundos para poder enviar otra informacion.');
    } else { 
        dale:
        $_SESSION['token'] = time();
        include("/LIB/HeadSQL.php");
        include("/LIB/comprueba.php");

    }
}

?>

I want to eliminate the GOTO instruction (because as you know is supported only in the last versions of php) in order to make more compatible my code, but i really cant figure out how to change the flow of the code (without repeating code) without the GOTO. Thanks for your help.

+5  A: 
<?php

include("/LIB/error.php");
session_start();
if (isset($_POST['Submit1'])) {

   // if 'token' isn't in SESSION then it jumps right away to where dale: was
   // otherwise it performs the check and if that fails it does dale: again
   if (isset($_SESSION['token']) && (time() - $_SESSION['token']) < 5) {
      error('Debes esperar 5 segundos para poder enviar otra informacion.');
   } else { 
      $_SESSION['token'] = time();
      include("/LIB/HeadSQL.php");
      include("/LIB/comprueba.php");
   }

}

?>
stagas
A: 

Re-order the if/else blocks (and then of course reverse the time condition) and it becomes easy.

<?php
include("/LIB/error.php");
session_start();
if (isset($_POST['Submit1'])) {

    if (!isset($_SESSION['token']) || (time() - $_SESSION['token']) >= 5) { 
        $_SESSION['token'] = time();
        include("/LIB/HeadSQL.php");
        include("/LIB/comprueba.php");
    }
    else {
        error('Debes esperar 5 segundos para poder enviar otra informacion.');
    } 
}

?>
SoapBox
stagas