【问题标题】:sess_regenerate doesn't work in CodeIgniter 3.01sess_regenerate 在 CodeIgniter 3.01 中不起作用
【发布时间】:2015-12-24 19:06:49
【问题描述】:

我这里有登录表单:http://localhost/AuctionsApp/account/login

在控制器中我有这个代码:

// Logowanie
    public function login()
    {

        // Sprawdzenie czy user jest zalogowany, jeśli tak to nastepuje przekierowanie do strony głównej
        logged_in() == false || redirect ( '/' );

        if ( !empty( $_POST ) )
        {

            if ( $this->form_validation->run( 'site_account_login' ) == true )
            {

                $username = $this->input->post( 'username' , true );
                $password = $this->input->post( 'password' , true );

                $where = array( 'username' => $username );
                $user = $this->Site->get_single( 'users' , $where );

                if ( !empty( $user ) ) 
                {

                    if( password_verify( $password , $user->password ) == 1 ) 
                    {

                        // Sprawdzenie czy użytkownik jest aktywny
                        if( $user->is_active == 1 )
                        {
                            // Zalogowanie użytkownika
                            $data_login = array(
                                'id' => $user->id,
                                'username' => $user->username,
                                'email' => $user->email,
                                'logged_in' => true
                            );

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

                            // Zapamiętaj mnie
                            if ( $this->input->post( 'remember' , true ) == 1 ) 
                            {

                                $remember_code = random_string();

                                $where = array( 'id' => $user->id );
                                $data = array( 'remember_code' => $remember_code );
                                $this->Site->update( 'users' , $where , $data );

                                $user_info_array = array(
                                    'id' => $user->id,
                                    'username' => $user->username,
                                    'email' => $user->email,
                                    'logged_in' => true,
                                    'remember_code' => $remember_code
                                );

                                $user_info_json = json_encode( $user_info_array );

                                $data_cookie = array(
                                    'name' => 'remember_me',
                                    'value' => $user_info_json,
                                    'expire' => 60*60*24*365,
                                    'path' => '/',
                                );

                                set_cookie( $data_cookie );

                            }

                            // Przekierowanie do strony głównej i wyświetlenie komunikatu
                            $this->session->set_flashdata( 'alert' , 'Zalogowałeś się poprawnie!' );
                            redirect( '/' );

                        }
                        else
                        {
                            $this->session->set_flashdata( 'alert' , 'Musisz aktywować konto, żeby się zalogować!' );
                            refresh();
                        }

                    }
                    else
                    {
                        // Użytkownik podał złe hasło
                        $this->session->set_flashdata( 'alert' , 'Błędne hasło.' );
                        refresh();
                    }

                }
                else
                {
                    // Uzytkownik nie istnieje
                    $this->session->set_flashdata( 'alert' , 'Użytkownik nie istnieje.' );
                    refresh();
                }

            }
            else
            {
                $this->session->set_flashdata( 'alert' , validation_errors() );
                refresh();
            }

        }

        // Ładowanie widoku
        $this->load->view( 'site/account/login' );

    }

我想创建一个注销,所以我创建了一个新函数:

// Wylogowywanie
    public function logout() 
    {

        $this->session->sess_regenerate( true );
        delete_cookie( 'remember_me' );

        // Wyswietlenie komunikatu i przekierowanie do strony głównej
        $this->session->set_flashdata( 'alert' , 'Wylogowałeś się.' );
        redirect( '/' );

    }

当我输入:http://localhost/AuctionsApp/account/logout 时,我收到消息并重定向,但我无法返回登录页面。

【问题讨论】:

    标签: php codeigniter session login logout


    【解决方案1】:

    我建议你使用两个类来处理这个问题

    • 首先创建两个名为LoginLogout的新类,

      李>
    • 然后在login里面创建一个名为validate的函数,

    • 然后在 logout 类中使用 index 函数只是为了销毁 strong>
      会话并将用户重定向到登录页面

    登录类,

    public function validate()
    
    {
        // Sprawdzenie czy user jest zalogowany, jeśli tak to nastepuje przekierowanie do strony głównej
        logged_in() == false || redirect('/');
        if (!empty($_POST)) {
            if ($this->form_validation->run('site_account_login') == true) {
                $username = $this->input->post('username', true);
                $password = $this->input->post('password', true);
                $where = array(
                    'username' => $username
                );
                $user = $this->Site->get_single('users', $where);
                if (!empty($user)) {
                    if (password_verify($password, $user->password) == 1) {
                        // Sprawdzenie czy użytkownik jest aktywny
                        if ($user->is_active == 1) {
                            // Zalogowanie użytkownika
                            $data_login = array(
                                'id' => $user->id,
                                'username' => $user->username,
                                'email' => $user->email,
                                'logged_in' => true
                            );
                            $this->session->set_userdata($data_login);
                            // Zapamiętaj mnie
                            if ($this->input->post('remember', true) == 1) {
                                $remember_code = random_string();
                                $where = array(
                                    'id' => $user->id
                                );
                                $data = array(
                                    'remember_code' => $remember_code
                                );
                                $this->Site->update('users', $where, $data);
                                $user_info_array = array(
                                    'id' => $user->id,
                                    'username' => $user->username,
                                    'email' => $user->email,
                                    'logged_in' => true,
                                    'remember_code' => $remember_code
                                );
                                $user_info_json = json_encode($user_info_array);
                                $data_cookie = array(
                                    'name' => 'remember_me',
                                    'value' => $user_info_json,
                                    'expire' => 60 * 60 * 24 * 365,
                                    'path' => '/',
                                );
                                set_cookie($data_cookie);
                            }
                            // Przekierowanie do strony głównej i wyświetlenie komunikatu
                            $this->session->set_flashdata('alert', 'Zalogowałeś się poprawnie!');
                            redirect('/');
                        }
                        else {
                            $this->session->set_flashdata('alert', 'Musisz aktywować konto, żeby się zalogować!');
                            refresh();
                        }
                    }
                    else {
                        // Użytkownik podał złe hasło
                        $this->session->set_flashdata('alert', 'Błędne hasło.');
                        refresh();
                    }
                }
                else {
                    // Uzytkownik nie istnieje
                    $this->session->set_flashdata('alert', 'Użytkownik nie istnieje.');
                    refresh();
                }
            }
            else {
                $this->session->set_flashdata('alert', validation_errors());
                refresh();
            }
        }
        // Ładowanie widoku
        $this->load->view('site/account/login');
    }
    

    注销

    public function index()
    
    {
        $user_datas = $this->session->all_userdata();
        foreach($user_datas as $key => $user_data) {
            $this->session->unset_userdata($key);
        }
        $this->session->sess_destroy();
        $this->session->set_flashdata( 'alert' , 'Wylogowałeś się.' );
        redirect('login');
    }
    

    【讨论】:

    • 我也可以将 sess_destroy 添加到我的函数中,但是没有显示消息。
    • @zf2newbie 你想显示什么消息?..sess_regenerate 将重新生成会话 ID。
    • @zf2newbie 我已经更新了我的代码...你可以添加任何你想要的消息。
    猜你喜欢
    • 2012-12-16
    • 2014-12-23
    • 2018-04-23
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多