tags:

views:

77

answers:

2

I have a PHP login which sets 2 cookies once someone login. The problem is that if you login from http://www.example.com and you go to http://example.com, you will find yourself not logged in. I think that is because the browser only send the cookies to the first syntax.

It is only one domain, the difference is the www. before the domain name, so how to set cookies to the whole domain whatever there is www. or not?

<?php setcookie('username',$username,time()+3600); ?>
+8  A: 

setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain)

Use $domain = '.site.com' instead of 'www.site.com'

Coronatus
Be aware that this will send the cookie to **all** your subdomains, which might not be desired behaviour.
Duncan
+6  A: 

There's really no reason to use both www and non-www domains. Use a 301 redirect to send all to the one of your choice. I prefer non-www because it's shorter. Here's how to do the redirect with htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
Syntax Error