Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Session works, just not the first time

Guest
Jun 03, 2009 Jun 03, 2009

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:

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

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

<?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";

TOPICS
Server side applications
778
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 04, 2009 Jun 04, 2009

In search2.php, the variable $find is undefined.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 04, 2009 Jun 04, 2009

Hi David Powers,

I appreciate the response.  It certainly seems to be true that initially, $find is undefined in search2.php.  However, the second time the user is redirected to search2.php, $find appears to be defined, and defined correctly.

Even so, since I set "$_SESSION['find'] = $find;" and I have "session_start();" at the top of search2.php, I don't understand why $find would ever be undefined for search2.php.

In any event, do you have a suggestion for how I could define $find for search2.php?

Thanks,

John

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 05, 2009 Jun 05, 2009
LATEST

ArizonaJohn wrote:

Even so, since I set "$_SESSION['find'] = $find;" and I have "session_start();" at the top of search2.php, I don't understand why $find would ever be undefined for search2.php.

In search1.php, you are getting the value of $find from the form submission. In search2.php, $find is never defined. It cannot be passed from search1.php. That's the whole idea of storing it as a session variable. Judging from the code you have posted here, $_SESSION['find'] is overwritten by the non-existent $find in search2.php. So, there's either something very strange going on, or you have posted only snippets of code that don't make sense in isolation.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 04, 2009 Jun 04, 2009

Since you're setting the session in search.php and you seem to be trying to use that data in search2.php, shouldn't that be the other way around in search2.php?


<?php
session_start();    
$find = isset($_SESSION['find']) ? $_SESSION['find'] : '';
?>

(I didn't test that isset() for syntax, but you may want to include it there.)

And on some servers, I find that I need to session_write_close() if a header("location") is in the next line. So maybe in search.php,

...
$_SESSION['find'] = $find;
session_write_close();
header("Location:search2.php");
exit;
}

[Edit: Placed session_write_close() BEFORE header()]


--
Mark A. Boyd
Keep-On-Learnin' 🙂
This message was processed and edited by Jive.
It shall not be considered an accurate representation of my words.
It might not even have been intended as a reply to your message.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines