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 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2006
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Session variables being overwritten:

    Hello.

    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:
    <?php
    session_start
    ();
    $source=$_GET['src'];
    require(
    "somefile.php");
    require(
    "someotherfile.php");
    $_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?

    Appreciated.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 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:
    if(!isset($_SESSION['src'])){
    $_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
    •