开发者

PHP session login different for url with www and without www?

I am debugging on session login, when i am login to w开发者_运维百科ww.domainname.com the session will be only set for with www.

When i am going to url domain.com, session login is ignored, and i will be prompted to login form again.

I have set session cookie_domain also, but not working.

Any one can help me? why?


I know this question is as old as "Who came first? The egg or the chicken?"

I had a lot of trouble about this issue. But... if you're using php I've found a solution:

In your login script insert in the first line

<?php
session_set_cookie_params(0, '/', 'www.yourdomain.com');
session_start()
?>

or you can try yoursubdomain.yourdomain.com instead www.yourdomain.com.

It works for me. Hope it help other users too.


I highly recommend redirecting users to the canonical version of the site (probably the www.example.com domain). Look around, you'll notice that most sites do exactly this for consistency (including SO; see what happens when you go to www.stackoverflow.com).


Have you tried setting the session domain to ".example.com"?:

$lifetime = 0;
$path = '/';
$domain = '.yourdomain.com';
session_set_cookie_params($lifetime, $path, $domain);

Note the dot in the beginning of the $domain string variable.

Check the reference of the function here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