Dirty Bird Design
Dirty Bird Design

Reputation: 5533

Specific use case to expire PHP SESSION

Other examples have not worked for me, curious how to go about making a session expire after 30 min. I have this code on a landing page that sets the session, so after filling out a form you go to the download page. The purpose of the session (and I know its not 100% secure) is to prevent people from going directly to the download page. The $_REQUEST stuff is just for tracking, we don't want people on these pages that didn't come from specifically formed landing page url's. I would like to make this session expiere after 30 min so a user could not keep going back to the download page bypassing the sign up page.

<?php
    session_start();
    $_SESSION["loggedIn"] = 'Y';
    //the below is for google analytics tracking purposes, please ignore.
    if (isset($_REQUEST['utm_source']) && (!empty($_REQUEST['utm_source']))) {
?>
<!DOCTYPE html>
     <head></head>
     <body>
         //all page code here
      </body>
 </html>
 <?php
     } else {

     header( 'Location: http://www.anothersite.com' ) ;

    }
  ?>

Upvotes: 0

Views: 166

Answers (1)

Jason OOO
Jason OOO

Reputation: 3552

Its better to use cookie and set expire time as (time()+(30*60)) or you can use session such as:

//set session to now + 30 minute
$_SESSION['expire'] = time()+(30*60);

//check 
if($_SESSION['expire'] <= time()){
 //destroy session
}

But there is one fact, session will be terminated automatically when user closes the browser window.

Upvotes: 1

Related Questions