AdRock Posted July 17, 2011 Share Posted July 17, 2011 I am getting this warning Notice: Undefined index: 5 in C:\Apache\htdocs\OB\php\userfuncs.php on line 448 which refers to this section // if there is no match for any session, return false so session is destroyed foreach($session_array as $key) { if(strcmp($key, $items[$key]) != 0) { return false; } } What is causing this warning? Do i need to wrap that code above in an if statement so it's only called if a session is registered> I can disable warnings as it works fine but i don't know the best way to make this code work as it should without warnings. Here is the whole code /** * This function is called by the checkLogin function * It connects to the datanase and gets the record with the users credentials in it. * It then creates 2 arrays, 1 for the database record and the other for the session. * it then compares each array with the other to see if there is a match. * If there is a match then everything is fine. if not someone has been tampering with the cookie so return false **/ $items = array(); while ($row = $result->fetch()) { $items = array( 'user_id' => $row['userid'], 'username' => $row['username'], 'email' => $row['email'], 'user_level' => $row['user_level'], 'encrypted_id' => md5($row['userid']), 'encrypted_name' => md5($row['username']), 'encrypted_email' => md5($row['email']), 'encrypted_user' => md5($row['user_level']) ); } $session_array = array( $_SESSION['userid'], $_SESSION['username'], $_SESSION['email'], $_SESSION['user_level'], $_SESSION['encrypted_id'], $_SESSION['encrypted_name'], $_SESSION['encrypted_email'], $_SESSION['encrypted_user'] ); // if there is no match for any session, return false so session is destroyed foreach($session_array as $key) { if(strcmp($key, $items[$key]) != 0) { return false; } } Quote Link to comment https://forums.phpfreaks.com/topic/242210-undefined-index-could-be-caused-by-sessions/ Share on other sites More sharing options...
wildteen88 Posted July 17, 2011 Share Posted July 17, 2011 You should only add the session keys to $session_array not the session values $session_array = array('userid', 'username', 'email', 'user_level', 'encrypted_id', 'encrypted_name', 'encrypted_email', 'encrypted_user'); Then your foreach loop should be foreach($session_array as $key) { if(strcmp($_SESSION[$key], $items[$key]) != 0) { return false; } } Quote Link to comment https://forums.phpfreaks.com/topic/242210-undefined-index-could-be-caused-by-sessions/#findComment-1243859 Share on other sites More sharing options...
AdRock Posted July 18, 2011 Author Share Posted July 18, 2011 I thought that worked for a minute I get this error message now. Notice: Undefined index: user_id Could it be becuase of the version of php i'm using locally? PHP Version 5.2.9-1 Quote Link to comment https://forums.phpfreaks.com/topic/242210-undefined-index-could-be-caused-by-sessions/#findComment-1244184 Share on other sites More sharing options...
AyKay47 Posted July 18, 2011 Share Posted July 18, 2011 This error is stating that the index of "user_id" is either not found in the $_SESSION array, or the $items array that is created. Can you show us the exact line that this error is occurring please. Quote Link to comment https://forums.phpfreaks.com/topic/242210-undefined-index-could-be-caused-by-sessions/#findComment-1244188 Share on other sites More sharing options...
AdRock Posted July 18, 2011 Author Share Posted July 18, 2011 on here if(strcmp($_SESSION[$key], $items[$key]) != 0) { Quote Link to comment https://forums.phpfreaks.com/topic/242210-undefined-index-could-be-caused-by-sessions/#findComment-1244211 Share on other sites More sharing options...
AyKay47 Posted July 18, 2011 Share Posted July 18, 2011 note that this isn't a warning and will not affect your code, this notice is simply telling you that the index of user_id does not exist, and according to the function that you have, will return false. Quote Link to comment https://forums.phpfreaks.com/topic/242210-undefined-index-could-be-caused-by-sessions/#findComment-1244223 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.