在CodeIgniter中设置会话错误

时间:2013-05-01 09:42:15

标签: codeigniter

我试图在codeigniter中设置Session。当一个人尝试使用电子邮件ID和密码登录时。

1st)使用电子邮件ID找到他/她的角色。然后在会话中返回该结果并重定向到相应的链接。我输入的代码如下。你能解决这个问题吗?

控制器

public function login_validation() {
        $this -> load -> library('form_validation');
        $this -> form_validation -> set_rules('email', 'Email', 'required|trim');
        $this -> form_validation -> set_rules('password', 'Password', 'required ');

        if ($this -> form_validation -> run()) {

            $this -> load -> model('model_database');
            $sessiondata['var_userrole'] = $this -> model_database -> login_session();

            $this -> session -> set_userdata($sessiondata);
            if ($var_userrole == admin) {

                redirect('welcome/admin');

            } else if ($var_userrole == staff) {

                redirect('welcome/staff');

            } else if ($var_userrole == student) {

                redirect('welcome/student');

            } else {

                redirect('welcome/login');

            }

模型

public function login_session() {
    $email = $this -> input -> post('email');
    $login_id = $this -> db -> query("SELECT var_userrole FROM tbl_login where var_email = '" . $email . "'");
    return  $login_id -> row_array();
}

1 个答案:

答案 0 :(得分:0)

首先,$ var_userrole没有在if语句

中检入的任何地方定义

控制器

public function login_validation() {
        $this -> load -> library('form_validation');
        $this -> form_validation -> set_rules('email', 'Email', 'required|trim');
        $this -> form_validation -> set_rules('password', 'Password', 'required ');

        if ($this -> form_validation -> run()) {

            $this -> load -> model('model_database');
            $var_userrole = $this -> model_database -> login_session();// was undefined 
            $sessiondata['var_userrole'] = $var_userrole

            $this -> session -> set_userdata($sessiondata);
            if ($var_userrole == admin) {

                redirect('welcome/admin');

            } else if ($var_userrole == staff) {

                redirect('welcome/staff');

            } else if ($var_userrole == student) {

                redirect('welcome/student');

            } else {

                redirect('welcome/login');

            }

<强>模型

public function login_session() {
    $email = $this -> input -> post('email');
    $login_id = $this -> db -> query("SELECT var_userrole FROM tbl_login where var_email = '" . $email . "'");
      //$row =  $login_id -> row() // as object return $row->var_userrole;
      $row =  $login_id -> row_array();
    return $row['var_userrole']
}

如果您正在为auth开发新的东西,那么请检查CI中的某个auth系统,例如ion_auth,该组可以管理并检查用户所属的组Ion auth