Hailwood
Hailwood

Reputation: 92581

cant set cookie?

I am using

public function setCookie() {
  $value = $this->info('id').':'.$this->info('salt');
  //$value == '1:0842d579c6c9f08401d7204240d06930'
  $expire = (time()+((3600*24)*365));
  return setcookie("remember", $value, $expire);
}

and it is returning true,

but when i print out $_COOKIE on the next page the only cookie set is the php sess id.

Why is it not setting?


Notes
The page that sets the cookie is requested through ajax.
The current page/ajax'd page/next page are all on the same domain name
Nothing is sent before the cookie is set

Upvotes: 1

Views: 3043

Answers (1)

Jim
Jim

Reputation: 18853

bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )

From the setcookie man page.

Try setting the domain and path values as well and see if that works, IE:

setcookie("remember", $value, $expire, '.domain.com', '/');

Edit

If you are working localhost here is more information on it:

Source: http://www.aeonity.com/frost/php-setcookie-localhost-apache

setcookie("username", "George", false, "/", false);

You would use that version. I am not entirely sure why that is required for localhost to work. A better method would be to setup a virtual-host in apache and then modify your hosts file to add what you set the ServerName of the vhost to. This will avoid you having to modify your setcookie function to coax it into working, so when you push to staging/production, you don't have to worry about the hack-arounds.

Upvotes: 2

Related Questions