我正在驗證登錄表單並使用flashdata發送錯誤消息。 Flashdata似乎有延遲,它隻影響第二次提交(第一次提交後沒有顯示錯誤消息,第二次提交後沒有錯誤),而userdata正常工作。CodeIgniter flashdata延遲
查看>
<?php if ($this->session->flashdata('error_messages')): ?>
<?php echo $this->session->flashdata('error_messages'); ?>
<?php endif; ?>
<?php echo form_open('user/login', array('name' => 'login_form', 'id' => 'login_form')); ?>
<fieldset>
<p>
<?php echo form_label('Username: ', 'username'); ?>
<?php echo form_input('username', set_value('username', ''), array('id' => 'username', 'placeholder' => 'username')); ?>
</p>
<p>
<?php echo form_label('Password: ', 'password'); ?>
<?php echo form_password('password', set_value('password', ''), array('id' => 'password', 'placeholder' => '********')); ?>
</p>
<p>
<?php echo form_submit('login', 'Login', array('id' => 'login')); ?>
</p>
控制器動作>
public function login() {
//user is not logged in, carry on
if (!user_is_logged_in()) {
//check if form was already submitted, if yes validate
if ($this->input->post('login')) {
$this->load->library('chronos_user');
//gather data from form
$username = $this->input->post('username');
$password = $this->input->post('password');
//validate form, show error messages if not valid
if (
!($this->chronos_user->valid_username($username)) ||
!($this->chronos_user->unique_username($username))
)
{
$this->chronos_template->set_data('header', array('controller' => 'users', 'action' => 'login', 'title' => 'Login'));
$this->chronos_template->set_view('content', 'user/login_view');
$this->chronos_template->view();
}
//redirect if ok
else {
header("Location http://c4studio.ro");
}
}
//if no form was submitted, just display it
else {
$this->chronos_template->set_data('header', array('controller' => 'users', 'action' => 'login', 'title' => 'Login'));
$this->chronos_template->set_view('content', 'user/login_view');
$this->chronos_template->view();
}
}
//user is already logged in, redirect to profile page
else {
$profile_url = "Location: ";
$profile_url .= site_url();
$profile_url .= "user/profile/";
header($profile_url);
}
}
Valid_username法>
public function valid_username($username) {
if (empty($username)) {
$this->CI->session->set_flashdata('error_messages', 'Value.');
return FALSE;
}
if (strlen($username) < 3) {
$this->CI->session->set_flashdata('error_messages', 'Value11.');
return FALSE;
}
return TRUE;
}
任何想法?另外如果有人知道更好的方法來做到這一點,請讓我知道!謝謝