Posts Tagged ‘session’

Creating user session checking with CodeIgniter library

April 17th, 2010   by  aditia rahman

When we build web application with user authentification, the application must have user login/logout or signin/signout or whatever you called it, then the system must have user logged feature to check whether the user is logged in or logged out. Session is the most common way to checking it. In Codeigniter, using Session Class you can use simple function inside controller, it’s something like this:

class User extends Controller {

	// constructor class
	function __construct()
	{
		parent::Controller();
		$this->load->library('session');
	}

	function index()
	{
		$this->load->view('user/index');
	}

	function login()
	{
		// if user already logged in, redirect to user index
		if ($this->_is_logged_in())
		{
			redirect('user/index');
		}
		else
		{
			$this->load->view('user/login');
		}
	}

	function register()
	{
		// if user already logged in, redirect to user index
		if ($this->_is_logged_in())
		{
			redirect('user/index');
		}
		else
		{
			$this->load->view('user/register');
		}
	}

	// checking user logged user by registered session
	function _is_logged_in()
	{
		$logged = $this->session->userdata('user_id');
		if ($logged)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
}

Posted in Codeigniter PHP3 Comments