Jump to content

PHP Notice: Undefined index: error


vspravin

Recommended Posts

Hello guys,

              Iam getting the following php notice in the error_log:

PHP Notice:  Undefined index:  memberId in classErrorHandle.php on line 3

 

Here's the code snippet that causes this notice.

 

<?php

      2        session_start();

      3        $memberId = $_SESSION['memberId'];

      4

 

Any help is highly appreciated....

 

Cheers...

Pravin.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/105722-php-notice-undefined-index-error/
Share on other sites

it shouldn't give a notice is it is set, how are you setting it ?

$_SESSION['memberId'] = $memberId; //new /correct way

or

session_register($memberId); //old way

 

If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled.

you can use sessions in classes, most common problem is either

session_start();

is not used before setting or getting the sesion values

OR

output is being sent before the session_start(); function and making it fail..

 

 

from my first post change

<?php
$memberId = (!empty($_SESSION['memberId']))?$_SESSION['memberId']:"";
?>

 

to

<?php
if (session_id() == "") session_start();
$memberId = $_SESSION['memberId'];
?>

 

check to see if you still get the errors

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.