View Full Version : Unregistered SESSION Variables

05-09-2008, 03:27 AM
I'm making the switch to PHP5 on my web host.

The web host that I am using has it's PHP5 configured to E_ALL. In the footer of my page I attempt to display $_SESSION['user_name']. When the user is not logged in - therefore no $_SESSION variables - I now get errors displayed.

"Undefined index: user_name in ..."

Without simply suppressing the error reporting, how should I correct this? I guess that I could always use IF's to check if the session variable has a value or I could pull the session variables into local variables. BUT is there another way to do this? What is considered the "correct" method to handle undefined session variables?


05-09-2008, 03:38 AM
The correct method of writing code that references a variable that might or might not exist is to use the isset() function -

// code to execute if the variable is set
} else {
// code to execute if the variable is not set

05-09-2008, 03:42 AM
Thanks... This was along the lines of what I was planning. I was just unsure if there was an alternate method.