Session works, just not the first time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ArizonaJohn
    New Member
    • May 2009
    • 21

    Session works, just not the first time

    Hello,

    On my site, a user enters a value into a form, and if that value is not in my database, the code below is meant to give the user the message "The topic "value" has not been added. Add the topic "value"." When I pull up my site in Firefox and enter in a non-existent value for the first time, the message says "The topic "" has not been added. Add the topic ""." Then if I enter in a second non-existent value, it works correctly.

    When I pull up my site in Google Chrome and enter in a non-existent value for the first time, the message is populated with the last non-existent value that I had previously looked up before closing the tab. Then the second time I look up another non-existent value, everything is fine.

    So somehow, the first time I use the code below after loading the site in a browser, the session variable $find is not getting pushed through to the end, but the second time I use the code it all works fine. Can anyone see any reason why?

    Thanks


    My index page has this:

    Code:
    <?php
    session_start();
    unset($_SESSION['find']);	
    ?>
    
     <form action="search.php" method="post">
      <label>Enter Topic:
      <input type="text" name="find" size="55"/>
      <input type="hidden" name="searching" value="yes" />
      <input type="submit" name="search" value="Search" />
      </label>
      </form>
    Then search.php has this:
    Code:
    <?php
    ob_start();
    session_start();
    unset($_SESSION['find']);	
    $find = strip_tags($_POST['find']);
    $find = trim ($find);
    $find = strtolower($find);
    $_SESSION['find'] = $find;
    ?>
    
    $anymatches=mysql_num_rows($result);
    if ($anymatches == 0)
    {
    
    $_SESSION['find'] = $find;
    header("Location:search2.php");
     exit;
       
    }
    Then search2.php has this:

    Code:
    <?php
    session_start();	
    $_SESSION['find'] = $find;
    ?>
    
    print "<p class=\"topic2\">The topic \"$find\" has not been added.</p>\n";
    
    print "<p class=\"topic2\">Add the topic \"$find\".</p>\n";
  • prabirchoudhury
    New Member
    • May 2009
    • 162

    #2
    Session works, just not the first time

    1. in the index page you have't set this session but calling for unseat. first time you din't set any session

    Code:
    should be 
    
    if ($_SESSION['find']){
    
    unset($_SESSION['find']); 
    
    }
    2. same problem in search.php page havent set any session but directly calling for unset

    Code:
    should be 
    
    if ($_SESSION['find']){
    
    unset($_SESSION['find']); 
    
    }
    
    then define session with new value
    :)

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      Line #4 in your search2.php code.
      Shouldn't that be reversed?

      I'm guessing you meant to assign the session variable to the local variable.
      The way it is now, you appear to be assigning a unset local value to the session variable, thus leaving both without a value.

      P.S.
      Does your server have the 'register_globa ls' directive enabled?
      That would explain a lot.

      You can check it out by doing:
      [code=php]<?php
      if(ini_get('reg ister_globals') ) {
      echo "register_globa ls is On";
      } else {
      echo "register_globa ls is Off";
      }
      ?>[/code]
      Last edited by Atli; Jun 4 '09, 11:46 AM. Reason: Added the P.S.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Originally posted by prabirchoudhury
        1. in the index page you have't set this session but calling for unseat. first time you din't set any session
        This doesn't really matter.

        Unsetting a session value that hasn't been set doesn't really do anything.
        The unset function simply... does nothing.

        In this case it has no effect on the code.

        Although, I do agree that you should test if the variable actually exists before unsetting it. May not seem important in cases such as these, but it's best to just start doing it now so it doesn't come back to bite you later.

        Comment

        • ArizonaJohn
          New Member
          • May 2009
          • 21

          #5
          Hi,

          Register globals in on.

          I have unset($_SESSION['find']); at the top of my index page because the user has the option of returning to it. In that case, I want to cancel the value of $find.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Ok.

            Since register_global s is on you have to be extra careful with the variable names you use. For example, avoid using local variable names identical to names of variables imported by register_global s. (Meaning: don't use the name $find when you have a $_POST or $_SESSION element named find).

            Did you try reversing the variables on line #4 in the search2.php code, like I suggested?

            Comment

            • ArizonaJohn
              New Member
              • May 2009
              • 21

              #7
              Hmm. I use $find elsewhere as both a session and local variable and it works just perfectly.

              I tried reversing $_SESSION['find'] = $find; to $find = $_SESSION['find']; and it doesn't seem to make a difference.

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Originally posted by ArizonaJohn
                Hmm. I use $find elsewhere as both a session and local variable and it works just perfectly.
                It'll probably work fine 99% of the time. It's that 1% I'm worried about.

                It's generally just not a good idea to mix up safe and unsafe data, nor to override global variables that your code may rely on further down the line.

                And, by the way, relying on the register_global s directive is a horrible idea.
                Even the PHP manual points that out ;)

                In any case, it will be remved in PHP6 so it won't be a problem much longer.

                Originally posted by ArizonaJohn
                I tried reversing $_SESSION['find'] = $find; to $find = $_SESSION['find']; and it doesn't seem to make a difference.
                Yep, seeing as register_global s is on, they are both the same variable.
                You can remove the line, it's completely pointless. Kind of like doing "$find = $find;".

                Besides this, I can't see anything in the code you posted that would account for this.
                You appear to have removed some code from the search.php file. Could the problem be in the code you didn't post?

                Comment

                Working...