0

I created a function to save the location (city, lat, long) of the user. So when he change the page, I don't need to convert his location again. So I've a problem. The session might be saved because when I close my computer, go to another place and reopen the page, my old location is still saved. How I can forget the location of the user when he leave my website ?

Here is a code example for how I store the user location :

function user_location_update($args) {
    drupal_session_start();
    if (isset($_GET['lat']) && isset($_GET['lng'])) {
        $_SESSION['user-location'] = array(
            'lat' => $_GET['lat'],
            'lng' => $_GET['lng'],
            'city' => $_GET['city'],
        );
    } else {
        $_SESSION['user-location'] = array(
            'city' => $_GET['city'],
        );
    }       
}

Then here is an example how I use my data (I send them to JS by settings) :

drupal_add_js(array('userlocation' => (isset($_SESSION['user-location']) ? $_SESSION['user-location'] : array())), 'setting');
1

This is the best advice: https://stackoverflow.com/questions/8311320/how-to-change-the-session-timeout-in-php

To put it in a Drupal context, you'd have to add a new column to the user table that tracked the last time a user was active and check that on a user hook.

|improve this answer|||||
0

It somewhat depends on your requirements... Drupal also controls sessions, but clearing that will for the user to login again. If that is no problem for you, you could consider changing Drupal's settings.php file and change ini_set('session.gc_maxlifetime', 200000);.

These 200000 seconds are about 23 days. If you would lower that to be 1800 (30*60 seconds) it would last for half an hour.

From settings.php:

/**
 * Set session lifetime (in seconds), i.e. the time from the user's last visit
 * to the active session may be deleted by the session garbage collector. When
 * a session is deleted, authenticated users are logged out, and the contents
 * of the user's $_SESSION variable is discarded.
 */
ini_set('session.gc_maxlifetime', 200000);

If you want to clear the location, but keep the user logged in, you need to do implement a custom check.

A simple way to check this would be to add a timestamp in the session, and compare that every page load.

if ($_SESSION['user-location']['last-time'] < time() - 1800) { // Session older than 30 minutes
  // Recalculate location.
}
$_SESSION['user-location']['last-time'] = time(); // Update last accessed time.
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.