jasy
jasy

Reputation: 45

Session timeout after 1 day

I have a homepage the user logins in too with this php code:

 <?php
    session_start();
    $username= $_SESSION['username'];

    if($_SESSION['username'] == "")
    {
        header("Location: http://new_system/");
    }

    require "connection.php";

    $result= mysqli_query($conn,"SELECT * FROM users WHERE username = '$username'");
    $row = mysqli_fetch_row($result);
    $firstname = $row[0];
    $lastname= $row[1];
    $email = $row[2];
    $birthday = $row[4];
    $gender = $row[5];
    $path = $row[8];


    ?>

I tried adding this

$_SESSION["timeout"] = time()+ (0*1*0*0);

So it logs out after a day but it didn't work, can anyone tell me why. I put it under the if statement for session

Upvotes: 0

Views: 7289

Answers (3)

BalaMurugan.N
BalaMurugan.N

Reputation: 118

Try this Code.

 $inactive = 60*60*24;
if( !isset($_SESSION['timeout']) )
$_SESSION['timeout'] = time() + $inactive; 

$session_life = time() - $_SESSION['timeout'];

if($session_life > $inactive)
{  session_destroy(); header("Location:index.php");     }

$_SESSION['timeout']=time();

Upvotes: 3

smartnet
smartnet

Reputation: 87

$_SESSION["timeout"] = time()+ (0*1*0*0);

Is incorrect Multiplying any number by zero (0) is zero (0)

Change to something like this For 10 minutes 10 * 60

For 24 hrs ( a day) 24 * 60 * 60

$_SESSION["timeout"] = time()+ (24 * 60 * 60);

You can echo it out to see the difference

Upvotes: 1

LF-DevJourney
LF-DevJourney

Reputation: 28529

You have also too set the cookie timeout. For the cookie default is the window session, when you close the browser, the cookie times out. the session_id is saved in the cookie, when the cookie expires, the session also expires.

Upvotes: 0

Related Questions