tags:

views:

39

answers:

3

at index.php i have a form to fill out some settings. the form posts to setsettings.php

at setsettings.php it uses the form data and sets some cookies and redirect back to index.php. if i print_r($_COOKIE) at setsettings.php all is well. but nothing comes up at index.php, the $_COOKIE array is empty :(

Does somebody know how i go about solving this?

I set det setting by (setsettings.php):

/* Write new cookie */
$expire = 60 * 60 * 24 * 60 + time();  // ~2 months
setcookie("user_var_001", $_POST['selection'], $expire);

and in index.php print_r($_COOKIE) turns up blank;

A: 

setcookie() function is called before any code was printed?

Svisstack
A: 

Try printing $_COOKIE['user_var_001']

sea_1987
+2  A: 

Copy-paste from PHP: setcookie comments:

When setting a cookie on a page that redirects, the cookie must be set after the call to header('Location: ....');

Such as:

<?php 
header('Location: http://www.example.com/'); 
setcookie('asite', $site, time()+60*60, '/', 'site.com'); 
?>

I suggest searching that page for "redirect", there is also some problem described about IIS.

P.S. Try without the redirect and see if the cookie is set after you refresh the page.

Indrek
I think my main problem was that the cookie had no domain... after setting one it works like a charm :D
Jason94
follow up: now im having problem with the domain. if i post my settings, i get redirected and it loads the cookie... works fine. but if i load the page, new tab, it cant seem to read the cookie
Jason94
Hard to guess, source example or more details could be helpful. Does it load the cookie in the same tab when refreshing/navigating? Did you set the domain to accept subdomains (for example www) with .example.com? Think the . at the start is important, don't remember 100%.
Indrek
wierd. im using setcookie("user_var_001", $_POST['selection'], $expire, '/', ''); now and it works. Guess IE was a little slow or did not refresh properly or something. It works for me now
Jason94