我正在CodeIgniter 3中建立博客。我想在用户通过身份验证但redirect()无法正常工作时从一个控制器重定向到另一个控制器。
我已经尝试了Stack Overflow中给出的所有解决方案,但是没有人适合我。检查我的代码,并告诉我问题出在哪里。提前致谢。
MY_Controller是我从扩展的核心控制器CI_Controller。
class Login_c extends MY_controller {
public function index() {
$this->load->helper('form');
$this->load->view('public/admin_login_v');
}
public function admin_login() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'User name', 'required|trim|alpha');
$this->form_validation->set_rules('password', 'Password' , 'required');
if($this->form_validation->run()) {
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('login_model');
$login_id = $this->login_model->login_valid($username,$password);
if( $login_id ){
$this->session->set_userdata('user_id', $login_id);
return redirect('admin_c/dashboard');
} else {
echo "user not authenticated";
}
} else {
$this->load->view('public/admin_login_v');
// echo validation_errors();
}
}
}
Run Code Online (Sandbox Code Playgroud)
<?php
// ob_start();
class Admin_c extends MY_Controller {
public function dashboard() {
$this->load->view('public/admin_dashboard');
}
}
Run Code Online (Sandbox Code Playgroud)
$config['base_url'] = "http://localhost/ci_blog";
Run Code Online (Sandbox Code Playgroud)
HTTP 500内部服务器错误
小智 0
这可能有很多原因。首先,您可以使用显示错误来诊断问题
如果你掉落
error_reporting(E_ALL);
ini_set("display_errors", 1);
Run Code Online (Sandbox Code Playgroud)
在文档的顶部,然后在浏览器中运行它,这可能会返回它不工作的原因。
至于redirect()函数本身,你有没有看过它需要哪些参数?我喜欢使用名为 PHPStorm 的软件,你可以免费试用它,在定义函数时它会告诉你函数需要哪些参数以及参数的顺序。很有帮助。
如果这些都没有帮助,我喜欢使用重定向功能,您可以将其放入文档中并回调到您喜欢的任何地方
$site_prefix = 'http'; //This could equal http or https depending on your ssl
$site_url = $_SERVER['HTTP_HOST']; //You could replace $_SERVER['HTTP_HOST'] with your actual hostname like 'example.com'
function redirect_to($page){
global $site_prefix;
global $site_url;
if($page == 'home'){
header("Location: {$site_prefix}://{$site_url}/");
}
elseif($page == ''){
header("Location: {$site_prefix}://{$site_url}");
}
else{
header("Location: {$site_prefix}://{$site_url}/{$page}");
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样调用这个函数
redirect_to('admin/dashboard.php');
Run Code Online (Sandbox Code Playgroud)
或者如果你使用漂亮的网址
redirect_to('admin/dashboard');
Run Code Online (Sandbox Code Playgroud)
我喜欢使用全局变量而不是site_url在函数中使用变量,因为我倾向于在整个网站中使用它们。
如果最坏的情况发生,您始终可以使用以下命令运行 PHP 重定向
header("Location: http://example.com/mypage.php");
Run Code Online (Sandbox Code Playgroud)
我知道这很啰嗦,但我希望它有帮助