开发者

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

开发者 https://www.devze.com 2022-12-23 22:10 出处:网络
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

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

精彩评论

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

关注公众号