Page 1 of 1

Problem with Header

Posted: Thu Aug 04, 2011 11:51 am
by gregerst
Hi,
Below I show the beginning of my index.php program, the code that I have in the beginning is not working for me what ever I try to do with it, I have run out of idea. What the code hopfully should do is to jump to program logout_member.php if there has been no activity for 5 minutes else run normaly, that is what happends for me, it just run as normal.

______________________________________________________________________________________
<?php
$maxlife = 300; // 5 minutes
session_start();

if (isset($_SESSION['last_accessed'])) {
if ($_SESSION['last_accessed'] < (time() - $maxlife)) {
# Session Expired go to LOGOUT program
header ("Location: ./wds_loginsystem/admin/logout_member.php");
} else {
# Session Still Active
$_SESSION['last_accessed'] = time();
}
} else {
# New Session
$_SESSION['last_accessed'] = time();
}
?>

<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/gmf_master.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>GMF Uppsala - Startsida</title>
<!-- InstanceEndEditable -->
<link href="css/uppsala_styles.css" rel="stylesheet" type="text/css" />

More code ofcource below!
____________________________________________________________________________________________________________________________

I hope for some help from you :D
Greger Stag

Re: Problem with Header

Posted: Thu Aug 04, 2011 2:57 pm
by genix2011
You are resetting the last accessed time even when it shouldn't (it's always set to time()):

this should work:

Code: Select all

<?php
$maxlife = 300; // 5 minutes
session_start();

if (isset($_SESSION['last_accessed'])) {
    if ($_SESSION['last_accessed'] < (time() - $maxlife)) {
        // reset session last accessed time
        unset($_SESSION['last_accessed']);
        # Session Expired go to LOGOUT program
        header ("Location: ./wds_loginsystem/admin/logout_member.php");
    }
} else {
    # New Session
    $_SESSION['last_accessed'] = time();
}
?>

Re: Problem with Header

Posted: Fri Aug 05, 2011 10:00 am
by gregerst
Hi, I test the change but with no better result.

I made up a test case shown below, 2 pages called sida5 and sida6. What I will should happening is that when VAR. "last_accessed" is lower than time() - $maxlife I will have a jump to sida6.php there some clean up will be done. So far nothing what I tested in different combinations have worked for me, no jump at all. I will be more than pleased if someone can find the problem and also have a solution for me.

8O Greger, run out of ideas! :?:


This is the code for sida5.php

<?php
$maxlife = 60; // 1 minuter
session_start();

function funk_test() {
if (isset($_SESSION['last_accessed'])) {
if ($_SESSION['last_accessed'] < (time() - $maxlife)) {
header ("Location: sida6.php"); // why dosent this work??? ********************
# exit();
} else {
# Session Still Active
$_SESSION['last_accessed'] = time();
}
} else {
# New Session
$_SESSION['last_accessed'] = time();
}
}
?>

<html>
<head><title>Page 5</title></head>
<body>
<h2>Page 5</h2>

<?php
$_SESSION["sidvisning"]++;
echo "Welcome, you have visited this page " . $_SESSION["sidvisning"] . " times!";
funk_test();
?>
<p><a href="sida5.php">Back to page 5 </a></p>

_________________________________________________________________

This is the code for Page 6.php

<?php
session_start();
?>

<html>
<head><title>Page 6</title></head>
<body>
<h2>Page 6</h2>

<?php
# Session Expired
session_unset();
session_destroy();
# Reset session last accessed time
unset($_SESSION['last_accessed']);

echo "Welcome, you have visited this page " . $_SESSION["sidvisning"] . " times!";

?>
<p><a href="sida5.php">Go back to page 5 </a></p>

_________________________________________________________________________

Re: Problem with Header

Posted: Fri Aug 05, 2011 12:18 pm
by genix2011
You are refreshing the page after the 5 min, right? It is not going to load it automatically.

Re: Problem with Header

Posted: Fri Aug 05, 2011 3:28 pm
by gregerst
Hi genix2011 and thanks for your answer.
I can't understand what you meant with your answer, My problem is that the "HEADER" statement not work for me. The meaning of my script is that I will jump to PAGE6 if timer value is exipred in the second IF statement.

/Greger