'Getting some error like "Warning: Undefined array key" in my website in php [duplicate]
I'm doing validate session I'm getting some field is "Warning: Undefined array key", This issue is happened now warning error my php code is blow i have given please what is i'm doing wrong...thanks...
These fields are I'm getting the error:
Warning: Undefined array key "lastActiveTime"
Warning: Undefined array key "USERNAME"
Warning: Undefined array key "SESSION_ID"
Warning: Undefined array key "HTTP_USER_AGENT"
Warning: Undefined array key "REMOTE_ADDR"
php code :
function validate_session()
{
//error_reporting(E_ERROR | E_PARSE);
$time_outvalue = 18000; // for session timeout
$now = time();
$diff = $now - $_SESSION['lastActiveTime'];
$username = $_SESSION['USERNAME'];
$session_id = $_SESSION['SESSION_ID'];
if(($diff >= $time_outvalue && isset($_SESSION['lastActiveTime'])) || (($_SESSION['HTTP_USER_AGENT'] != md5($_SERVER['HTTP_USER_AGENT'])) && ($_SESSION['REMOTE_ADDR'] != $_SERVER['REMOTE_ADDR']))){ // Session timeout
setcookie("PHPSESSID", "", time()-3600);
unset($_SESSION['lastActiveTime']);
unset($_SESSION['username']);
session_destroy();
}
$_SESSION['lastActiveTime'] = time();
}
Solution 1:[1]
$_SESSION creates after session_start() (or if session.auto_start enabled in php.ini), you can add something like this isset($_SESSION) || session_start(); at begin.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | WinterSilence |
