TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Setting Cookie... (http://www.talkphp.com/general/5620-setting-cookie.html)

gillweb 11-03-2010 06:15 AM

Setting Cookie...
 
I have a cookie being set but the page needs to be refreshed for the cookie to be written to the page? I have the set_cookie command in the header of my file (before html tags) but even though I've confirmed with the browser the cookie was set It does not write the cookie to the page. After hitting refresh, it is then written to the screen?

Anyone ever come across this? Is there a fix or some thing i'm doing wrong? I have 3 php files, header. php, results.php and footer.php. The form action is to results.php but that page includes the header.php file so shouldn't the cookie be set during the header.php load and then results.php write the cookie?

:-!

Village Idiot 11-03-2010 04:11 PM

Quote:

Originally Posted by gillweb (Post 31178)
I have a cookie being set but the page needs to be refreshed for the cookie to be written to the page? I have the set_cookie command in the header of my file (before html tags) but even though I've confirmed with the browser the cookie was set It does not write the cookie to the page. After hitting refresh, it is then written to the screen?

Anyone ever come across this? Is there a fix or some thing i'm doing wrong? I have 3 php files, header. php, results.php and footer.php. The form action is to results.php but that page includes the header.php file so shouldn't the cookie be set during the header.php load and then results.php write the cookie?

:-!

I'm not quite sure what you are asking. The cookie is set when the page is sent out, no refresh is needed for it to be there. What you may be doing is this:

PHP Code:

//Current cookie data
//Data:1001
//Lets change this
setcookie('data','2002');
echo 
$_COOKIE['data']; //Will output 1001 

The $_COOKIE variable is set at the beginning of every request with what the cookie data is. Setting a new cookie does not change the $_COOKIE array, although the user's cookie will be changed when the page outputs.

gillweb 11-03-2010 04:20 PM

thats sort wht i was doing. You put me in the right direction thanks. What iv'e done is told my page that if the form is submitted to set the cookie to the redirect to that forms "action" page with the parameters set by the form and this works pretty well.

Quote:

If ( $set_cookie == 'yes' ) {
$area_location = $_GET['area-location'];
$service = $_GET['service'];
setcookie("county", "$area_location");
header( 'Location: results.php?area-location='.$area_location.'&service='.$service.'' ) ;
} else {
}



All times are GMT. The time now is 06:53 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0