php重定向,前面有一个会话分配

时间:2013-12-28 18:01:09

标签: php codeigniter session

如您所知,任何与服务器的交换都会使header()变得不稳定。对于我的项目,在登录控制器中,我看到用户是否插入了真正的user / pass,我设置了会话$_SESSION['email'],然后将其重定向到user_home控制器。这会导致网站托管时出现问题。问题是什么意思?我只是说它什么都不做,既不是错误也不是别的。我该怎么办?

以下是使用CodeIgniter编写的代码作为登录控制器的示例。密切注意重定向和该会话分配之前。

$data['email'] = $this->input->post("email");
$data['password'] = $this->input->post("password");

echo ($login_result = $this->authentication->login($data));

if($login_result == -1)
{                   
    $this->session->set_flashdata("login-result", FAILED);
    redirect(base_url("access/login")); 
}
else if($login_result != -1)
{                                       
    if($login_result['status']  == 0) // not activated
    {
        $this->session->set_flashdata("login-result", NOACT); // for constant use please check the appropriate files
        redirect(base_url("access/login")); 
    }
    else if($login_result['status']  == 1) // activated
    {
        $this->session->set_userdata("email", input("email"));
        redirect(base_url("panel/home"));   
    }
    else if($login_result['status']  == 2) // banned
    {
        $this->session->set_flashdata("login-result", BANNED);// for constant use please check the appropriate files
        redirect(base_url("access/login")); 
    }                       
}               

2 个答案:

答案 0 :(得分:0)

您重定向功能正在使用标题:

header('Location: http://www.example.com/');
必须在发送任何实际输出之前调用

header(),无论是普通HTML标记,文件中的空行还是PHP。

$data['email'] = $this->input->post("email");
$data['password'] = $this->input->post("password");

$login_result = $this->authentication->login($data);

if($login_result == -1)
{                   
    $this->session->set_flashdata("login-result", FAILED);
    redirect(base_url("access/login")); 
}
else if($login_result != -1)
{                                       
    if($login_result['status']  == 0) // not activated
    {
        $this->session->set_flashdata("login-result", NOACT); // for constant use please check the appropriate files
        redirect(base_url("access/login")); 
    }
    else if($login_result['status']  == 1) // activated
    {
        $this->session->set_userdata("email", input("email"));
        redirect(base_url("panel/home"));   
    }
    else if($login_result['status']  == 2) // banned
    {
        $this->session->set_flashdata("login-result", BANNED);// for constant use please check the appropriate files
        redirect(base_url("access/login")); 
    }                       
}

echo $login_result;

答案 1 :(得分:0)

修复Dmitriy的建议(可能是正确答案)将是:

$data['email'] = $this->input->post("email");
$data['password'] = $this->input->post("password");

$login_result = $this->authentication->login($data);
if($login_result == -1)
{                   
    $this->session->set_flashdata("login-result", FAILED);
    redirect(base_url("access/login")); 
}
else if($login_result != -1)
{                                       
    if($login_result['status']  == 0) // not activated
    {
        $this->session->set_flashdata("login-result", NOACT); // for constant use please check the appropriate files
        redirect(base_url("access/login")); 
    }
    else if($login_result['status']  == 1) // activated
    {
        $this->session->set_userdata("email", input("email"));
        redirect(base_url("panel/home"));   
    }
    else if($login_result['status']  == 2) // banned
    {
        $this->session->set_flashdata("login-result", BANNED);// for constant use please check the appropriate files
        redirect(base_url("access/login")); 
    }                       
}

// echo ($login_result = $this->authentication->login($data));