-2

I have some trouble understanding why setcookie() doesn't work on several pages.

if (isset($_POST['group'])){ 
    $group = stripslashes($_POST['group']);
    setcookie(GSgroup, $group, time()+3600);
}

I use the above code to set a cookie when a form is posted with several options. I use this on an included page so that all pages which use this function will set the cookie to the right group.

Now, this worked perfectly fine when I tried it on localhost, but after uploading it to a live website it doesn't work anymore. The strange thing is, that after I used it on another included page it did work.

Does anyone have any clue how this could be? I am really confused.

If you need more information please ask.

hakre
  • 193,403
  • 52
  • 435
  • 836
JeroenN
  • 1
  • 1
  • Is `GSgroup` a defined constant? Or is it a `string`? If it's a `string` you have to put quotes around it. – Matt Aug 10 '12 at 13:15
  • "It does not work" - great explanation of your problem, I'm contacting Charles Xavier to help me figure out what "does not work" means for you. – hakre Aug 30 '12 at 07:53

3 Answers3

0
if(isset($_POST['group'])){ 
$group = stripslashes($_POST['group']);
setcookie(GSgroup, $group, time()+3600);
}

Is that the actual code? because the first parameter in the setcookie function should be a string or a variable containing a string.

Makita
  • 1,812
  • 12
  • 15
0
setcookie('GSgroup', $group, time()+3600);
Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
0

Bear in mind that cookies are loaded when the page loads, so if you set a cookie on line 5 in a PHP script with setcookie then accessing it with $_COOKIE on line 10 will not give you the cookie value.

You would need to reload the page to access the cookie value in this way.

I like this PHP cookie class that has been made available.

crmpicco
  • 16,605
  • 26
  • 134
  • 210