Frantisek
Frantisek

Reputation: 7693

How does this PHP error happen?

In my index.php, I have something like this (simplified):

session_start();

// some code, login, etc

if (!isset($_SESSION['user'])){ die(); }

// continues

include('function.prepare.foodlist.session.php');

// more code

Yet, in my apache2's error log, this error returns sometimes (I cannot reproduce it personally, but it sometimes happen from other users):

PHP Notice: Undefined index: user in /path/to/function.prepare.foodlist.session.php on line 37

How can this happen?

EDIT

Per request, the line 37 in the function.prepare.foodlist.session.php file reads:

`foodlist-categories`.`name-".$_SESSION['user']['language']."` as `catname`

Upvotes: 0

Views: 68

Answers (2)

Abhay
Abhay

Reputation: 6645

it could actually be hard to suggest from where is the call to function.prepare.foodlist.session.php made but to avoid this notice, perhaps you may add isset() before line-37, something like:

if (isset($_SESSION['user'])) {
  // line 37
  // etc.
}

It also might be that your session is perhaps getting lost before line-37 by some piece of code that is conditionally executed or it might be that the session expires on its own.

Hope it helps!

Upvotes: 0

Pro.Ton
Pro.Ton

Reputation: 112

This warning indicates that you are using array index "user" does not define. Before you can use $_SESSION['user'], you should check whether the index/key 'user' is in the array $_SESSION.

if (isset($_SESSION['user']['language'])) {
    //do something with $_SESSION['user']['language'] like your code:
    `foodlist-categories`.`name-".$_SESSION['user']['language']."` as `catname`
} else {
    // default
   `foodlist-categories`.`name-en` as `catname`
}

It's probably a user who has not yet signed, or which expired cookie, or You unset $_SESSION['user'] before this line, or you call session_start() onece again.

Sorry for my english

Upvotes: 1

Related Questions