You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter登录后无法跳转仪表盘,pages.php控制器问题求助

Troubleshooting Dashboard Access After Successful Login

Hey there, let’s dig into why your dashboard isn’t loading even after logging in with valid credentials. Since you suspect pages.php might be the issue, we’ll start there, but we’ll also cover other critical files in your setup—authentication problems often span multiple parts of an app, especially with frameworks like CodeIgniter (I’m guessing that’s what you’re using given the file structure!).

1. First, Audit Your pages.php Dashboard Method

The most obvious spot to check is the dashboard function in your Pages controller. Make sure it’s properly verifying the user’s logged-in status and loading the correct view.

Here’s a common correct implementation to compare against:

public function dashboard() {
    // Check if the user session exists (adjust the key to match your login setup!)
    if (!$this->session->userdata('logged_in')) {
        // If no session, redirect to login (but since you logged in, this might be misfiring)
        redirect('users/login');
    }

    // Optional: Fetch user data to pass to the dashboard view
    $data['user'] = $this->session->userdata();

    // Load the dashboard view
    $this->load->view('dashboard', $data);
}

Key checks here:

  • Are you using the correct session key? If your login sets user_id instead of logged_in, this check will fail even if the user is authenticated.
  • Is the view path correct? A typo like dashboard_view instead of dashboard would cause a blank screen or error.
  • Are there any silent errors? Add var_dump($this->session->userdata()); exit; at the start of the method to confirm the session is being passed through.

2. Verify the Login Logic in Users.php

If the session isn’t being set properly during login, the dashboard will never recognize the user as authenticated. Check your login method:

public function login() {
    // Validate form input (omitted for brevity)
    $this->form_validation->set_rules('username', 'Username', 'required');
    $this->form_validation->set_rules('password', 'Password', 'required');

    if ($this->form_validation->run() == TRUE) {
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        // Check credentials against your database
        $user = $this->user_model->get_user_by_username($username);

        if ($user && password_verify($password, $user->password)) {
            // Set session data (this is critical!)
            $session_data = array(
                'user_id' => $user->id,
                'username' => $user->username,
                'logged_in' => TRUE // Match this key to what you check in pages.php!
            );
            $this->session->set_userdata($session_data);

            // Redirect to dashboard (confirm the route here!)
            redirect('pages/dashboard');
        } else {
            // Invalid credentials error
            $this->session->set_flashdata('error', 'Invalid username or password');
            redirect('users/login');
        }
    } else {
        // Load login view if validation fails
        $this->load->view('login');
    }
}

Key checks:

  • Is $this->session->set_userdata() actually being called after valid credentials? Add a debug echo here to confirm.
  • Does the redirect path match your routes? If you have a custom route for dashboard, make sure you’re using that instead of pages/dashboard.

3. Check MY_Controller.php for Global Authentication Rules

If your Pages controller extends MY_Controller, there might be a global login check that’s blocking access. Here’s a typical example:

class MY_Controller extends CI_Controller {
    public function __construct() {
        parent::__construct();

        // List of pages that don't require login
        $allowed_routes = array('users/login', 'users/register', 'pages/home');

        // Check if current route is not allowed and user isn't logged in
        if (!in_array(uri_string(), $allowed_routes) && !$this->session->userdata('logged_in')) {
            redirect('users/login');
        }
    }
}

Key checks:

  • Is pages/dashboard (or your custom dashboard route) accidentally excluded from the allowed list?
  • Does uri_string() return the correct path? Sometimes URL rewriting can mess this up—test it with echo uri_string(); exit; in your controller.

4. Validate Your routes.php Configuration

A misconfigured route can send your login redirect to the wrong place entirely. Check for entries like:

// Make sure this points to your Pages controller's dashboard method
$route['dashboard'] = 'pages/dashboard';

// If you're using a default controller, ensure it's not overriding the dashboard
$route['default_controller'] = 'pages/home';

Key check: If you’re redirecting to /dashboard after login, confirm that route maps directly to pages/dashboard.

5. Quick Debugging Tips to Pinpoint the Issue

  • Check session data: After logging in, add var_dump($this->session->userdata()); exit; right before the redirect in Users.php to confirm the session is being set.
  • Enable error reporting: In your index.php, set error_reporting(E_ALL); ini_set('display_errors', 1); to catch any silent PHP errors that might be crashing the dashboard.
  • Inspect browser dev tools: Check the Network tab to see if the redirect to dashboard is returning a 404, 403, or 500 status code—this can tell you if it’s a routing, permission, or server error.

If you’ve gone through all these steps and still can’t figure it out, share snippets of your pages.php dashboard method, Users.php login method, and MY_Controller.php—that will help the community narrow down the exact issue!

内容的提问来源于stack exchange,提问作者AbuuJurayj Swabur Buruhan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:20:53