-1

I have one page session.php and my code is:

 @session_start();
            if(!isset($_SESSION['uname']) || $_SESSION['uname'] == " " || !isset($_SESSION['uid']) || $_SESSION['uid'] == " ")
                   {
                echo "<script language='javascript'>";
                echo "window.location='../index.php'";
                echo "</script> ";
                exit;           
            }

And I also want to edit session timeout function this code is:

$_SESSION['start'] = time(); // taking now logged in time
        $_SESSION['expire'] = $_SESSION['start'] + (1* 10) ; // ending a session in 30 seconds
            $now = time(); // checking the time now when home page starts
            if($now > $_SESSION['expire'])
            {
                session_destroy();
                echo "Your session has expire !  <a href='logout.php'>Click Here to Login</a>";
            }
            else
            {
                echo "This should be expired in 1 min <a href='logout.php'>Click Here to Login</a>";
            }

But this code is not working so help me how can I set session time out in session.php page.

Thank You..

Caramiriel
  • 7,029
  • 3
  • 30
  • 50
SATSANGI
  • 29
  • 1
  • 2
  • 7
  • From the code posted above, you seem to always set the session start time when loading the page. Due to this, the session can never expire, since the time difference between the two `time()` statements is very tiny. – Caramiriel Nov 27 '13 at 07:00
  • Check this link http://stackoverflow.com/questions/3068744/php-session-timeout – atluriajith Nov 27 '13 at 07:00
  • check this - https://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes?answertab=oldest#tab-top – Rohit Suthar Jan 23 '17 at 11:55

1 Answers1

0

Can you try this, Because of, in your code you have setting expire time each & every time the page gets load, now added condition.

    @session_start();       
    if(!isset($_SESSION['uname']) || $_SESSION['uname'] == " " || !isset($_SESSION['uid']) || $_SESSION['uid'] == " ")
    {
        echo "<script language='javascript'>";
        echo "window.location='../index.php'";
        echo "</script> ";
        exit;
    }

    $_SESSION['start'] = time(); // taking now logged in time

    if(!isset($_SESSION['expire'])){
        $_SESSION['expire'] = $_SESSION['start'] + (1* 10) ; // ending a session in 30 seconds
    }
    $now = time(); // checking the time now when home page starts

    if($now > $_SESSION['expire'])
    {
        session_destroy();
        echo "Your session has expire !  <a href='logout.php'>Click Here to Login</a>";
    }
    else
    {
        echo "This should be expired in 1 min <a href='logout.php'>Click Here to Login</a>";
    }
Krish R
  • 22,583
  • 7
  • 50
  • 59
  • Thank You so much. But this code is not properly work error will be occurred – SATSANGI Nov 27 '13 at 07:17
  • When I have done login successfully then after 5 minute session will be expire... But... Whenever screen is put ideal only 5 minutes then went to going expire........!! – SATSANGI Nov 28 '13 at 09:37