Jump to content

Recommended Posts

I am having issues with a time out script I wrote.. Now at one time it worked fine but now it won't redirect/logout the user when they go past the time frame. I have tried a few different things but what happens is it just gets stuck with a blank page.

 

The original code was this.

<?php
$timeout = 30;
$ctime = time();
$stime = $_SESSION['ses_time'];
$newtime = $ctime - $stime; 

if ($timeout<$newtime)
{
header("Location: index2.php?action=time");
exit;
}
else
{
$_SESSION['ses_time'] = $ctime;
}
?>

I tried removing the exit command and that just lets the page go straight to the home page with no timeout.

 

Then I tried this by reversing the if statement

<?php
$timeout = 30;
$ctime = time();
$stime = $_SESSION['ses_time'];
$newtime = $ctime - $stime; 

if ($timeout>$newtime)
{
$_SESSION['ses_time'] = $ctime;
}
else
{
header("Location: index2.php?action=time");
exit;
}
?>

 

which just take it to the home page and will never time out.

 

I found some code on here that did what I wanted and I tried it but it did the same thing of either giving me a blank page or going directly to the home page.

 

They way i have it setup is that any thing that happens always includes this script at the very top of the page

 

 <?php session_start();
include ("timecheck.inc");
?>

 

So I am at a lost I can't figure out if the program is having problems reading the timeout script, if I have the header redirect wrong or what is going on. The only thing I haven't tried is to remove the include and put the script directly at the top of the page.

 

Any suggestions on what I can look at would be appreciated.

 

Thanks

Bill

Link to comment
https://forums.phpfreaks.com/topic/181893-solved-problems-wiht-time-out-script/
Share on other sites

So I have tested putting the script directly at the top of the pages prior to the html tag.

It still won't redirect. What happens is that it stays on Home.php but as the sessions are cleared out no data shows up just the basic layout.

 

I have checked things out and I don't understand why the redirect won't work.  I have even changed the " to ' and tried and all that did was bring up a blank page.. I know the time out is working but just don't see why the redirect isn't working..

 

Any suggestions?

 

Thanks

Bill

You can try this:

 

?php
$timeout = 30;
$ctime = time();
$stime = $_SESSION['ses_time'];
$newtime = $ctime - $stime; 

if ($timeout>$newtime)
{
$_SESSION['ses_time'] = $ctime;
}
else
{
echo '<META http-equiv="refresh" content="1;URL=http://www.yoursite.com/index2.php?action=time">';
}
?>

 

And see if that works, if it does it means there is probably an error and you have error reporting turned off. To turn on error reporting you can add the following code to the top of your script:

error_reporting(E_ALL);
ini_set("display_errors", "on");

 

And that will inform you of any errors by forcing the error reports to turn back on.

that actually worked.. it doesn't go directly to the time out page like I want there is a brief delay where you see the other page and then it flips over to time out page but it works.. thanks for the help, still confused why the header command stopped working for me, but as I said it works now and I am happy...

 

Thanks

Bill

I should also mention that when I turned on the error as suggestion it gave me

 

Warning: Cannot modify header information - headers already sent by....

 

So I am lookign for ways to correct that and what I got was to use ob_start();to control the out put and correct this issue.. I will try that but as for now I have a work around to my problems. :)

That means that something was sent to the browser between the session_start() and the header() calls.  It could be blank space in the include file.  Make sure the openning tag (<?php) in the include file starts in the first position of the first line; also make sure there is no carriage return (or other white-space) after the closing tag (?>) (some people just leave the closing tag out all together).

 

By the way, you are referring to $_SESSION['ses_time']; unconditionally at the beginning of the script.  If this is your actual code, I foresee problems when the user first visits the site (with a brand new session).  That element of the array will not exist, there will be another notice and the variable will evaluate to zero, which will cause your page to timeout.

i did the check for whitespaces and there weren't any I could find so I just started with the error report checking for duplicates and after i cleaned up the duplicates i took your suggestion and just rewrote the timecheck file and made sure there was no white spaces and now it works with no issues.

 

Thanks again for all the help...

 

Bill

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.