Question

I'm wanting to show the user a countdown from 60 to 0 seconds of some sorts, as my script limits to one action every 60 seconds. I don't need to update in real-time

I have limited the user from accessing the page, but can't figure out the right approach to show them when they can next access the page.

Here's what I currently have:

$sql = "SELECT * from travel WHERE username = '" . $_SESSION['user'] . "'";
$result = $dbh->query($sql);

if ($result->rowCount() == 0)
{
    $sql = "SELECT name FROM countries";
    $result = $dbh->query($sql);
    $rows = $result->fetchAll(PDO::FETCH_ASSOC);

    foreach ($rows as $row)
    {
        $sql = "SELECT country FROM users WHERE username = '" . $_SESSION['user'] . "'";
        $result = $dbh->query($sql);
        echo '<input type="radio" name="country" value="' . $row['name'] . '">'. $row['name'];

        $obj = new travel($_SESSION['user']);
        echo " ($" . $obj->calcCost($row['name']) .")";
        echo '<br />';
    }
    echo '
            <button type="submit" name="submit" value="Travel" />Travel</button>
            <div class="spacer">
        </form>';
}
else
{
    $row = $result->fetch(PDO::FETCH_ASSOC); 
    $time_left =  time()-60;
    $seconds = 60;

    echo "You can access this page in " . date("G:i:s", $seconds); // static value at the moment

    $del = "DELETE FROM travel WHERE time_left<$time_left";
    $res = $dbh->prepare($del);
    $res->execute();
}
Était-ce utile?

La solution

I can't see where you add the records to the travel table. In there, you need to save the latest access time, then you can use it in the calculation of the time remaining before the next access is permitted.

Then

$time_left =  time()-60;

could be changed to calc the difference between time() and the last access time, with a value of less than a minute being displayed as the time remaining.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top