无法在codeigniter中使用回调重定向url

时间:2014-03-27 15:43:08

标签: php codeigniter url xss url-redirection

我只是创建登录类,在该用户输入他/她的主页部分后检查有效用户。但现在我需要有条件地改变用户家的目的地,无法做到 在网址重定向中附加switch case。

注意:我认为varibale没有在function.Plz中正确传递帮助我

我的代码如下:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class VerifyLogin extends CI_Controller {

        function __construct()
        {
            parent::__construct();
            $this->load->model('userp','',TRUE);
        }

        function index($dest) 
        {
            $this->load->view('header');  
            //This method will have the credentials validation
            $this->load->library('form_validation');
            $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
            $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');

            if($this->form_validation->run() == FALSE)
            {
                //Field validation failed.  User redirected to login page
                $data['msg'] = "";
                $this->load->view('login',$data);
            }

            $this->load->view('footer');
        }

        function check_database()
        {
            //Field validation succeeded.  Validate against database
            $email = $this->input->post('email');
            //query the database
            $password= $this->input->post('password');
            $result = $this->userp->login($email,$password);

            if($result!==FALSE)
            {
                $sess_array = array();
                foreach($result as $row)
                {
                    $sess_array = array(
                        'id' => $row->id,
                        'username' =>$row->email,
                        'fname' =>$row->first_name,
                        'lname' =>$row->last_name,
                        'pic'=>$row->pic
                    );
                    $this->session->set_userdata('logged_in',$sess_array);
                }

                switch($dest){
                    case 'NULL':{
                        redirect('home','refresh');   
                        break;
                    }
                    case 'part1':{
                        redirect('subhome','refresh');   
                        break;
                    }
                    case 'part2':{
                        redirect('subhome1','refresh');    
                        break;
                    }
                    default :{
                        redirect('mainhome','refresh');  
                        break;
                    }
                }
                return TRUE;
            }
            else
            {
                $data['msg'] =  "Something Wrong Username/Password";
                $this->load->view('login',$data);
                //return false;
            }
        }
    };
?>

1 个答案:

答案 0 :(得分:0)

问题是您正在将$dest传递给index()函数,但您正试图在check_database()函数中使用它。因此,要么将切换移动到索引函数中,要么希望将$dest传递给check_database(),如

function check_database($dest)
{
    //You can use $dest in here
}

如果要将额外参数传递给回调函数,则需要将其放在括号中,如

$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database[parameter]');

$this->form_validation->set_rules("password", "Password", "trim|required|xss_clean|callback_check_database[$parameter]");