开发者

Why are cookies only sent to http://www.example.com and NOT http://example.com?

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 domai开发者_开发技巧n whatever there is www. or not?

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


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

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


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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