2

I am pretty new to codeigniter. this is my first project i am building a project and i am stuck in my login registration module. Registration functionality is working fine and after login my goal is to redirect the user to admin page where he/she can see his details. but the problem now is after checking the login credential and set the user session with this code

$session_data = array(
                'username' => $result[0]->user_name,
                'email' => $result[0]->user_email,
                );
$this->session->set_userdata('loggedin', $session_data);
redirect('logincontroller/admin_dashboard');

but my session doesn't set. when i var dump the $this->session->userdata['loggedin'] in my admin dashboard view it return empty or does not show anything

Here is the code of my login controller

<?php
class Logincontroller extends CI_Controller 
{
    public $outputData = array();
    public $loggedInUser;

    public function __construct() 
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->model('login_database');
        $this->load->helper('url');
        $this->load->database();
    }   
    public function index()
    {
        $this->outputData['pagetitle'] = "Login Form";
        $this->load->view('login_form', $this->outputData);
    }

    public function registrationform()
    {
        $this->outputData['pagetitle'] = "Registration Form";
        $this->load->view('registration_form', $this->outputData);
    }

    public function user_registration_process() 
    {

        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
        $this->form_validation->set_rules('email_value', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'trim|required');
        if ($this->form_validation->run() == FALSE) 
        {
            $this->outputData['pagetitle'] = "Registration Form";
            $this->load->view('registration_form', $this->outputData);
        } 
        else 
        {
            $data = array(
            'user_name' => $this->input->post('username'),
            'user_email' => $this->input->post('email_value'),
            'user_password' => $this->input->post('password')
            );

            $result = $this->login_database->registration_insert($data);
            if ($result == TRUE) 
            {
                $this->outputData['pagetitle'] = "Login Form";
                $this->outputData['message_display'] = 'Registration Successfully !';
                $this->load->view('login_form', $this->outputData);
            } 
            else 
            {
                $this->outputData['message_display'] = 'Username already exist!';
                $this->outputData['pagetitle'] = "User Registration";
                $this->load->view('registration_form', $this->outputData);
            }
        }
    }

    public function login_process() 
    {
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required');

        if ($this->form_validation->run() == FALSE) 
        {
            if(isset($this->session->userdata['loggedin']))
            {
                    redirect('logincontroller/admin_dashboard');
            }
            else
            {
                    $this->outputData['pagetitle'] = "Login Form";
                    $this->load->view('login_form', $this->outputData);
            }
        } 
        else 
        {
            $data = array(
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password')
            );
            $result = $this->login_database->login($data);
            if ($result == TRUE) 
            {
                $username = $this->input->post('username');
                $result = $this->login_database->read_user_information($username);
                if ($result != false) {
                $session_data = array(
                'username' => $result[0]->user_name,
                'email' => $result[0]->user_email,
                );
                $this->session->set_userdata('loggedin', $session_data);
                redirect('logincontroller/admin_dashboard');
                }
            } 
            else 
            {
                $this->outputData['pagetitle'] = "Login Form";
                $this->outputData['error_message'] = "Invalid Username or Password";
                $this->load->view('login_form', $this->outputData);
            }
        }
    }

    public function admin_dashboard()
    {
        $this->outputData['pagetitle'] = "Admin Dashboard";
        $this->load->view('admin_page', $this->outputData);
    }

    public function logout() 
    {
        $sess_array = array(
        'username' => ''
        );
        $this->session->unset_userdata('loggedin', $sess_array);
        $this->outputData['pagetitle'] = "Login Form";
        $this->outputData['message_display'] = 'Successfully Logout';
        $this->load->view('login_form', $this->outputData);
    }
}

I have also used the method specified in this link session destroying in codeigniter after redirecting but nothing happen

I also used php default $_SESSION in place of $this->session->set_userdata('loggedin', $session_data); but not worked now i also moved in the site to http website and set the setting in config file also here is the setting of config file

$config['base_url'] = 'http://www.ratneshviop.esy.es/';
$config['index_page'] = 'index.php';
$config['encryption_key'] = '';
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
$config['cookie_prefix']    = 'test';
$config['cookie_domain']    = 'http://ratneshviop.esy.es/';
$config['cookie_path']      = '/';
$config['cookie_secure']    = FALSE;
$config['cookie_httponly']  = FALSE;
$config['csrf_protection'] = FALSE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = TRUE;
$config['csrf_exclude_uris'] = array();

I am using codeigniter 3.0.6 on server with mysql support

Community
  • 1
  • 1

3 Answers3

2

Please see Codeigniter Document. In this document says; one more variable set session with this code:

$newdata = array(
    'username'  => 'johndoe',
    'email'     => 'johndoe@some-site.com',
    'logged_in' => TRUE
);

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

So you should change your code like this:

