Jump to content

PHP SESSION Redirection ( I bet some of you might laugh at this )


Frizz

Recommended Posts

Hi Guys,

This is my first time posting so let me get right to this.

 

I'm trying to create a PHP MYSQL webpage that would create a session, allowing an anonymous (guest) user to view a particular page.. lets say "page1.php" for a certain amount of time "30 mins" and then be redirected to "page2.php" if they go back (or refresh) "page1.php" when that time runs out.

 

The auto-redirection should only last about 3 hours ( session timeout ). Then they should be able to view "page 1" again. This sounds so simple in theory but i just cant wrap my head around doing it. Any help would be very appreciated.

Link to comment
Share on other sites

Sessions can store many things, variables, arrays, etc.

 

What you'd probably want for this is an array.  The first array [0] will contain defaults, the session expiration time, the amount of time a user has on a page, etc.

 

there are also many other ways to do this i suppose.

First off, I could create a test page and have a code like this.

<?php
// if time is not yet, you'll start your redirection script.
if ( isSet ($_SESSION['time']) )
{
$_SESSION['time'] = time();
$_SESSION['next_page'] = 'page2.php';  // the page where users will travel after the time expires
} 
// so now we have $_SESSION['time'] containing the time the session was created.
// you can keep that code in your page1.php file if it is the inital page visited.
// now once you've stored the time in a session, we can check and see if the users time is up on page`1.php

$expire = $_SESSION['time'] + 1800; //30 minutes from now, 1800/30=60
// so now we check, if the sessions set time, 
// say 9:00 AM is greater than the sessions time with 30 minutes added to it
// then we redirect
if ($_SESSION['time'] > $expire)
{
   // lets redirect and update the new sessions time
    $_SESSION['time'] = time();
    header("Redirect: $_SESSION['next_page']);
} 

This sends the user to the next page, and when a user travels to that page from the redirect, that page will need to check if a session is expired, and it will also need to define $_SESSION['next_page'].

 

This is just a very basic example, of how to store necessary information to a session, then check with basic arguements to see how to handle that session.  Of course you will need to modify this code to suit your needs, but that shouldn't be an issue.  If you have any further questions, feel free to ask.

 

Edit: Eyewash01 did mention ajax which did make me realise, you will need this if you want a user redirected WHILE in the middle of viewing a page, otherwise, the user can stay on the page for as long as he/she wants, but as soon as that page is refreshed, he/she will be redirected.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.