我正在尝试登录工作。我对验证很满意,但是当它重定向到另一个控制器以显示登录页面的视图时会卡住。我仍然是codeigniter的新手,仍然不确定控制器如何工作。
这是我验证登录用户的控制器:
function index() {
$this->form_validation->set_rules('studentid', 'studentid', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE) {
$this->load->view('v_login');
} else {
//Go to private area
redirect(base_url('c_home'), 'refresh');
}
}
它的功能是验证用户是否在数据库中,但是当用户成功登录时,它不会重定向到此重定向(base_url('c_home'),'refresh'); < / em>它告诉我
Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.
这是应该重定向的c_home.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_home extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('m_login','',TRUE);
$this->load->helper('url');
$this->load->library(array('form_validation','session'));
}
function index() {
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['studentid'] = $session_data['studentid'];
$this->load->view('v_home', $data);
} else {
//If no session, redirect to login page
redirect('c_login', 'refresh');
}
}
function logout() {
//remove all session data
$this->session->unset_userdata('logged_in');
$this->session->sess_destroy();
redirect(base_url('c_login'), 'refresh');
}
}
可以从另一个控制器重定向控制器吗? 在它切换到c_home.php之后
它将显示v_home.php
<!DOCTYPE html>
<head>
<title>Simple Login with CodeIgniter - Private Area</title>
</head>
<body>
<h1>Home</h1>
<h2>Welcome <?php echo $studentid; ?>!</h2>
<a href="c_home/logout">Logout</a>
</body>
</html>
答案 0 :(得分:3)
您不需要重定向中的base_url()。只需使用
redirect('c_home',refresh);
虽然我应该让您了解2件事,但您需要('controller/function')
而不仅仅是('controller')
。并确保您在两个控制器上的__construct函数中加载$this->load->helper('url')
。
虽然仅供将来参考,但我认为这就是你的意思。
redirect(base_url().'c_home',refresh)
答案 1 :(得分:1)
Codeigniter的重定向功能已经嵌入了site_url()。
所以,而不是这个;
redirect(base_url('c_login'), 'refresh');
使用此代码,就像您之前在代码中一样
redirect('c_login', 'refresh');
希望这有帮助