2013-10-30 65 views
1

我正在使用flashdata存储我的搜索查询,以便在用户导航到下一页时可以检索它。Flashdata在托管在服务器上但在本地工作时无法在Chrome上工作

这是我如何保存flashdata

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

这是我如何检索flashdata。

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

Flashdata在本地工作正常,但是当我主持它在我的服务器上,它无法在Chrome(Windows)和Chrome浏览器(Android版)的工作,但在IE(Windows)中。此外,它在Chrome(iOS)上运行得非常好。我也做了一个测试用例,它只适用于IE(Windows)和Chrome(iOS)。任何人都知道什么是错的?

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"; 
     } 
    } 
} 

这里是我的.htaccess文件

# 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] 
+0

codeigniters整个会话停止工作,或只是flashdata?如果它只是flash数据,那么可能会有一个额外的请求被触发,即清除flash数据。即使Chrome浏览器不在您的HTML中,Chrome等浏览器也会尝试请求收藏图标。如果你有一个捕获所有.htaccess这个请求可以路由到你的应用程序,这将清除flashdata。 – Jeemusu

+0

你能发布你的.htaccess吗? – Jeemusu

+0

我已将hta​​ccess文件添加到上述问题中。谢谢@jeemusu – Xuanhao

回答

3

问题是由最后RewriteCond.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) 

最有可能导致上述用于列出请求该不该”不会被路由到您的应用程序index.php。

可能发生的情况是您的网站包含一个您不希望路由到您的应用程序的图标/ css/image/etc的请求。当这个请求通过你的应用程序时,它会拦截你的flash数据(它只能用于下一个请求),这意味着你的页面的实际请求变得不可用。

这样的例子通常是favicon.ico,虽然还有其他情况。我认为即使没有在<head>中指定,也会请求收藏图标。

你可以通过添加favicon.ico到排除要求清单解决这个问题:

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

这是可能的,这不是由favicon.ico的引起的,但对于另一些要求完全不同。最好的方法是将所有请求记录到文件中,并检查代码中当时正在发生的事情。

+0

感谢您的快速和有益的答复,它解决了问题,真的很感激它。 – Xuanhao

+0

太好了!请留意,因为您很可能会遇到更多问题,需要阻止某些资源通过应用程序。 – Jeemusu

相关问题