new member here, ...when im trying to try a simple code then i have encountered a undefined index: error ... i would like to know on how to fix this error.. thanks in advance.
with this code auth.php
<?php
$users = array(
"admin" => "adminpass",
"member1" => "member1pass"
);
//Returns true if $username and $password are valid
function credentials_valid($username, $password){
global $users;
return isset($users[$username]) &&
$users[$username] == $password;
}
//Logs into the user $user
function log_in($user){
$_SESSION['user'] = $user;
}
//Returns the currently logged in user (if any)
function current_user(){
return $_SESSION['user'];
}
?>
undefined index: user
Moderator: General Moderators
-
joeymanzano
- Forum Newbie
- Posts: 2
- Joined: Thu Oct 11, 2012 1:22 am
Re: undefined index: user
Undefined index indicated that code is trying to call a piece of an array that is not defined. Assuming line 24 is one of the lines above then it would be in the current_user() function. There is the only place you are trying to get an index of 'user'. The best thing to do is always check to see if it exists before using it such as:
This will either return the user value or FALSE if none is set. For clarification, the above code gives same resutls as:
Code: Select all
function current_user(){
return (isset($_SESSION['user'])) ? $_SESSION['user'] : FALSE;
}Code: Select all
function current_user(){
if (isset($_SESSION['user'])) {
return $_SESSION['user'];
} else {
return FALSE;
}
}Re: undefined index: user
Be aware that isset() will return FALSE if given a variable set to NULL.
-
joeymanzano
- Forum Newbie
- Posts: 2
- Joined: Thu Oct 11, 2012 1:22 am
Re: undefined index: user
thank you very much for the reply sir twinedev and sir McInfo,
sir twindev, ur code is really good my simple code its run well. thankyou very much..but i think i need more elaborate on how to use isset..hehehe
sir McInfo, thanks the for warning... hehehe
sir's how about this problem. can u pls help me on how to use isset on this problem, and add a simple elaborate if u don't mind. thanks again.
sir twindev, ur code is really good my simple code its run well. thankyou very much..but i think i need more elaborate on how to use isset..hehehe
sir McInfo, thanks the for warning... hehehe
sir's how about this problem. can u pls help me on how to use isset on this problem, and add a simple elaborate if u don't mind. thanks again.
Re: undefined index: user
As twinedev explained, the problem is that the code tells PHP to read from a variable that doesn't exist. PHP can write to variables that do not exist because it allocates the memory on-the-fly, but it cannot read from non-existent variables.
$_GET is an array variable that is always set, but the variables within it are not guaranteed to be set. So when you try to read the variable identified by the 'error' index within the $_GET array without first checking that the index (also called a key) exists, PHP complains with "Notice: Undefined index: error".
The solution is to first confirm that the index exists in the array by using array_key_exists() or isset(). Once you know that the index exists, it is safe to read from that position in the array.
You can do the check inline:
Because of short-circuit evaluation of the and operator (or && if you use that instead) the second condition ($_GET['error'] == '1') will not be evaluated if the first condition (array_key_exists('error', $_GET)) is false.
A cleaner solution is to write a function to check that the index exists before returning the value or return a default value if the index does not exist:
Then the condition would look like this:
Notice that a function call replaces the array syntax, so there is no dollar sign ($) and the square brackets ([]) are replaced with parentheses (()).
Another solution is to simply suppress the notice by inserting the error-suppression operator (@) before the variable:
$_GET is an array variable that is always set, but the variables within it are not guaranteed to be set. So when you try to read the variable identified by the 'error' index within the $_GET array without first checking that the index (also called a key) exists, PHP complains with "Notice: Undefined index: error".
The solution is to first confirm that the index exists in the array by using array_key_exists() or isset(). Once you know that the index exists, it is safe to read from that position in the array.
You can do the check inline:
Code: Select all
if (array_key_exists('error', $_GET) and $_GET['error'] == '1') { }A cleaner solution is to write a function to check that the index exists before returning the value or return a default value if the index does not exist:
Code: Select all
function _GET ($index) {
if (array_key_exists($index, $_GET)) {
return $_GET[$index];
}
return null;
}Code: Select all
if (_GET('error') == '1') { }Another solution is to simply suppress the notice by inserting the error-suppression operator (@) before the variable:
Code: Select all
if (@$_GET['error'] == '1') { }