Nel tutorial di oggi imparerete come applicare una scadenza personalizzata ad una sessione impostando la durata di "vita" di quest'ultima. Iniziamo!
< ?php /** * Return false if session variable is no longer valid otherwise return the value that * the session variable holds. * * @param string $name Session variable name * @return bool|mixed true if var has expired otherwise return the value of the variable */ function getSessionVar($name) { if (!isset($_SESSION[$name]) || !isset($_SESSION[$name.'_expires']) || (isset($_SESSION[$name.'_expires']) && $_SESSION[$name.'_expires'] < time())) { return false; } return $_SESSION[$name]; } /** * Set a session variable with an expiry time. * * @param string $name Session variable name * @param mixed $value Could be a string, int, array, object * @param int $expires The lifetime of the variable */ function setSessionVar($name, $value, $expires = 600) { $_SESSION[$name] = $value; $_SESSION[$name.'_expires'] = time() + $expires; } // Example if (!($total_messages = getSessionVar('total_messages'))) { $total_messages = getTotalMessages(); // Your own code to get the total setSessionVar('total_messages', $total_messages, 60); } // Use the variable $total_messages as you would normally ?>



Leave Your Response