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