51 thoughts on “Create User login with codeigniter

  1. It is really very awesome accept a little bug in login_model.php file script replace $query->num_rows with $query->num_rows()

    That’s all

  2. the use of xss_clean() in $username = $this->security->xss_clean($this->input->post(‘username’)); is unnecessary since you are not storing data… the active record class escapes data automatically to prevent sql injections…

  3. awwwwwwwww amazing i just have complete the login form in just 10 min
    but one thing i dont understand neither it take from me the user name not password nor it logged in!!!
    anybody is here to help me in this regards

    thnx in advance

  4. thanks man
    your short tutorial its great
    although i have a problem
    when i want to exit of the session
    i get this error:
    Call to undefined method CI_Session::destroy()
    but i wrote it correctly and the session class is loaded on the autoload file

    Please excuse my bad English

    1. By what I see, CI_Session::destroy(), I think you wrote

      it should be

  5. This is a great script but forme does not seem to work in IE, it works in all other browsers, is this a known issue?

    Any help would be much appreciated!

    1. Actually, it is a known error with codeigniter and IE with sessions and cookies. I had this problem once, and solved it by changing the default cookie name, from ci_sessions to cisessions. In many cases this fix works for IE, but not on all. You should try it out and see if it works for you.

      This is indeed one of the reasons why I built my own small sessions class. It is not a perfect class, but get’s the job done. You can take a look at it here:

      http://www.jotorres.com/2012/06/simple-sessions-class/

  6. Hi dude,
    It’s very useful,

    i had small problem, if user is already logged in it didn’t show the login page it has to redirect to some page (after valid login page mean user home ).where i have to check whether user is logged-in or not how to check.

  7. Great example. I insert one line code in home.php to retrive session for example ‘userid’

    public function index(){
    // If the user is validated, then this function will run
    echo ‘Congratulations, you are logged in.’;
    echo $this->session->userdata(‘userid’);

    // Small correct for tag
    echo ‘
    Logout!‘;
    }

  8. my views codeIgniter is always the best position as it is light Weight and easy to use and allows your perform by providing some of the functions. It allows fantastic separation between logic and presentation.

  9. Thanks for the article! I’m new to CI (I’ve never coded with PHP outside of WordPress and some minor things).

    I liked the script and everything, but for me your logout system was a little too “clunky” (for lack of a better word). I don’t know if what I did was smart or not, but I went ahead and made “logout” it’s own controller:

    class Logout extends CI_Controller{

    function __construct(){
    parent::__construct();
    $this->load->helper(‘url’);
    $this->load->database();
    }

    public function index($msg = NULL){
    $this->session->sess_destroy();
    redirect(‘login’);
    }

    You can pretty much guess where I go from there!

    Thanks a lot for the article! Bookmarked for later, it may come in handy! Wait — actually, it will. 🙂

  10. Awesome !!
    Thank for this tutorial , it helps a lot !!
    Just a question :

    in my DB i store the password for registered users like this :

    $data_user[‘user_password’] = sha1($this->input->post(‘user_mail’) . $this->input->post(‘user_password’));

    How can i verify this when a user try to login ? Cause when he trys i dont know his email… or maybe should i try to log them with not the user name but the used email in registration ?

    Thanks in advance.

    1. Hi Kliklipse,

      Nice question. You can use either one you would prefer as the username. Remeber, you need to set all these fields as unique within your script while doing registration. Assuming you already have the login script, (hence reading this post), at a given point you are retrieving the users data. Within that data, you should have the user email. With this in mind, all you need to do is add a bit of code within the model above. Check it out: adding to the validate function in the model above

  11. In order to follow good practices in MVC programming, i think that the controller must receive the posted variables from the form and then transfer to the function in the model by parameters. Great tutorial bro, it helps me so much.

  12. I’ve got another idea “reg_user”
    I will use that on my side
    username that you can not use it as the pw
    and not the domain name
    I know this is not the yellow of the egg
    but I have often noticed that many do so

    srry for my bad english
    and since I have another question

  13. I am using this code to create the login page,but when i submit after given the username and password,it will not redirect to the home page.

  14. Hi,

    Log in works good, but when I try to attempt log out it is redirect to login page and then is i click on back it shows the last running session
    Can you please help me to solve this issue?
    Please reply me ASAP

    Thank You

  15. hi why is it when i submitted the user and password it will direct me to login/process url but i will got a HTTP 404 Not Found.. please help

      1. yes I saw that code, but why codeigniter has not provided the functionality to handle this?
        Infact the above logic is working fine for some people. why am I facing this problem?
        Do you have any answer?

        1. I really wish I did. From what I have investigated, some people had that problem with the naming of cookies in CI, others were Internet Explorer, etc. It’s something really weird, and I have decided to stop using them for that same reason. So, to avoid hassle and headaches, I just use my own sessions, which use PHP Native sessions.

  16. Then what is the use of the below function?
    function __construct()
    {
    parent::__construct();
    }
    you said that it is used to validate all the functions of tat particular controller.
    I tried it but its not working.

  17. Then what is the use of the below function?
    function __construct()
    {
    parent::__construct();
    }
    you said that it is used to validate all the functions of tat particular controller.
    I tried it but its not working.
    i tried checking the session within this construct, but no satisfaction

    1. It’s the same issue with CI. If you hit the back button, it will return to your previous page without “loading”, it seems to be a cache problem and it seems to return to the ‘members’ area, even if you logged out. If you hit refresh on that same page, you will then get redirected to the login page.

Leave a Reply

Your email address will not be published. Required fields are marked *

The qTranslate Editor has disabled itself because it hasn't been tested with your Wordpress version yet. This is done to prevent Wordpress from malfunctioning. You can reenable it by clicking here (may cause data loss! Use at own risk!). To remove this message permanently, please update qTranslate to the corresponding version.