General Question

gagara's avatar

How to make cookie in php track both in www.site.com and site.com for some web-site?

Asked by gagara (102points) May 11th, 2010

I am making website in php where I track users via cookies. For brevity reasons users are normally directed to http://site.com (not the actual site, just example), but I cannot exclude users navigating also from http://www.site.com. I am having trouble creating cookies that can be tracked both from site.com and www.site.com in PHP, as PHP creates cookie in either domain, and it is not visible in the other domain. What should I do?

Observing members: 0 Composing members: 0

4 Answers

MrItty's avatar

Set the domain argument to just ’.site.com’, including the leading period, rather than accepting the default (which will be whatever the current domain is)

http://php.net/manual/en/function.setcookie.php shows the function accepts domain as the 5th argument. You have to set it explicitly.

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

So you might have something like:

setcookie(‘my_cookie’, 123, 0, ””, ’.site.com’);

gagara's avatar

Thanks, that helps. Although I tried it before and it didn’t seem to work, but maybe I did something wrong at that time.

simplicity's avatar

Create a 301 redirect that redirects anyone from the www version to the non www version. Search engine friendly and makes sure everyone is in the same location.

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther