Jump to content


Photo

Session help: Undefined Index (I read the sticky)


  • Please log in to reply
8 replies to this topic

#1 Mahdi

Mahdi
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 12 March 2006 - 01:38 AM

This has really been confusing me. I have my php script set up to take the user ID out of the database, and put it into a session variable, however.. This will only work as long as I navigate through one directory.

i.e user/index.php --> user/account.php holds the info, but user/index.php --> account/index.php gives me this error:

"Notice: Undefined index: user_id in (host root dir)/account/index.php on line 14"

Now for the code..

Function that sets the info for $_SESSION['user_id']:
function getId()
}

$email = $_POST['txtEmail'];
$password = $_POST['txtPassword'];

$sql = "SELECT login_id
       FROM tbl_login 
       WHERE login_password = ('$password') AND login_email = ('$email')";

        $result = mysql_query($sql);
        $row = mysql_fetch_assoc($result);
        $_SESSION['user_id'] = $row['login_id'];
{

user/library/functions.php - holds the getId() function.
user/login.php - calls the function and registers the session.
user/account/index.php - tries to get the session info but fails (yes I put session_start() before I called the info) "Notice: Undefined index: user_id in (host root dir)/account/index.php on line 14"

The wierdest thing is about this, is that when I go back to user/login.php to check if the session is registered or not, it says that it is. But then I go back to account.php and of course its not.. stays this way till I unregister.


like I said.. if I keep in in the same directory everything runs fine. As far as I understand sessions are supposed to retain data no matter which directory you are on as long as staying in the root dir (or untill you unregister/close browser).

Anything helps. My main goal is to be able to hold this login_id info from 'blahblah/var.php' to 'blahblah/anything/var.php' Thank you very much in advance.

#2 saiko

saiko
  • Members
  • PipPipPip
  • Advanced Member
  • 37 posts

Posted 12 March 2006 - 01:12 PM

i am having the same problem in my skining system but no one has yet to help :(

#3 saiko

saiko
  • Members
  • PipPipPip
  • Advanced Member
  • 37 posts

Posted 14 March 2006 - 10:19 AM

*bump*

#4 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 14 March 2006 - 11:43 AM

someone please correct me if i'm wrong, but the last time i had this error myself, i believe i had the settings in my php.ini file too strict for reporting errors. the 'error' youre getting is just a notice, and i believe it's because you don't declare/set up $_SESSION['user_id'] before you assign a value to it.

open up your php ini file and find a line that says 'error_reporting', and change it to:

error_reporting = E_ALL & ~E_NOTICE

OR

error_reporting = 2039

which are both the same thing, and which will give you errors if you have them but not notices.
to be honest, and maybe someone will tell me it's bad practice, but i never personally set up a variable before declaring values to it. just seems too much trouble and a bit 'old school', so when i'm developing a system, i always have the error_reporting at the level i just mentioned.

if you don't have access to your php.ini file, then a line right at the top of your code like

ini_set("error_reporting",2039);

will do pretty much the same thing, albeit only for the script that you include the line on, without permanently changing the php.ini file.
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#5 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 14 March 2006 - 02:37 PM

Correct your errors, don't just hide them.

I have tested setting $_SESSION['user_id'] and moving between directories and it works fine. The only thing I can think of is that your server configuration is some how affecting it.

Sorry that I can't be of more help, but I am curious to see a solution.

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.


#6 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 14 March 2006 - 06:32 PM

[!--quoteo(post=354897:date=Mar 14 2006, 02:37 PM:name=txmedic03)--][div class=\'quotetop\']QUOTE(txmedic03 @ Mar 14 2006, 02:37 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Correct your errors, don't just hide them.

I have tested setting $_SESSION['user_id'] and moving between directories and it works fine. The only thing I can think of is that your server configuration is some how affecting it.

Sorry that I can't be of more help, but I am curious to see a solution.
[/quote]

yeah i never mentioned hiding them. but in some land, long long ago, it was common practice (and ONLY practice) to declare a variable before giving it a value, otherwise you had an 'unknown variable' error. nowadays, it's just ok normally to give a variable a value without actually declaring it first. from most of the scripts i see on here, and commercial/opensource scripts i see, it's common practice not to declare the variable first. but the problem with E_NOTICE is that it'll report all the little fiddly things that do not affect the way the script works at all, but seems to be there for the 'old fashioned' way of coding.

txmedic03, you'll prob find (pull up a phpinfo and check) that your error_reporting is prob set at 2039 (E_ALL & ~ E_NOTICE) or lower, wheras my guess is that if Mahdi and saiko did the same, they'd probably find a higher value, or E_ALL for example
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#7 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 14 March 2006 - 11:47 PM

You didn't specify a difference either, which could be misconstrude and lead to a code full of @'s or someone turning off error reporting all together. "Hey if I just turn off error reporting it won't say anything bad at all!" I find that to be a gigantic coding turn off. I strive for standard compliance and error free code, or at the very least code that knows what to do when it encounters an error. The last thing I want to happen is for my scripts to error out in someone's browser because I didn't think ahead and cover a highly probable scenario. I can't always cover every single error someone may encounter, but I can try to cover the majority. It was nothing personal, just trying to make sure those new to scripting don't do silly things. I suppose I should have been clearer in my statement.

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.


#8 saiko

saiko
  • Members
  • PipPipPip
  • Advanced Member
  • 37 posts

Posted 15 March 2006 - 12:20 AM

it aint setting for me because if it is set it will a page with a login form else it will display another page with there details on it and links to usercp etc.!!

now it is set on the home because i have echoed and done some parseing etc. and i can access usercp etc.
BUT other pages which i include in the skining system wont pickup the variables as set!!

plz help!!

#9 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 15 March 2006 - 12:26 AM

without wanting to hijack the thread, i totally agree with you, txmedic03.
i think that sometimes when you get used to your environment and deal with it, it's easy to code AROUND that environment. so one chap might have errors off totally, and code away - but normally be the first to encounter problems when they port their software or scripst to a different server. including the age old "it worked on my localhost, but when i uploaded it to my host provider....". and the one with it on no-holds-barred-fully-strict error_reporting will find that they bloat their code with bits that are, in normal cases, not needed and still get loads of notices and warnings for things that are, for the sake of getting the script to deliver, pointless. i guess i probably sit somewhere in the middle but have found that out by playing around.

notices, warnings and errors are displayed differently for a reason, and - from my own opinion rather than any documents, they go in order of 'disaster factor'.
- notices - i tend to always turn these off, as they tend to be for very strict coding practice rather than anything that will affect the end result
- warnings - this is where i start paying attention, as warnings seem to uncover very sloppy code practices, missing parameters, etc.
- errors - obviously these cannot be ignored as they halt your script

i'd probably recommend turning the setting up to its highest level when you first start out - to get used to how things work and why - if anyone ever remembers programming a Spectrum or a Commodore 64, etc, you'll know how picky they can be. but without trying to take too many loopholes and covering up of errors, as txmedic03 rightly pointed out, you'll soon realise what is going to get the job done and for your code to be versatile enough to work on any system you stick it on.


"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users