Xuanhao
Xuanhao

Reputation: 106

Flashdata not working on Chrome when hosted on server but working locally

I am using flashdata to store my search query so I can retrieve it when users navigate to the next page.

This is how I store the flashdata

$this->session->set_flashdata('searchQuery', $queryStr);

This is how I retrieve the flashdata.

$queryStr = $this->session->flashdata('searchQuery');

Flashdata is working fine locally but when I host it on my server, it does not work on Chrome(Windows) and Chrome(Android) but works on IE(Windows). Also it works perfectly fine on Chrome(iOS). I've also did a test case and it only works on IE(Windows) and Chrome(iOS). Anyone know what's wrong?

class Flashdata extends MY_Controller {
    function __construct()
    {
        parent::__construct();

        $this->load->library('session');
    }

    function index()
    {
        if ($var = $this->session->flashdata('test'))
        {
            echo "Found data: $var";
        }
        else
        {
            $this->session->set_flashdata('test', 'flash data test');
            echo "Set data";
        }
    }
}

Here is my .htaccess file

# Options
Options -Multiviews
Options +FollowSymLinks

#Enable mod rewrite
RewriteEngine On
#the location of the root of your site
#if writing for subdirectories, you would enter /subdirectory
RewriteBase /

#Removes access to CodeIgniter system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#This last condition enables access to the images and css
#folders, and the robots.txt file
RewriteCond $1 !^(index\.php|images|robots\.txt|css)

RewriteRule ^(.*)$ index.php?/$1 [L]   

Upvotes: 1

Views: 795

Answers (1)

Jeemusu
Jeemusu

Reputation: 10533

The problem is most likely caused by the last RewriteCond in your .htaccess

#This last condition enables access to the images and css
#folders, and the robots.txt file
RewriteCond $1 !^(index\.php|images|robots\.txt|css)

The above is used to list requests that shouldn't be routed to your applications index.php.

What is likely occurring is that your site includes a request for an icon/css/image/etc that you don't want routed to your application. As this request is passing through your application it is intercepting your flashdata (which is only available for the next request), which means it is becomes unavailable to your actual request for the page.

An example of this is often a favicon.ico, although there are other cases. I think Chrome will request a favicon even if one isn't specified in the <head>.

You could solve this by adding the favicon.ico to your list of excluded requests:

RewriteCond $1 !^(favicon\.ico|index\.php|images|robots\.txt|css)

It's possible that this is not caused by a favicon.ico, but another request for something completely different. The best way to get to the bottom of this is to log all your requests to a file and check what is happening at that point in the code.

Upvotes: 3

Related Questions