CodeIgniter会话未设置

时间:2014-08-11 21:03:50

标签: php codeigniter session

在以下情况下未设置会话。它早些时候对我有用。 我正在使用CodeIgniter。

 public function authenticate(){
    $user = new Entity\User;
    //$user2 = new Entity\User;
    $user->username = "prayag@gmail.com";
    $user->password = "prayag";
    $userRepository = $this->doctrine->em->getRepository('Entity\User');
    $checkLogin = $userRepository->checkLogin($user);
    echo "Username => ". $checkLogin[0]->username;
    echo "ID => ". $checkLogin[0]->id;
    $this->load->library('session');
    $this->session->sess_destroy();

    if(!empty($checkLogin)){
        $CI =& get_instance();
        $this->session->set_userdata('user_id',$checkLogin[0]->id);

        $loggedIn = $this->session->set_userdata('user_id');    
            if($loggedIn !== TRUE){
                echo "session is not set";
                //echo $this->session->set_userdata('user_id');
            } else {
                    echo "session is set";
            }
        }
        else{
            echo "Username not found";  
        }
    }

我尝试使用以下帖子解决它:https://stackoverflow.com/questions/10712250/w hat-is-the-proper-way-to-test-codeigniter-session-variable

3 个答案:

答案 0 :(得分:0)

$this->session->sess_destroy();之后

不应该像session_start();

或者在代码中间销毁会话的重点是什么?

答案 1 :(得分:0)

更改此行

$loggedIn = $this->session->set_userdata('user_id');  

$loggedIn = $this->session->userdata('user_id'); 

答案 2 :(得分:0)

你加载了会话库吗

即。 $this->load->library('session');

另外,转换代码

$loggedIn = $this->session->set_userdata('user_id');    
            if($loggedIn !== TRUE){
                echo "session is not set";
                //echo $this->session->set_userdata('user_id');
            } else {
                    echo "session is set";
            }
        }

if($this->session->userdata('user_id')!=true)
   {
        echo "session is not set";
                //echo $this->session->set_userdata('user_id');
   } 
else 
{
    echo "session is set";
 }