$this->session->set_userdata($session_data);
  • This code is not working for me when i add your code in my login_process function and run the page and print the `print_r($this->session->userdata);` then i will get this only this `Array ( [__ci_last_regenerate] => 1490872665 ) ` no user data – Ratnesh Choudhary Mar 30 '17 at 11:21
1

PHP 7 Upgrade - * Known SESSION / COOKIE Bug

This answer addresses the known session/cookie bug - when you upgrade to PHP7 from PHP 5.

If your CodeIgniter version is @ 3.1.0 or below - and you are upgrading to PHP 7.1 - You will need to update CodeIgniter. Otherwise, change your PHP 7.1 to 5.6 it will work fine.

There is a bug with $this->session->set_userdata(); - that can be pretty annoying. It will overwrite your session as soon as you redirect or visit another page within your site structure.

Or you can do this

if you are working in CI 3.x and just upgraded your server php version to php 7.x

Go to system/libraries/Session/session.php at Line no 281 and replace ini_set('session.name', $params['cookie_name']); by ini_set('session.id', $params['cookie_name']);

0
<?php class Logincontroller extends CI_Controller 
{
public $outputData = array();
public $loggedInUser;

public function __construct() 
{
    parent::__construct();
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->load->library('session');
    $this->load->model('login_database');
    $this->load->helper('url');
    $this->load->database();
}   
public function index()
{
    $this->outputData['pagetitle'] = "Login Form";
    $this->load->view('login_form', $this->outputData);
}

public function registrationform()
{
    $this->outputData['pagetitle'] = "Registration Form";
    $this->load->view('registration_form', $this->outputData);
}

public function user_registration_process() 
{

    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
    $this->form_validation->set_rules('email_value', 'Email', 'trim|required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'trim|required');
    if ($this->form_validation->run() == FALSE) 
    {
        $this->outputData['pagetitle'] = "Registration Form";
        $this->load->view('registration_form', $this->outputData);
    } 
    else 
    {
        $data = array(
        'user_name' => $this->input->post('username'),
        'user_email' => $this->input->post('email_value'),
        'user_password' => $this->input->post('password')
        );

        $result = $this->login_database->registration_insert($data);
        if ($result == TRUE) 
        {
            $this->outputData['pagetitle'] = "Login Form";
            $this->outputData['message_display'] = 'Registration Successfully !';
            $this->load->view('login_form', $this->outputData);
        } 
        else 
        {
            $this->outputData['message_display'] = 'Username already exist!';
            $this->outputData['pagetitle'] = "User Registration";
            $this->load->view('registration_form', $this->outputData);
        }
    }
}

public function login_process() 
{
    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
    $this->form_validation->set_rules('password', 'Password', 'trim|required');

    if ($this->form_validation->run() == FALSE) 
    {
        if(isset($this->session->userdata['loggedin']))
        {
                redirect('logincontroller/admin_dashboard');
        }
        else
        {
                $this->outputData['pagetitle'] = "Login Form";
                $this->load->view('login_form', $this->outputData);
        }
    } 
    else 
    {
        $data = array(
        'username' => $this->input->post('username'),
        'password' => $this->input->post('password')
        );
        $result = $this->login_database->login($data);
        if ($result == TRUE) 
        {
            $username = $this->input->post('username');
            $result = $this->login_database->read_user_information($username);
            if ($result != false) {
            $session_data = array(
            'username' => $result[0]->user_name,
            'email' => $result[0]->user_email,
            );
            $this->session->set_userdata('loggedin', $session_data);
            redirect('logincontroller/admin_dashboard');
            }
        } 
        else 
        {
            $this->outputData['pagetitle'] = "Login Form";
            $this->outputData['error_message'] = "Invalid Username or Password";
            $this->load->view('login_form', $this->outputData);
        }
    }
}

public function admin_dashboard()
{
 if(!(isset($this->session->userdata['loggedin'])))
        {
                $this->outputData['pagetitle'] = "Login Form";
                $this->load->view('login_form', $this->outputData);
        }
    $this->outputData['pagetitle'] = "Admin Dashboard";
    $this->load->view('admin_page', $this->outputData);
}

public function logout() 
{
    $sess_array = array(
    'username' => ''
    );
    $this->session->unset_userdata('loggedin', $sess_array);
    $this->outputData['pagetitle'] = "Login Form";
    $this->outputData['message_display'] = 'Successfully Logout';
    $this->load->view('login_form', $this->outputData);
}
}

Try this code for each method that you are loading a view you have to check sessions

   if(!(isset($this->session->userdata['loggedin'])))
        {
                $this->outputData['pagetitle'] = "Login Form";
                $this->load->view('login_form', $this->outputData);
        }
Shyamali
  • 329
  • 7
  • 22