【问题标题】:codeigniter redirect fails only when i include "refresh"仅当我包含“刷新”时,codeigniter 重定向才会失败
【发布时间】:2026-01-29 20:50:01
【问题描述】:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class VerifyLogin extends CI_Controller
{

 public function __construct()
 {
    parent::__construct();
    //Model for fetching users in database
    $this->load->model('user', '', TRUE);
 }                             


 public function index()
 {
    //Load method to help verify form credentials
    $this->load->library('form_validation');

    //-----------------------------------------------
    //Verify Existance of values in login form fields
    //-----------------------------------------------
    //Validate existance of username field value
    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
    //Validate existance of password field value, and if exists then call 'check_database' func
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');

    //If the form validation is false (i.e. Missing form fields)
    if($this->form_validation->run() == FALSE){
            //Redirect back to login page
            $this->load->view('general-Login');
    }
    //Login Success, goto main-page
    else{
        //Go to main page
        redirect('Main', 'refresh');
    }
}

function check_database($password)
{
    //Field validation succeeded.  Validate against database
    $username = $this->input->post('username');

    //Check the username and password against database
    $result = $this->user->login($username, $password);

    //If there is a result
    if($result){
        //will be used for session information
        $sess_array = array();
        foreach ($result as $row){
            $sess_array= array(
                'id' => $row->id,
                'username' => $row->username
            );
            //Session set as logged in
            $this->session->set_userdata('logged_in', $sess_array);
        }
        return true;
    }
    //No existance of user or, incorrect password
    else{
        //Send Message of incorrect username or password
        $this->form_validation->set_message('check_database', 'Invalid Username or Password');
        return false;
    }
}
}

所以我有一个验证登录控制器来处理来自我的登录页面的表单数据。数据库访问效果很好,一切正常,直到我使用重定向功能。我尝试通过刷新进行重定向,它只是给了我一个显示“未定义”的页面。现在,当我删除“刷新”并留下控制器名称时,它会突然起作用,但我试图弄清楚为什么这个“刷新”不起作用。

我已禁用我的.htaccess 我尝试将$config['uri_protocol'] 设置为REQUEST_URIAUTOPATH_INFO 并没有运气。

另外,这是我第一次在网上提交给论坛之类的任何东西,所以请......温柔一点。

【问题讨论】:

    标签: php codeigniter redirect controller codeigniter-2


    【解决方案1】:

    首先,欢迎来到 Stack Overflow。

    其次,如果没有“刷新”的 redirect() 可以正常工作,我会使用它并稍后解决。

    为了更深入的解释,“刷新”尝试对页面进行类似元的刷新,类似于:

    &lt;meta http-equiv="refresh" content="0;URL='http://example.com/'"&gt;

    注意:它不会在您的视图中执行此操作,而是在响应中向浏览器发送类似的 sn-p)

    你用的是什么浏览器?什么版本?希望它是最新的,因为这可能会对刷新的工作方式产生影响。

    【讨论】:

      【解决方案2】:

      我有类似的问题。例如。在我的本地主机刷新不起作用并且在托管服务提供商设置上它起作用。也许在 Apache/PHP 配置中有些东西。不用刷新就可以了。我认为这与浏览器无关。发生这种情况时,我使用的是同一个浏览器。

      【讨论】:

        最近更新 更多