Frizz Posted March 19, 2011 Share Posted March 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/231115-php-session-redirection-i-bet-some-of-you-might-laugh-at-this/ Share on other sites More sharing options...
Eyewash01 Posted March 19, 2011 Share Posted March 19, 2011 Well you could set a session variable with the time that they came onto the site, then do an AJAX call every so often to check against the time and when 30 mins has passed then redirect them to the new page. Quote Link to comment https://forums.phpfreaks.com/topic/231115-php-session-redirection-i-bet-some-of-you-might-laugh-at-this/#findComment-1189692 Share on other sites More sharing options...
creata.physics Posted March 19, 2011 Share Posted March 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/231115-php-session-redirection-i-bet-some-of-you-might-laugh-at-this/#findComment-1189699 Share on other sites More sharing options...
Frizz Posted March 19, 2011 Author Share Posted March 19, 2011 i wasen't even thinkin about a timestamp to get the time... Thanks alot creata.physics . Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/231115-php-session-redirection-i-bet-some-of-you-might-laugh-at-this/#findComment-1189731 Share on other sites More sharing options...
creata.physics Posted March 19, 2011 Share Posted March 19, 2011 Glad I was able to help you. If you need further assistance never hesitate to ask. Quote Link to comment https://forums.phpfreaks.com/topic/231115-php-session-redirection-i-bet-some-of-you-might-laugh-at-this/#findComment-1189733 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.