Try Tuts+ Premium, Get Cash Back!
CodeIgniter From Scratch: Day 6
videos

CodeIgniter From Scratch: Day 6 – Login

This entry is part 6 of 17 in the CodeIgniter From Scratch Session
« PreviousNext »

In today’s hour long video tutorial, we’ll be doing a lot! We’ll implement a login and signup form that will allow us to lock down certain sections of our fictional site. As you’ll find, working with sessions in CodeIgniter is a cinch!

Catch Up

Day 6: Sessions

More Viewing Options

Login Form
Signup Form

P.S. The Logout Button

I forgot to add the “logout” button during the screencast. There was so much to cover, that I was bound to forget something! Luckily, it’s really easy. Simply add a link, to the “members” area, which links to the login class, and a “logout” method (login/logout). Then, all we must do is destroy the user’s session, and redirect them back to the login form. Download the source code if confused.

function logout()
{
	$this->session->sess_destroy();
	$this->index();
}

Hope you Enjoy it!

Keep in mind that we’re just scratching the surface, in terms of flexibility and security. We can – and very well may – take things much further. But this should get you started!

Thank you, Screencast.com!


Screencast.com

…for providing the hosting for these video tutorials.


Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • SeanH

    In “function validate_credentials()” we are using “redirect(‘site/members_area’);”.

    Is there any reason why we can’t:

    $data['main_content'] = 'members_area';

    $this->load->view('includes/template', $data);

    ?

  • freeride5567

    Awesome Tutorial! I was hoping you could make a tutorial on how to store and retrieve unique user data from database. Ex: So if i allow my user to store data in database once logged in, his data will be different from another users data depending on what they input.

  • http://twitter.com/madhureddykunta madhu reddy

    Hi am Madhu. I like your tutorials.
    Can you please integrate all the modules into one. if it is possible?

  • kyeZ

    It isn’t required to close form? (form_close (); )

  • newbie

    i got an error..it says->
    An Error Was Encountered
    In order to use the Session class you are required to set an encryption key in your config file.

  • FRANK JOHN

    after logout when i click the back button in the browser it takes me back to the page where i am checking the session for logged_in_user it says restricted only on page refresh how to fix it..??

  • Pedro

    hi, I´ve used the version 2.2 Codigniter. The contoller doesnt work with ” …. extends Controller ” . Works with ” …. extends CI_Controller “.

    The last function in the Site controller ” function __construct()

    { parent::Controller();

    $this->is_logged_in(); } returns error message

    “Call to undefined method CI_Controller::Controller() in C:xampphtdocsci_day6applicationcontrollerssite.php on line 5

    Can you help to solve the issue ?

  • Pedro

    Hi, i´ve find the solution !

    public function __construct()
    {
    parent::__construct();

    That´s it !

  • Novica89

    The only bad thing with this login / signup form is that more users with the absolutely same credentials could register. There is no checking if the user trying to register already exists. Good tutorial :)