dsstlsqt696435 2015-09-27 12:13
浏览 47

sess_regenerate在CodeIgniter 3.01中不起作用

I have login form here: http://localhost/AuctionsApp/account/login

In controller I have this code:

// 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' );

    }

I want to create a logout so I created a new functions:

// 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( '/' );

    }

When I enter in: http://localhost/AuctionsApp/account/logout, I get the message and redirected but I can not go back to the login page.

  • 写回答

1条回答 默认 最新

  • dongluobei9359 2015-09-27 12:45
    关注

    I would suggest you to handle this using two classes

    • First create two new classes called Login and Logout,

    • Then inside login create a function called validate,

    • Then inside logout class use the index function just to destroy the
      session and redirect user to the login

    Login class,

    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');
    }
    

    Logout class

    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');
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染