38

I have a problem similar if not identical to the problem in this thread: Randomly Losing Session Variables Only In Google Chrome & URL Rewriting

But all solutions in that thread don't work for me. I'm getting a strange behavior from only Google Chrome in my PHP/MySQL App. If I try it with Firefox, it works, but Chrome doesn't.

I navigate to some place in my shopping cart and at several places in the code I'll store session data. Don't worry about me starting the session or anything related to that, I've got 11 years in webapp dev, all is done fine.

In all browsers, I can var_dump($_SESSION) and get my data back, but in Chrome it doesn't keep the data. Also note that the session does get passed on, I can look in the network monitor and I see the cookie being sent and many other things related to session work but that one $_SESSION['last_viewed_element'] is not kept. I also can't seem to set anything else, all gets lost.

EDIT:

Problem resolved by switching from SESSIONS TO COOKIES...

Community
  • 1
  • 1
Mathieu Dumoulin
  • 11,584
  • 6
  • 38
  • 65
  • You haven't provided enough information. PHP is browser-agnostic by nature, you're either passing malformed cookies or using an abstraction layer you didn't mention that treats different browsers specially. – Incognito Nov 23 '11 at 19:27
  • I know this is what makes it the most stupid of all this, this session data is not used by anything else than my little mod and the data gets lost only on that browser for the same exact steps reproduced – Mathieu Dumoulin Nov 23 '11 at 19:43
  • I don't think anyone can help you with only the information you've provided. – Incognito Nov 23 '11 at 20:06
  • I've shown this bug to my other senior collegue and my even more senior programming boss and we are all stumped, it works with the same steps in Firefox but not in Chrome. I'm going to try safari on a windows pc to see if the bug could be related to webkit or only chrome. And yes, i know, it shouldnt incluence server side data, but something from chrome is creating that issue... – Mathieu Dumoulin Nov 23 '11 at 20:32
  • 1
    PHP identifies which session belongs to each "visitor" (browser) by storing a session identifier as a cookie in the visiting browser. On each subsequent visit the browser is supposed to send back the cookie in its request headers, where PHP looks up the session data on the server using the session id passed by the browser. It sounds like Chrome is not storing the cookie. Have you tried clearing cookies and making sure Chrome is properly accepting them? – Carl Zulauf Nov 23 '11 at 20:52
  • Yeah, the session id was the same from page to page, nothing out of the ordinary... I fixed my problem using COOKIES instead of SESSIONS, it works everywhere now... Strange, we'll probably never know why it happened, thanks for all the input i could get... – Mathieu Dumoulin Nov 23 '11 at 21:04

20 Answers20

53

I had a very similar problem, in my case the problem was a 404 called due to a missing favicon.ico in Chrome only. The 404.php called the footer which altered the Session Variables. I hope that helps someone.

Ben
  • 1,531
  • 12
  • 14
  • This totally helped me. What a strange bug, even if it was on my side. Really, really hard to debug. Thanks! – Matt James Dec 17 '12 at 16:31
  • Very strange, but that's true... quite impressed!! – Hola Soy Edu Feliz Navidad May 11 '13 at 18:25
  • 1
    Some useful links: http://allinthehead.com/retro/359/a-favicon-broke-my-app Bugs in chrome related favicon: https://code.google.com/p/chromium/issues/detail?id=216633 https://code.google.com/p/chromium/issues/detail?id=114082 https://code.google.com/p/chromium/issues/detail?id=180764 Similar problems in other browsers: https://bugzilla.mozilla.org/show_bug.cgi?id=263057 http://www.webperformance.com/load-testing/blog/2010/11/ie8-favicon-ico-and-silly-server-stunts/ – Marek Gregor Dec 11 '13 at 11:20
  • You are a lifesaver, Ben. If your day was anything like mine when you tried to track down this issue, my sympathy goes out to you. This is really a freak issue... – Morten Jun 21 '15 at 22:28
  • OMG I have been racking my brains for days trying to figure this out. As much as I love Chrome it's buggy as hell sometimes. Its a love/hate relationship. – Cesar Bielich Jul 11 '15 at 07:13
  • Thank you, Ben. You totally helped me. I had the same problem. The failed 404 call doesn't show up in Chrome's network trace window. They really should do something about that. – Frog Pr1nce May 05 '17 at 12:17
12

The issue could be your server is looking for favicons, if it is not found the server throws out a 302 redirect, which kills the session variables.

user1665622
  • 171
  • 1
  • 5
  • 1
    I understand that you don't have the rep at the moment to post this as a comment, but in the future, this might be best served as a comment. Answers generally should be something that you are pretty certain of that will solve the problem. – Fluffeh Sep 27 '12 at 10:30
  • 8
    302 kills session variables? why? – Flash Thunder Sep 09 '13 at 15:24
  • 1
    This is the best answer I guess. And I hate chrome today. because it takes 5 hours to figure out what the issue was. Thanks man for this answer. – Just code Jul 25 '15 at 09:58
4

I had this issue and was able to fix it. Chrome keeps looking for a .ico file and for some reason it was affecting it. Once I placed the .ico file in the root of the site everything started working. Crazy but true.

Camilo
  • 41
  • 1
3

I faced same problem, but on IIS with ASP.Net MVC. IE and Firefox were doing fine, but on Chrome I was losing session data. Eventually found out that a 404 error was clearing a cookie in Chrome. Below are the steps I followed to find the problem and resolve. I suggest others to try:

  1. On Chrome, Use Tools -> Developer Tools. Refresh the page so "Developer Tools" starts showing data.

  2. On Developer tools, Check Resources -> Cookies. Right after a successful log in, I had 2 cookies for the domain I was testing. On navigating to the page where I lost session, one of the cookies did not show up anymore. The screenshot was taken after the fix, showing both cookies: enter image description here

  3. Now check Network tab. Look carefully for any resource (html/image/css/js/...) which has any error. I had a 404 error for a font file. The 404 error was caused by missing mime type in IIS. fixing the 404 error cleared the problem in Chrome. The screenshot, again taken after fix, had all resources with OK status: enter image description here

