Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    49
    Thanks
    1
    Thanked 1 Time in 1 Post

    Unregistered SESSION Variables

    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?

    Thanks
    Of all the things I've lost in my life time, my mind is the one I miss the most!

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,122
    Thanks
    2
    Thanked 327 Times in 319 Posts
    The correct method of writing code that references a variable that might or might not exist is to use the isset() function -

    PHP Code:
    if(isset($_SESSION['user_name']))
    {
    // code to execute if the variable is set
    } else {
    // code to execute if the variable is not set

    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    49
    Thanks
    1
    Thanked 1 Time in 1 Post
    Thanks... This was along the lines of what I was planning. I was just unsure if there was an alternate method.
    Of all the things I've lost in my life time, my mind is the one I miss the most!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •