Code works on website but not localhost...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
orbdrums
Forum Commoner
Posts: 82
Joined: Wed Sep 14, 2011 11:42 pm

Code works on website but not localhost...

Post by orbdrums »

Hello,

The following code redirects to a start page when 10 minutes of inactivity is reached:

Code: Select all

session_cache_expire(10);
session_start();
$inactive = 600;
$redirect_inactive = 615;
if(isset($_SESSION['start_res'])) {
	$session_life = time() - $_SESSION['start_res'];
	if($session_life > $redirect_inactive) {
	    $_SESSION['start_res'] = time();
	} elseif($session_life > $inactive) {
	     unset($_SESSION['start_res']);
	    header("Location: start.php");
	}
}
$_SESSION['start_res'] = time();
This works as expected on my website however, instead of redirecting to start.php on my localhost, it refreshes the page that it's called from.
This used to work on my localhost because that's where I tested it, but for some reason it no longer redirects. Any suggestions would be greatly appreciated.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Code works on website but not localhost...

Post by requinix »

First things first: anything in your error log?
orbdrums
Forum Commoner
Posts: 82
Joined: Wed Sep 14, 2011 11:42 pm

Re: Code works on website but not localhost...

Post by orbdrums »

I'm looking at the system log and see nothing remarkable. My localhost setup is MAMP and I've cleared my cache files in the browser settings. Here is the only system log entry at the time of the refresh:
WebProcess[519]: Periodic CFURLCache Insert stats (iters: 363) - Tx time:0.434139, # of Inserts: 4, # of bytes written: 81477, Did shrink: NO, Size of cache-file: 4786176, Num of Failures: 0

Thanks for your help.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Code works on website but not localhost...

Post by requinix »

Well there is a bug in your logic:

Code: Select all

        if($session_life > $redirect_inactive) {
            $_SESSION['start_res'] = time();
        } elseif($session_life > $inactive) {
First comparison should be $session_life < $redirect_inactive. As you have now, 0-600 will do nothing, 600-615 will redirect,and >615 will reset the timer.

Depending on when you're hitting the pages, that could be the problem.
orbdrums
Forum Commoner
Posts: 82
Joined: Wed Sep 14, 2011 11:42 pm

Re: Code works on website but not localhost...

Post by orbdrums »

I really appreciate you analyzing my code however, after reading and trying your suggestion, I realized the code is okay and performing as intended because I want nothing to occur if 0-600, I want the the re-direct if 600-615,and if >615 I want the timer to be reset. The timer reset over 615 allows for user lag.

My issue seems to be a difference in server settings on my localhost and my hosted web server. This code works as expected on my hosted server but only refreshes the current page on my localhost instead of re-directing to start.php on my localhost.

I have tried changing the $inactive variable on my localhost but this has no apparent affect. If I change any text on the page, it's recognized immediately.

I'm not sure how to proceed with this. Any suggestion is greatly appreciated.

Thanks!
orbdrums
Forum Commoner
Posts: 82
Joined: Wed Sep 14, 2011 11:42 pm

Re: Code works on website but not localhost...

Post by orbdrums »

By the way, everything refreshes on my localhost EXCEPT the header re-direct header("Location: start.php"); I don't know if that helps but I've tried "include" as well.
orbdrums
Forum Commoner
Posts: 82
Joined: Wed Sep 14, 2011 11:42 pm

Re: Code works on website but not localhost...

Post by orbdrums »

Moving my comments from the first entry to below the code resolved this issue on my localhost and the code works on my hosted server as well. I can only guess this has something to do with the header() function on my localhost. I wanted to circle back and make sure folks has a resolution.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Code works on website but not localhost...

Post by requinix »

If moving code around fixes it then it sounds like you were outputting something (unintentionally) before the header(). But that would have shown up in the error log... if you had the right settings, so maybe that's it.

As for the timing, so there's a 15 second window during which one can get logged out for inactivity, and that's it? If you miss that window then you don't get logged out... If that's really what you want, alright, I'm just saying that seems really odd.
Post Reply