The bonus was, investigating this problem helped me find out missing mime type in IIS, which was affecting more pages on all browsers.

Kash
  • 1,118
  • 9
  • 10
2

Had same problem and finally solved. Login set session with domain.com but in the redirect it was www.domain.com. IE and FF seem to assume www and no www are same but Chrome doesn't. Found by checking Host in network log for each page load.

2

Just try this before wasting your time

If you are already logged in your webspace ( control panel / Cpanel / Plesk Panel ) in the same browser. Then logout from that control panel and clear the cookies and try Again

In case of

session data lost in chrome only

In my case I just reset chrome browser

Go to chrome://settings/ then click advanced then reset

enter image description here

sijo vijayan
  • 1,280
  • 16
  • 31
  • 1
    Worked for me, thanks! Had the problem since a few days and it only occurred on localhost somehow. Incognito mode worked fine somehow. Before finding your comment I tried a lot with deleting cookies, cache, deactivating addons and so forth. Nothing helped except your recommendation with chrome reset. Since your comment is also pretty recent, I guess it is some new chrome bug. – Albrecht Striffler Jul 07 '17 at 13:08
  • I really glad to hear that, I lost a day due to this problem :( – sijo vijayan Jul 08 '17 at 13:52
1

I solved the problem by removing the line:

base href="http://mysite/"

from the head tag in the HTML code.

SchmitzIT
  • 8,307
  • 9
  • 53
  • 82
1

Looking on the following link: http://code.google.com/p/chromium/issues/detail?id=45582

I belive the issue is with PHP getting the request that did not match a file and then not properly handling 404's correctly. I had to tell Nginx to match any URL with favicon.ico and then return a 404.

Here is my line for Nginx:

 if ($request_uri ~ 'favicon') {
            return 404;
 }
John Conde
  • 207,509
  • 96
  • 428
  • 469
kornstar11
  • 104
  • 1
  • 7
1

HA! i finally solved it!

When doing a header() redirect in PHP, you must do a die() right after it. THAT only solves it for all browsers except for Chrome.

For Chrome you also gotta do a session_write_close() right before the header()

Sweeeeeeeeet success

Matt
  • 258
  • 3
  • 12
1

The code I was working with had the same issue. Solved by removing the following:

session_id($_GET['sid']);
session_write_close();
Yi Jiang
  • 46,385
  • 16
  • 133
  • 131
rota
  • 11
  • 1
0

This solved my problem instantly: go to chrome://settings/cookies then locate your localhost then remove its cookies. The solution is so simple, it's worth a try

Louie Almeda
  • 4,576
  • 24
  • 32
0

We had the same issue yesterday the whole day.

What (seemingly) solved it (for us) was a chrome update.

We had Version 45.0.2454.93 und since the update to version 45.0.2454.99 the problem didn't occure again ...

Benjamin
  • 965
  • 19
  • 27
0

I had similar problem, I discovered the reason which was very strange. In my case one image url inside a css class was wrong!! The browser coudn't load the image and because the page was a sign up form with password field, the browser reset the session for security reasons.

DeepBlue
  • 644
  • 7
  • 20
0

I am not sure if your case is similar to mine. But for me the reason was the formation of URL.

With chrome, when typing the URL as "http://www.domainname.com" and setting the session variables there.

and redirecting with "http://domainname.com" without the WWW. the sessionid is not reused.

This resolved my issue hope this is help

DhanushD
  • 36
  • 5
0

You are probably loosing sessions only on your development environment, and it may be most probably because of 'same origin policy' of Chrome. If so, then this is your solution Disable same origin policy in Chrome

abbas
  • 4,704
  • 2
  • 31
  • 30
0

I found my version of the issue was exactly as described here and here and so I want to add a further qualifier to the above that

Google Chrome (v.59, stable) Inspector does not tell you that the favicon.ico is inaccessible and does not tell you the page is 302 redirected.

Martin
  • 19,815
  • 6
  • 53
  • 104
0

I my case it was just ini_set('session.cookie_secure','1');. I was checking site locally on XAMPP. On FF no problem but when accessing chrome it just kept loosing session.

Mortimer
  • 164
  • 1
  • 11
0

In your php ini file try setting

session.save_path = /path/to/your/tmp

On some servers, sometimes the session needs an explicitly direct session file to save in a local directory or otherwise some weirdness happens.

kylex
  • 13,335
  • 30
  • 103
  • 169
-3

After all, no answer, problem still exists, i just made a switch to using cookies instead, if anyone ever gets that problem with chrome+wordpress at the same time, dont lose more time, switch to cookies...

Mathieu Dumoulin
  • 11,584
  • 6
  • 38
  • 65
-4

I found a "solution" (is not a problem but only effects!!) ... if in your page use ajax, ajax is asyncronus ... If I call a function that working on SESSION than i call another page that working on session, some times the first call is not finish before second start and effect of the first overwrite response of the second. I resolve problem with async:false in every ajax call.

Ext.Ajax.request({
    url: '/io/resetsession.php', 
    **async: false**
});
Ext.Ajax.request({
    url: '/io/loaddata.php', 
    **async: false**,
    .....
});
Rizier123
  • 56,111
  • 16
  • 85
  • 130
AT404
  • 1