当用户尝试输入寄存器 URL 时,如何让用户注销?

How to make user logout ,when he try to enter register URL?

我正在尝试让用户在登录时输入 url 进行注册、注销并显示注册表。

我在我的控制器注册方法中添加了这个,但它不起作用:

 public function register()
 {
        if($this->session->userdata('is_logged_in')) {
            $this->session->sess_destroy();
            redirect('auth/logout');
        }
        //form validation
        if ($this->form_validation->run()==FALSE)
        {
            $this->signup();
        }
        else 
        {  
          //login
        }
  }

  public function signup()
  {

        $data['dynamic_view'] = 'auth/register_form';
        $this->load->view('templates/main',$data);  
  }

我的登录方式是

 public function login ()
    {

        $this->form_validation->set_rules('username', 'username', 'trim|required');
        $this->form_validation->set_rules('password', 'password', 'trim|required'); 


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

            $this->index();
        }
        else 
        {
            if ($user = $this->auth_model->login()) {
                if(count($user) > 0 )    
                {
                    $this->load->library('session'); 

                     $data = array(
                        'username' => $user['username'],
                        'user_id' => $user['user_id'],
                        'is_logged_in' => TRUE

                    );
                    $this->session->set_userdata($data);         

                    redirect('home');
                }
            }
            else
            {   

                 $this->index();
            }
        }
    }

尝试$this->session->unset_userdata('is_logged_in');,无需销毁整个会话。在这种情况下我会做其他事情,我会重定向到主页并设置 flashdata 消息。

加载库也可能有问题。尝试将会话库添加到 application/config 中的 autoload.php。

在您的 public function register() 中,如果您使用 redirect('auth/logout'); 重定向到另一个页面,脚本的其余部分将永远不会执行。 $this->session->sess_destroy(); 应该足以断开用户连接。