0

I am trying to saving the user login time in session array so that i may calculate the logout time later. My login_success file saving time in session successfully but the remaining two files are not saving the same time in sessions. Help me in finding out the problem please.

login_success.php

$sql2 = "SELECT DATE(login_time) AS date_part, TIME(login_time) AS time_part FROM log where username = '$USERNAME' ";
$res = mysqli_query($link, $sql2);

while ($row = mysqli_fetch_array($res))
{
    echo $row['time_part'];
    $_SESSION['time'] = $row['time_part'];

}

login.php

if(isset($_GET['action']) && $_SESSION['time'] )
{
    session_destroy();

    $start_time = $_SESSION['time'];
        $life = time() - $start_time;
        $_SESSION['life'] = $life;
        header('location:session_life.php?act=life');
}

session_life.php

<?php
include_once './include/db_connection.php';
session_start();
print_r($_SESSION);
if(isset($_GET['act']))
{
    if(isset($_SESSION['life']))
    {
        $lifee = $_SESSION['life'];
        print_r($lifee);
    }
}

1 Answers1

2

When you call session_destroy() in your login.php script, you have to call session_start() again in order to use $_SESSION variables again.

session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.

Source: php.net


In comment section you also mentioned you are getting different time format than you would like to.

Since time() function you use returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) and you are subtracting two values in this format, you get a difference in the same format.

With that knowledge, you can recalculate it to the real value you are interested in. For more reading about this topic, try looking at these links & questions:

Edit: Converting timestamp difference to Hours:Minutes:Seconds

$timeStampDifference = 1458290809;
echo gmdate("H:i:s", ($timeStampDifference/1000000)); // This should do the trick
Community
  • 1
  • 1
FanaticD
  • 1,416
  • 4
  • 20
  • 36
  • @FanticD I am getting the value $life = 1458290809. I want to convert into time format like h:m:s? Ho would i get that ? – bc110402307 Syed Zeeshan Haide Mar 18 '16 at 08:45
  • `$life = 1458290809; echo date("Y-m-d H:i:s", $life);` – rmondesilva Mar 18 '16 at 08:51
  • @bc110402307SyedZeeshanHaide Does this http://stackoverflow.com/a/6468171/3444151 answer solves your issue? :-) – FanaticD Mar 18 '16 at 09:04
  • @FanaticD Look at this ! I am doing the same thing but i am returning with strange time value. Like if the start time is 14:04:45 and i logout after just two minutes then the $life = time() - $start_time; $lifee = date("Y-m-d H:i:s",$life); shows the current date right, but the time value is very strange like 10:05:42 . Hope you understand that – bc110402307 Syed Zeeshan Haide Mar 18 '16 at 09:10
  • @bc110402307SyedZeeshanHaide I updated the answer with way of printing out the time spent in the system. For the given value in your first comment under my answer, it prints out 00:24:18. I tested it. – FanaticD Mar 18 '16 at 09:42
  • @FanaticD Thanks again. I tried your trick. When i logged in, the time was 14:58:46. Then after some seconds i logout to check the difference and it returns the value 00:24:18. Now you yourself check that is it right ? – bc110402307 Syed Zeeshan Haide Mar 18 '16 at 10:00
  • @bc110402307SyedZeeshanHaide Provide me please with echoed starttime and log off time, I'll do some calculation with that. I am at work, so I am limited on resources I can use. It is possible I made a mistake. – FanaticD Mar 18 '16 at 10:17
  • @bc110402307SyedZeeshanHaide One more thing occurred to me: You may have inconsistent timestamps - you are getting $_SESSION['time'] from database and the other one you generate via PHP, which could be the source of your problems here. The code above works, the calculation is correct. Your input data don't have to be. – FanaticD Mar 18 '16 at 10:49