"Notice: Undefined index"

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
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

"Notice: Undefined index"

Post by kettle_drum »

Just wondering how i can get rid of this error - "Notice: Undefined index: user" when i use the following code:

Code: Select all

$this->username = strtolower(IO::xss_check($_COOKIE['user']));
user is a variable in my cookie and xss_check is a method to check for xss in the IO class. Whatever i try i just cant seem to remove the Notice message - and i dont really want to just hide the error messages.

Thanks.
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

Don't you get this message when "user" isn't a valid index into the $_COOKIE array?

Trying echo'ing $_COOKIE['user'] and see if that error's by itself and if it contains what you're expecting.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Check if the array element is set first:

Code: Select all

<?php
if(isset($_COOKIE['user']))
{
    // do something

} else {

    // do something else
}
?>
Post Reply