Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2006
    Thanked 0 Times in 0 Posts

    Session variables being overwritten:


    When a user lands on my home page, I grab several variables (referring page etc) and pass them into session variables. I then pass these into the database when they register on my site. The problem is that if the user clicks around on my site and clicks the link back to the home page, the same code that initially created the session variables overwrites them.

    Here's the code for the variable $_SESSION['src'] that I want to grab from a GET parameter:

    PHP Code:
    $_SESSION['src'] = $source;

    Again, the variables are writing just fine. The problem is that they are lost when the user clicks back onto the home page for a second time via the "Home" link. Then they overwrite to null because there is no GET parameter in the URL. Then when the user registers, the variable isn't passed into the database, it just comes up blank.

    Can anyone suggest a fix for this?


  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Denver, Colorado USA
    Thanked 340 Times in 332 Posts
    Yes, it is referred to as a "one-shot". You only set the variable if it does not exist. Once it exists, you don't set it again -
    PHP Code:
    $_SESSION['src'] = $source;

    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.


    Posting Permissions

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