Jump to content

Session works, just not the first time


ArizonaJohn

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/160856-session-works-just-not-the-first-time/
Share on other sites

<?php
session_start();
#unset($_SESSION['find']);   //pointless here
?>

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

 

<?php
#ob_start(); //avoid using ob functions unless you know what their actually for (not lazy script)
session_start();
#unset($_SESSION['find']);   /pointless, you write over it in 4 lines anyway
$find = strip_tags($_POST['find']);
$find = trim ($find);
$find = strtolower($find);
$_SESSION['find'] = $find;
?>
...
<?php
$anymatches=mysql_num_rows($result);
if (!$anymatches)
{

#$_SESSION['find'] = $find; //already set
header("Location:http://www.plufyn.com/search2.php");
exit;
   
}


}

 

<?php
session_start();   
#$_SESSION['find'] = $find; //wrong way round
$find = $_SESSION['find'];
?>

print "<p class=\"topic2\">The topic \"$find\" has not been added.</p>\n";

print "<p class=\"topic2\">Add the topic \"$find\".</p>\n";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.