douqi4673
2014-08-11 21:03
浏览 43
已采纳

CodeIgniter会话未设置

The session is not set in the following case. It was working for me earlier. I am using 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";  
        }
    }

I tried to solve it using the following post: https://stackoverflow.com/questions/10712250/w hat-is-the-proper-way-to-test-codeigniter-session-variable

图片转代码服务由CSDN问答提供 功能建议

在以下情况下未设置会话。 它在我之前工作。 我正在使用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(! 空($ checkLogin)){
 $ CI =&  get_instance(); 
 $ this-> session-> set_userdata('user_id',$ checkLogin [0]  - > id); 
 
 $ loggedIn = $ this-> session-> set_userdata(  '用户身份');  
 if($ loggedIn!== TRUE){
 echo“session not set”; 
 // echo $ this-> session-> set_userdata('user_id'); 
} else {
  echo“session is set”; 
} 
} 
 else {
 echo“User name not found”;  
} 
} 
   
 
 

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dsbowmvth16379079 2014-08-12 13:02
    已采纳

    did u load the session library

    i.e. $this->load->library('session');

    also, convert the code

    $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";
                }
            }
    

    to

    if($this->session->userdata('user_id')!=true)
       {
            echo "session is not set";
                    //echo $this->session->set_userdata('user_id');
       } 
    else 
    {
        echo "session is set";
     }
    
    已采纳该答案
    打赏 评论
  • doutu9810 2014-08-11 21:08

    After $this->session->sess_destroy();

    Shouldn't be something like session_start(); ?

    Or what's the point to destroy session in the middle of code?

    打赏 评论
  • dongquan0024 2014-08-12 12:48

    change this line

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

    to

    $loggedIn = $this->session->userdata('user_id'); 
    
    打赏 评论

相关推荐 更多相似问题