View Full Version : $_COOKIE problems. Not storing consistently.

03-17-2009, 08:22 PM
This is only my second time using cookies, so I may be missing something.

I have a form:

<form id="eduPartSearch" name="partSrch" method="post" action="results.php">
<label>Search </label><input type="text" name="qrystr"/>
<label>PartNumber </label><input type="radio" name="type" value="pn" checked="checked" />
<label>Description </label><input type="radio" name="type" value="desc" />
<label>Filter by Type </label><select name="category">
<option value="none"> - - Optional - -</option>
<option value="none">options ...</option>
<button type="submit">Search</button>
<button type="reset">Reset</button>

And here are the pertinent parts of my action page:

if(isset($_POST["type"])){setcookie("type", $_POST["type"], $expire);}
if(isset($_POST["qrystr"])){setcookie("qrystr", mysql_real_escape_string($_POST["qrystr"]), $expire);}
if(isset($_POST["category"])){setcookie("category", $_POST["category"], $expire);}

// echo for testing
echo "search type: ".$_COOKIE["type"]."<br/>";
echo "search term: ".$_COOKIE["qrystr"]."<br/>";
echo "category group: ".$_COOKIE["category"]."<br/><br/>";

The problem is that, sometimes, after submitting a new query from the form page, the values from the previous search are still held in the cookies. After one or two more tries, the cookies will capure the correct data.


note: only tested in FF so far, but it must work at least in FF.

~ Mo

03-17-2009, 08:30 PM
$_COOKIE is not set until the browser makes a page request. You cannot use setcookie() (which sends the cookie to the browser) and a corresponding $_COOKIE (which is set by any cookies the browser sends when it makes a page request) in the the same page request.

03-17-2009, 08:42 PM

So then, should I be using the POST on the first results page, and then just use the COOKIE if the POST !isset?

~ Mo