actionman
10-05-2005, 03:46 AM
hi,
Anyone knows the proper way of using SetCookie() ?
I keep on keeping Header output errors.
No idea how to solve!
I know what it should be placed before any html tags,etc but if i do that the code is not being executed. Please help.
Examples would be good
thanks
PlanetWebHost
10-05-2005, 06:40 AM
<?
setcookie("var", "value", time()+2592000, "/");
echo '<html><body>';
echo 'your cookie is: '.$_COOKIE['var'];
echo '</body></html>';
?>
That code sets a cookie named 'var' with the value 'value' for 30 days,
$_COOKIE['var'] will not register until the next page load.
actionman
10-05-2005, 07:35 AM
I don't think that will work. I am actually writing a script that remember s the last searched item for my search facility.
You have any example on that ? I don't get how the code you gave could be implemented for my search form.
thanks
PlanetWebHost
10-05-2005, 07:43 AM
well.. that was just a simple sample on how to set a cookie.
assumming your sending the keywords with <input type="text" name="keyword"> in the form on the previous page, set the cookie with....
setcookie("keyword", $_POST['keyword'], time()+2592000, "/");
It won't be available on that page load, but from then on, for the next 30 days, you can check the cookie when you display the initial search form.
<input type="text" name="keyword" value="<? echo $_COOKIE['keyword']; ?>">
PlanetWebHost
10-05-2005, 07:51 AM
you could also take it a step further...
<?
if ( $_POST['keyword'] ){
$keyword = $_POST['keyword'];
setcookie("keyword", $keyword, time()+2592000, "/");
}
elseif ( $_COOKIE['keyword'] ){ $keyword = $_COOKIE['keyword']; }
?>
<input type="text" name="keyword" value="<? echo $keyword; ?>">
This method is particulary helpful if you have your search form displaying on every page, including the search page.
actionman
10-05-2005, 08:22 AM
where should i place the setcookie line ?
after the user click on the submit button ?
don't works for me...
maybe i did something wrong..
Do you search a script that's similar ? please attach it here, i would like to have a look
thanks
PlanetWebHost
10-05-2005, 08:27 AM
the setcookie line needs to be placed before any HTML is outputed, otherwise your get header errors.
best place for it is on the very top of your script, before anything else is run.