Welcome! 登入 註冊
美寶首頁 美寶百科 美寶論壇 美寶落格 美寶地圖

Advanced

A Possible Solve to Cookie Issues in Google Chrome (setcookie)

Posted by Hsinping 
A Possible Solve to Cookie Issues in Google Chrome (setcookie)

分類標籤: PHP
A quick suggestion, if you encountered problems setting cookies in Google Chrome, is:

Change your original setting for subdomains

setcookie('session',      $session_key, $timeout, '/', '.domain.com' );

To

setcookie('session',      $session_key, $timeout, '/');

This could make things easier.

My personal experience is, HOW YOU CREATE A COOKIE IS THE WAY YOU DELETE THE COOKIE.

(If you set the cookie in NULL domain setting, you should use NULL domain to delete it.)

And, use the embedded Chrome tool (F12->Recources->Cookies) to check your cookies. I suggest to check if the cookies are the same in www.domain.com and domain.com (or xxx.domain.com). In the cases I encountered, they were simply DIFFERENT.

--

My scenario is making some changes in the session/login management (for the cross-domain stuff). Originally, I use the same setcookie setting in session restore and session create, like

setcookie('session', $session_key, $timeout, '/', '.domain.com' );

But, I changed the setting in session restore to

setcookie('session', $session_key, $timeout, '/')

Any things go wrong after that.


If my memory and interpretation are right, I guess the cookies set in NULL domain are SUPERIOR than those set in other domain settings. So once you set a cookie in NULL domain, you cannot delete/modify the cookie in other means.

If you set different cookies in .domain.com and domain.com (I main NULL), only the cookie set in NULL domain takes effect in domain.com. The one set in .domain.com (only) work in www.doamin.com.

So in my case, I set cookie in NULL domain in LOGIN (session create) and set in .domain.com in SESSION RESTROE. This (hoplefully) can make cookies work fine in all scenarios.