php会话生存期始终为0

I'm using this code to set session life time in all of my pages and the login page

session_set_cookie_params(time()+120);
session_start();

and when i print_r session_get_cookie_params i get this

Array ( [lifetime] => 1441447767 [path] => / [domain] => [secure] => [httponly] => )

But the session expires immediately after closing the web page and when i open it again I get redirected to the login page. So what's the correct way to set session lifetime that keeps counting the new lifetime as long as the user keeps browsing the website. For example if the session lifetime is 120 seconds it doesn't expire unless the user has closed the web page and doesn't return in 120 seconds but if he returns to the website during these 120 seconds the session starts counting new 120 seconds again.

You need to check for the last activity time, updated each time someone visits a page:

if(($_SESSION['lastActivity'] + 120) < time()) {
    // timeout, destroy the session.
    session_destroy();
    unset($_SESSION);
    die('Timeout!');
} else {
    $_SESSION['lastActivity'] = time();
}