PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I'm trying to set a session variable with little luck. The variable is in an include. The parent file includes the session_start;. Also, I have a variety of session variables all around the site but don't have this problem with the others.
The first echo always results in an undefined variable error.
The second echo always results in 1.
And while I'm asking newbie questions... is there a way to avoid always checking if a session variable is set? I have a solution that has about 50 fields during a signup process and I'm getting lost in all the validation.
Interesting... If I put this in a seperate file, everything works. If I put it either in my include file or directly in ANY of the parent files, it doesn't seem to remember that the variable is set and it always restarts.
Considering that I have other session variables that are working fine, could there be something else that would clear it?
session_unset() frees all the sesion variables and you should put that when you have finished with your session. But as for it not working in an included file, try using require() instead in include(). I don't know why this works, but it has for me before .
I put an include in my include and it worked! Simply putting it directly in the first include didn't work
Then I tried putting it directly within the first include (as it was) and then as you recommended, I changed the reference from include_once to require_once. That also worked!
I guess that's one for good old fasioned experience.