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

CodeIgniter中普通用户登录后误跳转到管理员登录页问题

Troubleshooting Incorrect Redirect to Admin Login After User Login in CodeIgniter

Hey there, let's work through this redirect issue you're hitting with your dual login setup. I’ve tackled similar scenarios in CodeIgniter before, so here are the most common fixes to check off:

1. Verify Your Login Form's Submission Target

First up, double-check that your regular user login form is submitting to the correct route. It’s easy to accidentally hardcode the wrong action URL. Make sure your form’s action points to users/login instead of admin/login:

<!-- Correct user login form action -->
<form action="<?php echo base_url('users/login'); ?>" method="post">
    <!-- Form fields here -->
</form>

If this is pointing to the admin route, that’s why you’re being sent there after submission.

2. Check the Redirect Logic in Your Users Controller

Next, look at the login() method in your Users controller. When a regular user’s credentials are validated correctly, you must redirect them to a user-specific page (not the admin login!). Here’s a corrected example:

public function login() {
    // Run form validation
    if ($this->form_validation->run() === FALSE) {
        $this->load->view('users/login');
    } else {
        // Validate user with type=1 (regular user)
        $user = $this->user_model->authenticate(
            $this->input->post('email'),
            $this->input->post('password'),
            1 // Ensure we're checking for regular users
        );

        if ($user) {
            // Set session data for the logged-in user
            $this->session->set_userdata([
                'user_id' => $user->id,
                'user_type' => $user->type,
                'logged_in' => TRUE
            ]);

            // Redirect to USER dashboard, NOT admin login!
            redirect('users/dashboard');
        } else {
            // Show error and redirect back to user login
            $this->session->set_flashdata('error', 'Invalid email or password');
            redirect('users/login');
        }
    }
}

If you had redirect('admin/login') here by mistake, that’s a clear culprit.

3. Audit Your Permission Validation Logic

You mentioned you’ve set up permission checks—make sure these aren’t misdirecting users. For example, if you have a hook or controller constructor check for admin access, ensure it’s only applied to admin routes, not user routes.

Here’s how to structure role-based checks correctly:

// In Admin controller constructor
public function __construct() {
    parent::__construct();
    // Only enforce admin access for admin routes
    if (!$this->session->userdata('logged_in') || $this->session->userdata('user_type') != 2) {
        redirect('admin/login'); // Correct for admin pages
    }
}

// In Users controller constructor
public function __construct() {
    parent::__construct();
    // Enforce regular user access for user routes
    if (!$this->session->userdata('logged_in') || $this->session->userdata('user_type') != 1) {
        redirect('users/login'); // Correct for user pages
    }
}

If your user controller’s check was incorrectly redirecting to admin/login, that would cause the issue.

4. Fix Session Data Conflicts

Occasionally, old session data can interfere with new logins. Make sure you destroy the existing session before setting up a new user session:

// Before setting new session data in login method
$this->session->sess_destroy();
$this->session->sess_create();

// Now set the new user session data
$this->session->set_userdata([...]);

This ensures no leftover admin session data is causing misdirection.

5. Check for Misconfigured Routes

Finally, look at application/config/routes.php to ensure there’s no rogue route that’s mapping users/login to admin/login:

// Remove any line like this if it exists!
// $route['users/login'] = 'admin/login';

Quick Test Tip

If none of the above work, try testing in incognito/private browsing mode—sometimes browser cache can hold onto old redirects.


内容的提问来源于stack exchange,提问作者Mohammed Hamad

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:04:29