vspravin Posted May 15, 2008 Share Posted May 15, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/105722-php-notice-undefined-index-error/ Share on other sites More sharing options...
MadTechie Posted May 15, 2008 Share Posted May 15, 2008 a session with the key member doesn't exist update to session_start(); $memberId = (!empty($_SESSION['memberId']))?$_SESSION['memberId']:""; Quote Link to comment https://forums.phpfreaks.com/topic/105722-php-notice-undefined-index-error/#findComment-541653 Share on other sites More sharing options...
vspravin Posted May 15, 2008 Author Share Posted May 15, 2008 Hi MadTechie, thanks for the solution...it worked!!...but if the variable $memberId was already registered as a session variable, then why does PHP throw such notice...?.. thnx Pravin. Quote Link to comment https://forums.phpfreaks.com/topic/105722-php-notice-undefined-index-error/#findComment-541662 Share on other sites More sharing options...
MadTechie Posted May 15, 2008 Share Posted May 15, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/105722-php-notice-undefined-index-error/#findComment-541668 Share on other sites More sharing options...
vspravin Posted May 15, 2008 Author Share Posted May 15, 2008 iam setting it using the old way i.e. session_register($memberId); Quote Link to comment https://forums.phpfreaks.com/topic/105722-php-notice-undefined-index-error/#findComment-541678 Share on other sites More sharing options...
MadTechie Posted May 15, 2008 Share Posted May 15, 2008 i would update your code asap, As of PHP 4.2.0, the default value for the PHP directive register_globals is off, and it was completely removed as of PHP 6.0.0 so your going to get more and more problems by leaving it as is. Quote Link to comment https://forums.phpfreaks.com/topic/105722-php-notice-undefined-index-error/#findComment-541683 Share on other sites More sharing options...
vspravin Posted May 15, 2008 Author Share Posted May 15, 2008 also the problem here was that i was unable to access the session variable $memberId directly inside a class method.....n that is when php was throwing the Undefined Index notice.... Quote Link to comment https://forums.phpfreaks.com/topic/105722-php-notice-undefined-index-error/#findComment-541687 Share on other sites More sharing options...
MadTechie Posted May 15, 2008 Share Posted May 15, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/105722-php-notice-undefined-index-error/#findComment-541694 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.