tags:

views:

175

answers:

2

I wrote this

<?php
session_start();

if(!isset($_SESSION['now'])){
$_SESSION['now'] = time();
}

if(time() > ($_SESSION['now'] + 300)){
echo "15 minutes have passed";
} else {
echo "waiting";
}

?>

What I want to do is, let the user wait for 15 minutes before he/she can view the page. Is the code correct?

+2  A: 

Yes the code is correct, you can try it simply by having a smaller time constraint.

if(time() => ($_SESSION['now'] + 900)){

Just to be extra sure.

Try this to see what your variables are printing out.

<?php
session_start();

if(!isset($_SESSION['now'])){
$_SESSION['now'] = time();
}

if(time() > ($_SESSION['now'] + 10)){
 echo date("d.m.Y H:i", time())."<br />";
 echo date("d.m.Y H:i", $_SESSION['now'])."<br />";
 echo "15 minutes have passed";
} else {
 echo date("d.m.Y H:i", time())."<br />";
 echo date("d.m.Y H:i", $_SESSION['now'])."<br />";
 echo "waiting";
}

?>
Ólafur Waage
A: 

The time() function returns the number of seconds since the Epoch. 15 minutes is equal to 900 seconds, not 300. Other than that the code is correct.

Bobwise