【发布时间】: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