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
  • icantstop

    Thanks alot, Jeff! Maybe be will be good idea for the next screencast to show how implement and use ajax in CI?

    • http://www.freshclickmedia.com Shane

      That would be very good :)

    • jeanv

      that would be great!!!!

    • http://aryandelhi.webs.com aryan

      I’ll also vote for AJAX. JQuery preferred.

      • http://www.antonagestam.se Anton Agestam

        1 up for jQuery!

    • http://sliversofmysoul.netsons.org fluxish

      yes, ajax please! Also I prefer jquery

    • Anmol

      Yea, Please Please try to show how to use Ajax in code Igniter

    • Vishal

      Yes please some AJAX stuff :-) am up for it too

      • arnold

        good idea icantstop…jquery + ajax…hopefully

    • http://systemride.com Nick

      One more vote for ajax + jquery :)

    • http://geek-like-me.com Baylor Rae’

      Yes, please add a video on implementing jQuery in CI

    • Vincent

      AJAX & jQuery +1!

      That would be great ;)

      BTW, thanks for all the effort you put in these screencasts !

  • Carlos Caniguante

    At Last!!!! being waiting this all week :)
    Thank You!

    PD: Hey Jeff It’s seems that your personal domaing http://www.jeff-way.com/ has expired.

  • http://bazargostaran.com Hooman Asgari

    Thanks
    been waiting for this episode
    Thanks Jeff
    Thanks Envato for teaching us all the good stuff
    Look forward for the nex one too

  • http://www.fatlizardmedia.com Juan C Rois

    I’ve been looking into CodeIgniter for a while. This and all the previous posts will be very handy when I take the plunge.

    Thanks Jeff

  • Mathew Davies

    This is no good, hashing a password with 1 pass of md5 without a salt is not a good idea. Once your database is hacked, hackers can easily look up passwords via a rainbow table. Please reconsider your security implementation.

    There are a number of other issues I would fix.

    1. Hard-coded link in the “is_logged_in” method.
    2. The “is_logged_in” method should be in a library. No need for it to be in a controller.
    3. Line 59 of the login controller, assigning the query variable in the “if” statement is pointless, you are not using it.
    4. Your logout method should redirect to your index page, not call it. You will end with 2 URLs with duplicate content otherwise.
    5. The validate method in the “membership_model” can be improved by changing this :

    if($query->num_rows == 1)
    {
    return true;
    }

    into this

    return (bool) $query->num_rows;

    6. This tutorial should be teaching best practices, including a header in multiple goes against the DRY mantra.

    -Mathew

    • http://net.tutsplus.com Jeffrey Way
      Author

      Your Md5 reference — For an introductory tutorial to a login system, it’ll work just fine. We’ll cover more advanced security steps in future tuts. You can only cover so much in one tutorial.

    • http://www.jeff-way.com Jeffrey Way
      Author

      Also…

      #2 – Well sure. But we can only cover so much in a short tutorial. :)

      #3 – Ahh good point. I must have revised the code, and forgotten that I don’t need to assign that value. Good eye.

      #5 – This is true, but we’ll be adding to this section in the next tutorial. That’s why I used the long form.

      #6 – Not sure what you’re referring to. We only reference the header from the template view. Did you watch the screencast?

      • Mathew Davies

        For #6

        The login_form and signup_form views both include the header and footer files.

    • Jeff

      You must have accidentally received an old file. I just downloaded the source code, and they aren’t there. Sorry for the confusion. :)

    • http://www.fatlizardmedia.com Juan C Rois

      Mathew, I’m sure you are a seasoned coder, but many of us are just learning this stuff, which I believe is the point of the tutorial, I mean to get the basics.
      Thanks for pointing out this details, however I don’t think this tutorial is intended for real life implementations.
      In my case, I can’t wait to get to this level and hopefully one day I get to learn from one of your tutorials.

      Thanks everybody and keep up the good work.

      • Alain

        I agree with Juan.

  • Robert DeBoer

    I downloaded the movie, and it appears that there is voice problems – it will go out for some seconds and then come back in.

  • http://simon.vansintjan.org Simon

    Hey, I just finished building a login / sign up system using codeigniter.

    A couple of days too late this tutorial.

    I’ll watch it anyway to see if there is anything I missed.

  • http://www.kamaroc.com Kamal El Fatihi

    thankyou thats so great i will just start read the post i just had the news by rss

  • http://nathanledet.com Nathan Ledet

    Thanks so much for this Jeff.

    I really am learning a lot about CodeIgniter, and programming logic in general, with these tutorials.

    Keep it up…they really are helpful and I can’t wait till next week!!!

  • Guillermo Carrion

    Best Weekend gift ever! Specially since I have to give a 2-hour introductory tutorial about CodeIgniter to my fellow co-workers on Monday morning.

    Thanks a lot!

    :-)

  • http://voratec.com Ashit Vora

    Awesome. I have started loving CI. I was wondering if there’s any similar framework (easy to get started) for Java.

  • Wiredesignz

    Don’t use the input object inside your models. Pass the data in.

    Models are not meant to be aware of the framework at all.

    • eglyph

      +1 to this opinion. I would personally argue against using globals in the code. This way models aren’t portable across different projects, so better approach would to create model’s methods and pass data into it.

  • http://twitter.com/kevinquillen Kevin Quillen

    Nice video Jeff. I would only recommend not to shoot around so fast with windows and app switching, even I got dizzy hah.

    CI looks nice.

  • Javed Gardezi

    thank for this epi… waiting for this..
    Thanks a lot for providing download link :)

    Does it contain title for next epi???

  • http://www.xtensives.com Hasanga

    Hi,

    Nice tut Jeff! very well done!

    How about a tutorial using a master page template with CI?
    ah! using jquery with CI to post form data also would be nice.

  • Rahul

    Thanx a lot Jeff :)

  • Kenshin

    I am very interested to see the advanced security CI tut :-)

    Thank you Jeffrey for the very good work!!!

  • ricochet

    Thanks Jeffery!

    Do I just use the download link & give up checking iTunes from now on?

    • macCesar

      Jeff… I have the same question here…

      day 5 never made it in iTunes…

      In fact.. The most recent video is dated is 8/24/09 (WordPress for Designers: Day 18)

      So what gives!!!!…

      • http://www.jeff-way.com Jeffrey Way
        Author

        I know – sorry guys. It’s a pain, because screencast.com doesn’t automatically submit to iTunes like Blip did. However, the quality is much, much higher. I’ll take care of the iTunes issue this weekend. :)

  • ibura

    excellent!! as always

  • http://www.philsbury.co.uk Phil

    There’s an issue with CI Sessions in Internet Explorer (at least 6 and 7, but maybe 8 too), in that they don’t really work. There’s a library you can add to make it work properly, and all your code can remain the same. The library uses native PHP sessions, rather than CI’s version.

    There’s one on my blog for people that want it

    http://www.philsbury.co.uk/blog/code-igniter-sessions

  • Fynn

    Great stuff! I’ve recently watched the first lessons of CodeIgniter (being a total CodeIgniter noob), and started creating my own CMS from what I’ve learned from your tuts and the great Code Igniter user guide! Been playing with it for a few weeks now and already learned a lot.

    Thanks Jef! Will watch this one tonight. Keep ‘um going.

  • Carlos

    Nice tutorial, I have been waiting for this one!

    How about implementing a remember me, in the next tutorial?

  • Shaun C.

    Awesome Jeffrey, pure awesomeness.

  • http://www.jaaprood.nl Jaap Rood

    I’ve been following your CI tutorials, but this really was the one I was waiting for. Can’t wait to hear more on this topic!

  • http://blog.315design.com/ Mike

    Tutorial series that makes me wanna use CodeIgniter

  • JT

    Your series is making me convert to Codeigniter! Thanks and keep up the great work Jeff!

  • http://www.mykethecomputerguy.com Myke Bates

    It would be awesome to see some examples of the new cart library releases in 1.7.2. Your tuts are amazing!

    • http://tutorial-city.net Tutorial City

      I agree. A tutorial covering the cart class and a bit of an e-commerce website would be very cool.

  • http://www.benoitvilliere.com Benoit

    Thank you a lot Mr Way :D

  • Bobbybazooka

    This is some great stuff. Kudos to Jeff.
    Would be nice with a part 2 were you show how to tighten the security and so on.

  • http://www.brianswebdesign.com Brian Gottier

    Wow, an hour long tutorial shows the pure dedication that you have to both the tuts+ site, and to CodeIgniter! I have not ever seen an hour long tutorial for anything, anywhere on the net.

    I recently released an auth system for CodeIgniter. There are a lot of auth libraries in the CI wiki to choose from, but each with little differences.

    If you’re interested in getting a user management and authentication system up and running, you might check out my Community Auth. You don’t have to do it yourself!

    Community Auth website w/ download – http://auth.brianswebdesign.com/

    CI wiki page – http://codeigniter.com/wiki/Community_Auth/

  • Cody

    Awesome thank you very much Mr.Way!

    One request: Would it be possible to export your database and include that within the source!

  • Sri

    This was a very good tutorial. I like how all the different aspects have been combined in this one (things like templating, sessions, new css properties…)

    Would like to see an extended version of this tutorial for day 7. Preferably a little more robust with things like different access levels.

    Thanks!

  • http://www.fatlizardmedia.com Juan C Rois

    Hey Jeff, for the little research I’ve done so far, I notice that there are “helpers” for Prototype and Scriptaculous. Do you know if there is one for jQuery?
    Also, there should be a link to your screencasts on the CodeIgniter Wiki page. I was going to add them my self but I figured I’d be out of line or maybe it’s better once the series is completed.

    Thanks

    • http://www.jeff-way.com Jeffrey Way
      Author

      Yeah – I’m hoping that the CI sites makes some reference to this video series.

      • http://www.fatlizardmedia.com Juan C Rois

        Well, the Wiki page actually allows user edits, so I actually added the links to the screencasts myself.
        This is the link to the CI Wiki page:
        http://codeigniter.com/wiki/Category:Help::Tutorials/

        Hope it brings even more people to Nettus.

  • dani

    Thank you very much Jeffrey. I waited so long for such a tutorial.

    Cheers

  • http://www.encoder2002.com Daniel Sitnik

    Thanks for the awesome tutorial Jeff!!

    And thanks so much for showing an easy way to load header/footer in all pages, I asked for it last week. :D

  • http://kreatemarket.com Fred Flinston

    This is really cool yeah!!! wohoo

  • http://www.umoor.eu/blog/ umoor

    Hey Jeff I see Screencasts are no more hosted on Blip.tv is there a reason for that?

  • rosnovski

    Jeff,

    The only thing to say is well done and please keep them coming. Your other series have done us a lot of good. Thanks Muchacho!

  • C.44

    Great Tut again Jeffrey, we won’t get bored by a hourlong screencast. On the contrary, we love hourlong screencasts as it shows your dedication and commitment to make a decent vid that we all benefit from. Props to you :)

    One thing i’ve been wondering, you have the default controller login, after that you have the controller members_area (iirc) that actually checks if the user is_logged_in, but how do we check that in other controllers ? I mean, it wouldn’t be right to just stuff everything into one controller and model with a load of functions and views.

    Btw, are we allowed to give some suggestions for the next tut ? I’d love to see some ajax action to be honest.

  • http://www.kriesi.at Kriesi

    dont know if I am the only one who is encountering these problems with screencast.com content, but downloading the video doesnt really work. I get decent download speed for the first half and then it drops to 4-5kb/sec until it stops completley. straming the video is a pain as well.

    would love to see this one on itunes, it seems its the only way to get it for me :(

  • timo.huber

    thanks again :)

  • ibura

    @Kriesi, I am having the same problem with the download link.

    • Amine

      Awesome tutorial !! I’ve been waiting for these explanations for so long :)
      Really looking forward for a bullet proof security tutorial and a user management tutorial (groups, rights …) !
      Thanks a lot Jeff for the effort and such complete tutorials !

  • tom kirby

    wonderful tutorial,

    it would be good to see a tut on more detailed session security, combinding the CI session DB in aswell,

    also, i noticed you never checked if a username existed before inserting…

    Nice tut! trying to learn CI recently, and this is most defo the best way! many thanks, many more tuts to follow i hope! :)

  • http://guthnur.net richard

    Do you plan on making the sql available for the whole series?

  • http://www.itsmotherfuckintylerperry.com Tyler Perry

    Very cool tutorial. I have always wanted to learn this stuff. I understand you can only do so much in one video but don’t mind longer videos. Keep it up man. This is extremely helpful when it comes to building websites. This is a skillset that is very important to have.

    Thanks

  • http://tutorial-city.net/ Tutorial City

    Great Jeffrey, you’re the man! ;)
    The web comunity owe you a lot!

  • Vishal

    Hi Jeff,

    Let me first tell you something that I always wait eagerly to watch your Code Igniter Screencast and I’ve learned Codeigniter with the help of all your 6 videos. I really really appreciate your contribution towards Codeigniter Community. You are doing a lot for beginners like me, I really appreciate that! I would like to thank you a lot for such videos. These videos keeps us updated and boost our confidence to learn something new. I saw that Codeigniter 1.7.2 has been released. Hope you will include some new features from that in your videos. Is it possbile for you to include how to used Ajax/Javascript in Code Igniter?

    Many Thanks
    Vishal

  • http://www.soulsizzle.com Ryan

    Though I was able to figure most of this out on my own previously, I’ve still been waiting on this exact tutorial. Good lookin out!

  • http://mike.carinslater.com slatron

    Yeah, I just did this for a number of sites over the past few months in CI – but it’s great to see how someone else goes through the same process. No matter what you’re covering – I always pick up little ways to improve my code. And not always in ways directly pertaining to the subject you’re covering.

    I’ve been recommending CI to a lot of people – and this is the first place I send them for tutorials. Much better than the ones on the CI site.

    Thanks again, Jeffrey! Let’s see some url rewriting in future parts of the series. But really, I’ll watch these tutorials on any CI aspect.

    • Vishal

      I do the same thing, I always recommend all PHP/Codeigniter users to watch these videos :-) Thanks again Jeff

  • Karl Fredrik

    Hello jeff, really great tutorial – as always :D

    But i get a Database Error :S
    http://i26.tinypic.com/2cclzer.png

    Whats wrong ?

    • Vishal

      check your database fields! does it says email_address ? is it spelled correctly in your DB table?