Skip to content
Advertisement

PHP: Notice: Undefined index where the session variable is defined

I am making a registration system with an e-mail verifier. Your typical “use this code to verify” type of thing.

I want a session variable to be stored, so that when people complete their account registration on the registration page and somehow navigate back to the page on accident, it reminds them that they need to activate their account before use.

What makes this problem so hard to diagnose is that I have used many other session variables in similar ways, but this one is not working at all. Here’s my approach:

JavaScript

Now to check for the variable, I placed this at the top of the page.

JavaScript

Now, I used to have the page redirect to a new page, but I took that out to test it. When the page reloads from submit, my message in the if statement above appears and then I get an Notice: Undefined index: registrationComplete blah blah from the echoing of the session var!

Then if I ever go back to the page, it ignores the if statement all together.

I have tested for typos and everything, clearing session variables in case old ones from testing were interfering, but I am having no luck. A lot of Googling just shows people suppressing these errors, but that sounds insane! Not only that, but I am not getting the same persistence of session variables elsewhere on my site. Can someone point out if I’m doing something blatantly wrong? Help! Thanks!

FYI, I read several related questions and I am also a beginner, so I may not know how to utilize certain advice without explanation.

As requested, more code, heavily annotated to keep it brief

JavaScript

Advertisement

Answer

This came down to the basics of debugging/troubleshooting.

  1. Understand as much as you can about the technique/library/function/whatever that you’re trying to use.
  2. Inspect the salient bits and make sure that they are what you expect or what they should be. (There’s a slight difference between those two, depending on the situation.)
  3. If that doesn’t bring you towards a solution, step back and make sure you’re understanding the situation. This may mean simplifying things so that you’re only dealing with the issue at hand, i.e. create a separate, simpler test case which exposes the same problem. Or, it may simply mean that you stop coding and work through the flow of your code to make sure it is really doing what you think it is doing.

A typical issue with sessions not working is forgetting to use session_start() (near or at the top) of any page which uses sessions.

One of my favorite snippets of PHP code, for debugging:

JavaScript

I try to use print for debugging and echo for regular output. It makes it easier to spot debugging code, once it’s goes beyond a few trivial bits of output.

Meanwhile, var_dump will print a bit more info about the variable, like it’s type and size. It’s important to wrap it in <pre></pre> so that it’s easier to read the output.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement