CodeIgniter Forums
How to implement codeigniter login system ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: How to implement codeigniter login system ? (/thread-28341.html)



How to implement codeigniter login system ? - El Forum - 03-08-2010

[eluser]WarDoGG[/eluser]
Earlier i used to check logins in PHP using
Code:
isset($_SESSION['username'])

and used to put this code on top of every page that had to be login protected.

But now, how do i implement this in codeigniter ?

Do i have to do a session variable check inside every controller like this ? or is there a better way ?

Code:
class Gallery extends Controller  {
      
      function index() {
           if(isset($this->session->userdata('username')){
               // controller code here
           }
}



How to implement codeigniter login system ? - El Forum - 03-08-2010

[eluser]nelson.wells[/eluser]
There are a few different ways to do it. You can do it the way you're suggesting (though you don't use isset with CI's session class), but there are better ways. I'll tell you the way I do it, I'm sure someone else can offer other solutions.

I built an authorization library that tracked whether or not the user is logged in. I keep track of the data with sessions. In controllers that required the user be logged in, I can do something like this in the constructor.

Code:
if($this->auth->logged_in() && $this->auth->level > 5)
{
    //don't do anything, display the page as per the function
}
else
{
    //redirect to the login page
    redirect("login");
}

If you need more finely grained control, you could do this step in individual functions instead. If you wanted more broad control, you can put this functionality in a MY_ controller, and every controller that needd to be gaurded could inherit it.

Hope that helps.


How to implement codeigniter login system ? - El Forum - 03-08-2010

[eluser]WarDoGG[/eluser]
How did you write the authorization library ?

The library class cannot recognize $this->session ?

Can someone help me with this ?


How to implement codeigniter login system ? - El Forum - 03-08-2010

[eluser]WarDoGG[/eluser]
Also, how do i check if a user's logged in ?

if
Code:
isset($this->session->userdata("userid"))

doesnt work,

then will this do :

Code:
if($this->session->userdata("userid")>0)

Or can you suggest something more robust ?????


How to implement codeigniter login system ? - El Forum - 03-08-2010

[eluser]2think[/eluser]
WarDoGG,

I think what nelson.wells meant was that using the session class in CI to merely set the session would also set it in a cookie which could be changed by the user. Or so I'm thinking, he or others with more knowledge could clear that up.

If you want something quick, check out Ion Auth or Matt Auth in the Ignited Code forum on here.

Here's a posting I made in reference to another question on user auth. It's not definitive but might help.
http://ellislab.com/forums/viewthread/147674/