Randomly Losing Session Variables Only In Google Chrome & URL Rewriting
Using Google Chrome, I'm seemingly losing/corrupting session data when navigating between pages (PHP 5.0.4, Apache 2.0.54). The website works perfectly fine in IE7/8, Firefox, Safari & Opera. The issue is only with Google Chrome.
I narrowed down the problem. I'm using search friendly URL's, and hiding my front controller (index.php) via a .htaccess file. So the URL looks like: www.domain.com/blah/blah/ Here's the .htaccess file contents:
Options +FollowSymlinks
RewriteEngine on
#allow cool urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]
#allow to have Url without index.php
If I remove the .htaccess file, and expose the front controller in the URL: www.domain.com/index.php/blah/blah/, Chrome works perfectly fine.
Any thoughts ideas? I'm thinking it开发者_运维百科's some kind of problem with how Chrome identifies what cookie to use and send to the server? This happens in Chrome 4 & 5. Thanks!
I had the same issue, and to fix it I only had to create a favicon.ico and place it in the webroot - otherwise I could see using Fiddler that a 404 resulted for this with every page request from Chrome (despite me not actually linking to a favicon in the page markup).
In my mind this is clearly a bug in Chrome, as the lack of a favicon should have no bearing on session data.
Turns out the issue was with the contents of my .htaccess file. This resolved the issue:
#<IfModule mod_rewrite.c>
############################################
## enable rewrites
Options +FollowSymlinks
RewriteEngine on
############################################
## always send 404 on missing files in these folders
RewriteCond %{REQUEST_URI} !^/.*(themes|wysiwyg|images|js)/
############################################
## always send 404 on missing favicon
RewriteRule ^favicon.ico$ favicon.ico [L]
############################################
## never rewrite for existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
############################################
## rewrite everything else to index.php
RewriteRule .* index.php
#</IfModule>
Try using;
session_set_cookie_params(0, '/', '.domain.com');
to enforce the session cookie params. Remove the prefixed period if you are enforcing 'no www', or aren't using subdomains.
You can also try calling session_write_close()
at the end of the script to force PHP to write and close the session then and there (this is especially handy when you run redirect headers right after writing session data).
UPDATE:
Try using this in your .htaccess
;
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Seems like this issue has reappeared on Chrome browsers lately. I have had a site running for 6 months with no issues and suddenly yesterday started having issues with session data being overwritten. Using fiddler I could see that chrome was trying to load the favicon. My issue was also that I had a link to "favicon.ico" rather than "/favicon.ico" so chrome then appended favicon.ico to my querystring, eg. /product/abc/favicon.ico which resulted in the page loading twice, the second time overwriting the session data. So my fix was:
- change link ref to "/favicon.ico"
- uploaded a favicon
- added this to .htaccess: RewriteRule ^favicon.ico$ favicon.ico [L]
The last step has been mentioned in several answers and I would consider it a good answer except it failed me because my link ref was "favicon.ico".
精彩评论