Connecting Tech Pros Worldwide Forums | Help | Site Map

Session works, just not the first time

Newbie
 
Join Date: May 2009
Posts: 21
#1: Jun 4 '09
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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. unset($_SESSION['find']);    
  4. ?>
  5.  
  6.  <form action="search.php" method="post">
  7.   <label>Enter Topic:
  8.   <input type="text" name="find" size="55"/>
  9.   <input type="hidden" name="searching" value="yes" />
  10.   <input type="submit" name="search" value="Search" />
  11.   </label>
  12.   </form>
  13.  
Then search.php has this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ob_start();
  3. session_start();
  4. unset($_SESSION['find']);    
  5. $find = strip_tags($_POST['find']);
  6. $find = trim ($find);
  7. $find = strtolower($find);
  8. $_SESSION['find'] = $find;
  9. ?>
  10.  
  11. $anymatches=mysql_num_rows($result);
  12. if ($anymatches == 0)
  13. {
  14.  
  15. $_SESSION['find'] = $find;
  16. header("Location:search2.php");
  17.  exit;
  18.  
  19. }
  20.  
Then search2.php has this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. session_start();    
  4. $_SESSION['find'] = $find;
  5. ?>
  6.  
  7. print "<p class=\"topic2\">The topic \"$find\" has not been added.</p>\n";
  8.  
  9. print "<p class=\"topic2\">Add the topic \"$find\".</p>\n";
  10.  

prabirchoudhury's Avatar
Familiar Sight
 
Join Date: May 2009
Location: Wellington, New Zealand
Posts: 152
#2: Jun 4 '09

re: 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

Expand|Select|Wrap|Line Numbers
  1. should be 
  2.  
  3. if ($_SESSION['find']){
  4.  
  5. unset($_SESSION['find']); 
  6.  
  7. }
  8.  
  9.  
2. same problem in search.php page havent set any session but directly calling for unset

Expand|Select|Wrap|Line Numbers
  1. should be 
  2.  
  3. if ($_SESSION['find']){
  4.  
  5. unset($_SESSION['find']); 
  6.  
  7. }
  8.  
  9. then define session with new value
  10.  
:)
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#3: Jun 4 '09

re: Session works, just not the first time


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_globals' directive enabled?
That would explain a lot.

You can check it out by doing:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if(ini_get('register_globals')) {
  3.     echo "register_globals is On";
  4. } else {
  5.     echo "register_globals is Off";
  6. }
  7. ?>
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#4: Jun 4 '09

re: Session works, just not the first time


Quote:

Originally Posted by prabirchoudhury View Post

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.
Newbie
 
Join Date: May 2009
Posts: 21
#5: Jun 4 '09

re: Session works, just not the first time


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.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#6: Jun 4 '09

re: Session works, just not the first time


Ok.

Since register_globals 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_globals. (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?
Newbie
 
Join Date: May 2009
Posts: 21
#7: Jun 5 '09

re: Session works, just not the first time


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.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#8: Jun 5 '09

re: Session works, just not the first time


Quote:

Originally Posted by ArizonaJohn View Post

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_globals 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.

Quote:

Originally Posted by ArizonaJohn View Post

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

Yep, seeing as register_globals 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?
Reply