我正在使用ion_auth
库。仅当表单中存在验证错误时,此代码才会重定向到登录页面。此页面中没有AJAX,但我已尝试this answer
这是我的代码:
MY_controller.php
class MY_Controller extends CI_Controller {
protected $data = Array();
protected $controller_name;
protected $action_name;
protected $previous_controller_name;
protected $previous_action_name;
protected $save_previous_url = false;
protected $page_title;
public function __construct() {
parent::__construct();
$this->output->set_header('Content-Type: text/html; charset=utf-8');
//save the previous controller and action name from session
$this->previous_controller_name = $this->session->flashdata('previous_controller_name');
$this->previous_action_name = $this->session->flashdata('previous_action_name');
//set the current controller and action name
$this->controller_name = $this->router->fetch_directory() . $this->router->fetch_class();
$this->action_name = $this->router->fetch_method();
if ($this->uri->uri_string() !== 'admin/auth/login' && !$this->ion_auth->logged_in()) {
//redirect them to the login page
redirect('admin/auth/login', 'refresh');
}
elseif ($this->uri->uri_string() !== 'admin/auth/login' && !$this->ion_auth->is_admin()) {
//logout if there is any user who is already logged in
if($this->ion_auth->logged_in())
{
$this->ion_auth->logout();
}
//redirect them to the login page
redirect('admin/auth/login', 'refresh');
}
}
protected function render($view_path) {
//save the controller and action names in session
if ($this->save_previous_url) {
$this->session->set_flashdata('previous_controller_name', $this->previous_controller_name);
$this->session->set_flashdata('previous_action_name', $this->previous_action_name);
}
else {
$this->session->set_flashdata('previous_controller_name', $this->controller_name);
$this->session->set_flashdata('previous_action_name', $this->action_name);
}
$view_path = $view_path. '.php'; //set the path off the view
// echo "<center><h1>".$view_path."</h1></center>";
if (file_exists(APPPATH . 'views/' . $view_path)) {
$this->data['content'] .= $this->load->view($view_path, $this->data, true); //load the view
}
$this->load->view("layouts/main.tpl.php", $this->data); //load the template
}
}
types.php
class Types extends MY_Controller {
public function add()
{
$categoryId = $this->uri->segment(4);
if(isset($categoryId) && !empty($categoryId))
{
$data['categoryId'] = $categoryId;
$this->data['title'] = lang( 'add_type' );
$this->form_validation->set_rules( 'nameAR', lang( 'name_ar' ), 'trim|required|min_length[2]|xss_clean' );
$this->form_validation->set_rules( 'nameEN', lang( 'name_en' ), 'trim|required|min_length[2]|xss_clean' );
if ( $this->form_validation->run() == FALSE ) {
$this->session->set_flashdata( 'msg_class', 'alert alert-danger' );
$this->session->set_flashdata( 'msg', validation_errors() );
//$data['msg_class'] = 'alert alert-danger' ;
//$data['msg'] = validation_errors();
$this->load->vars($data);
$this->render( 'admin/types/add' );
}
else {
$postedArray = $this->input->post( NULL, TRUE );
//check if name already exists
if(count($postedArray) > 0)
{
$this->checkIfTitleExists($postedArray['nameAR'], $postedArray['nameEN'], 'admin/types/add/'.$categoryId, 0);
//insert
$postedArray['cat_id'] = $categoryId;
if ( $this->type_model->insert( $postedArray ) ) {
$this->session->set_flashdata( 'msg_class', 'alert alert-success' );
$this->session->set_flashdata( 'msg', lang( 'insert_success_message' ) );
redirect( 'admin/types/add/'.$categoryId, 'location' );
} else {
$this->session->set_flashdata( 'msg_class', 'alert alert-danger' );
$this->session->set_flashdata( 'msg', lang( 'insert_error' ) );
redirect( 'admin/types/add/'.$categoryId, 'location' );
}
}
}
}
else{
redirect( 'admin/categories/index/3', 'location' ); //show real estate categories
}
}
}
这是错误应该在视图中的位置:
<?php if ( $this->session->flashdata( 'msg' ) ) {?>
<div id="divMessage" class="<?php echo $this->session->flashdata( 'msg_class' );?>" >
<?php echo $this->session->flashdata( 'msg' );?></div>
<?php } ?>
这也是我的会话配置部分:
$config['sess_cookie_name'] = 'cisession';
$config['sess_expiration'] = 604800; // 7 days
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = FALSE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
那么,问题是什么?