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); ?>
1

2 Answers 2

8

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

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

1
  • Be aware that this will send the cookie to all your subdomains, which might not be desired behaviour.
    – Duncan
    Commented Mar 22, 2010 at 2:34
6

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]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.