php CodeIgniter“flashdata”不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8307705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter "flashdata" doesn't work
提问by Kate Wintz
I use CodeIgniter 2.1.0, i want after insert data in database get a message like "Your information was successfully updated.". For this work i have in CI_Controller following function:
我使用 CodeIgniter 2.1.0,我想在数据库中插入数据后收到一条消息,如“您的信息已成功更新。”。对于这项工作,我在 CI_Controller 中有以下功能:
function myCiInser(){
... Here is my query ...
//$data: this var is result query that is true
if($data){
$this -> session -> set_flashdata('message', 'Your information was successfully updated.');
redirect('url/myurl');
}
}
And i have in view as:
我认为:
<?php
$message = $this->session->flashdata('message');
if($message){
echo '<div id="error_text">' . $message . '</div>';
}
//I test this : "echo $message;" but don't give output
?>
But i don't give message in view but redirect
is done and work true. and in database in table ci_sessions
column user_data
i have this:
但是我没有在视图中给出消息,但是redirect
已经完成并且工作正常。在表ci_sessions
列的数据库中,user_data
我有这个:
a:2:{s:9:"user_data";s:0:"";s:19:"flash:new:message";s:42:"Your information was successfully updated.";}
a:2:{s:9:"user_data";s:0:"";s:19:"flash:new:message";s:42:"您的信息已成功更新。";}
How can fix this problem?
如何解决这个问题?
UPDATE:
更新:
I had the following error (i use from chorme and by Ctrl+Shift+j i get this alert):
我有以下错误(我从 chorme 使用并通过 Ctrl+Shift+ji 获取此警报):
Failed to load resource: the server responded with a status of 404 (Not Found)
加载资源失败:服务器响应状态为 404(未找到)
And i fix it (Now i do not have the error) but still is same problem in display message. what do i do?
我修复了它(现在我没有错误)但在显示消息中仍然是同样的问题。我该怎么办?
回答by Quetzy Garcia
From the Codeigniter Session Class documentation, regarding Flashdatawe can read:
从Codeigniter Session Class 文档中,关于Flashdata,我们可以阅读:
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.
CodeIgniter 支持“flashdata”,或仅可用于下一个服务器请求的会话数据,然后会自动清除。
Your problem might be that when you redirect, the process takes more than one request, clearing your flashdata.
您的问题可能是当您重定向时,该过程需要多个请求,清除您的 flashdata。
To see if that's the case, just add the following code to the constructor of the controller you are redirecting to:
要查看是否是这种情况,只需将以下代码添加到您要重定向到的控制器的构造函数中:
$this->session->keep_flashdata('message');
This will keep the flashdata for another server request, allowing it to be used afterwards.
这将保留另一个服务器请求的 flashdata,允许它在以后使用。
回答by Vinicius
I had that problem too. I don't remember where I saw but here's my solution.
我也有这个问题。我不记得我在哪里看到的,但这是我的解决方案。
redirect('url/myurl','refresh');
CodeIgniter didn't treated redirect as another request. So flashdata wasn't set in the redirect, but it was on the next page I loaded.
CodeIgniter 没有将重定向视为另一个请求。所以 flashdata 没有在重定向中设置,但它在我加载的下一页上。
回答by Sujeet Kumar
// Set flash data in our controller file
// 在我们的控制器文件中设置闪存数据
$this->session->set_flashdata('sessionkey', 'Value');
// After that we need to used redirect function
// 之后我们需要使用重定向功能
redirect("admin/signup");
// Get Flash data on view
// 获取视图中的 Flash 数据
$this->session->flashdata('sessionkey');
回答by Abdulqader Kapadia
Using sessions with database has caused me issues at times. I recommend setting $config['sess_use_database'] = FALSE;
in the config.phpand see if the flashdata works fine.
使用带有数据库的会话有时会给我带来问题。我建议$config['sess_use_database'] = FALSE;
在config.php 中设置并查看 flashdata 是否工作正常。
回答by Tihomir Mihaylov
You can also use database for the sessions, but you have to set the config items:
您也可以将数据库用于会话,但您必须设置配置项:
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = FALSE;
In that way the session flashdata will work again
这样会话 flashdata 将再次工作
回答by Petr
I had the same problem. After checking the code I have found, that I am calling $this->session->sess_destroy();
, which causes the problem.
我有同样的问题。检查我发现的代码后,我正在调用$this->session->sess_destroy();
,这会导致问题。
回答by Alok Babu
Except one page, I am able to display/pass values using session. I tried using the var_dump($this->session)
and I get:
除了一页,我可以使用会话显示/传递值。我尝试使用var_dump($this->session)
,我得到:
["flash:old:Array"]=> bool(false)
["flash:new:message"]=> string(10) "My Message"
I have tried echoing the flash data within the page without redirecting just after setting the data, but the result was same. I recommend to trim down the code, and try to set session in other pages. If the problem persists check your var_dump
. This might not be the solution, but I think it can help.
我试过在页面内回显 flash 数据而不在设置数据后立即重定向,但结果是一样的。我建议修剪代码,并尝试在其他页面中设置会话。如果问题仍然存在,请检查您的var_dump
. 这可能不是解决方案,但我认为它可以提供帮助。
UPDATE: trimming down spaces and newlines within the text worked. I was passing 2 long sentences with empty line breaks and spaces.
更新:修剪文本中的空格和换行符。我传递了 2 个带有空换行符和空格的长句。
if (0) //Assume this condition is false
{
$this->load->view('error_page');
// Generate validation error
}
else
{
//Show success message
$data = array(
'message' => 'My message'
);
$this->session->set_flashdata($data);
$this->session->keep_flashdata($data);
echo $this->session->flashdata('message');
//echo var_dump($this->session);
//redirect(base_url().'success_page');
}
回答by dpkrai96
As I am observing about codeigniter flashdata. When I use it in second request using codeigniter redirect() method it is working fine in mozila but in the case of chrome it is not working.
正如我正在观察有关 codeigniter flashdata。当我使用 codeigniter redirect() 方法在第二个请求中使用它时,它在 mozila 中工作正常,但在 chrome 的情况下它不起作用。
回答by Rishabh Malhotra
I know I am very late but I was having this problem and I couldn't believe that in my case the solution was very easy
我知道我迟到了,但我遇到了这个问题,我无法相信在我的情况下解决方案非常简单
just replace
只需更换
$this->session->flashdata('message');
to
到
print_r($this->session->flashdata('message'));
回答by qwertzman
I had Chrome developer console open and flashdata was removed. After closing it and retrying it works. Version 71.0.3578.98 (Official Build) (64-bit)
我打开了 Chrome 开发者控制台并删除了 flashdata。关闭它并重试后它可以工作。版本 71.0.3578.98(官方版本)(64 位)