我在我的登录表单中出现codeigniters flashdata这个奇怪的问题。当我提交包含错误的表单时(无法识别的电子邮件或错误的电子邮件密码组合),需要两次提交错误消息才能显示。下面是相关的代码:flashdata错误不显示
//If an email address is matched
if($rowcount === 1) {
$row = $query->row();
if (hash('sha1', $row->salt . $_POST['password']) === $row->password) {
//there's a matching user...create a session here and redirect to homepage
} else {
$this->session->set_flashdata('credentials_error', '1');
// echo 'recognise email but not password';
}
} else {
//send message back to view here
$this->session->set_flashdata('email_error','1');
}
print_r($this->session);
$global_data['page_data'] = $this->load->view('login-template','',true);
$this->load->view('global', $global_data);
,并从视图中的相关位:
if($this->session->flashdata('email_error')) {
echo '<p class="error">We dont recognise this email address.</p>';
}
if($this->session->flashdata('credentials_error')) {
echo '<p class="error">We dont recognise these details. Please try again.</p>';
}
所以,如果我提交表单与这是无法识别的,然后我设置email_error闪存数据不好的电子邮件地址。问题是,在视图中,我可以看到在打印出所有会话数据([flash:new:emaili_error] => 1)时设置了flashdata,但我的错误消息未显示。但是,当我再次提交表单(重新发送相同的数据)时,显示错误消息。
任何想法,为什么这可能是?
但我在第一次看到会话数组中设置的相关会话数据。这是预期的吗?那么在这种情况下不使用flashdata会更好吗?我想知道在这种情况下重定向是否是常见做法......因为这看起来确实起作用 – 2012-02-21 20:54:36
如果你需要flashadata,是的,你需要重定向。否则,它们是无用的,因为你也可以直接打印消息。你不觉得吗? – 2012-02-21 21:08:00