undefined index: user

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joeymanzano
Forum Newbie
Posts: 2
Joined: Thu Oct 11, 2012 1:22 am

undefined index: user

Post by joeymanzano »

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'];
}

?>
Attachments
undefined index.jpg
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: undefined index: user

Post by twinedev »

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:

Code: Select all

function current_user(){
    return (isset($_SESSION['user'])) ? $_SESSION['user'] : FALSE;
}
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(){
    if (isset($_SESSION['user'])) {
        return $_SESSION['user'];
    } else {
        return FALSE;
    }
}
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: undefined index: user

Post by McInfo »

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

Post by joeymanzano »

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.
Attachments
login.jpg
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: undefined index: user

Post by McInfo »

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:

Code: Select all

if (array_key_exists('error', $_GET) and $_GET['error'] == '1') { }
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:

Code: Select all

function _GET ($index) {
    if (array_key_exists($index, $_GET)) {
        return $_GET[$index];
    }
    return null;
}
Then the condition would look like this:

Code: Select all

if (_GET('error') == '1') { }
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:

Code: Select all

if (@$_GET['error'] == '1') { }
Post Reply