Codeigniter App Tutorial
Lesson 6 of 10 · Codeigniter App Part 6 – Login/Register System · IT & Software
Codeigniter App Part 6 – Login/Register System
all right in this section we're going to take a look at models and models are used to they used to interact with data whether that be database or maybe a
restful api or something like that even hard-coded values it's it's just used to hold the data or just grab the data from something all right so what we want to do here is we have our registration form
and we want to submit it and enter our fields into the database and we need to do that through a model alright so we have our form and it's going to submit to the users controller and we're going
to do some validation on it and if the validation passes then we want to insert the data now of course we need to create a user's model but to do this we can call this user model and models it's not
obviously it's not mandatory but I recommend using singular for models and plural for controllers so we have a user's controller but we'll have a user model alright so user model and then a
function or a method that we want to run which will be create member all right so that's gonna call the create member method in the user model all right now instead of just calling it
I want to wrap it in an if statement so that we can redirect we can get a message saying you are now registered and redirect all right so let's put this in if statement alright so we're going
to redirect back to home a home index but I don't want it to just redirect we want to we want to create a message to let the person know that they're actually registered and the most common
way to do that with codeigniter is to use flash data and that's that's given to us by the session library all right so we can say this session set flash data all right and in here
we're gonna have two arguments we're gonna have basically a key so say registered and it's just basically a key to identify this message and we're just gonna say you are now registered and can
login all right so it's gonna set this this flash date of this message and redirect to the index page the home index page so I'll save this and that's the first part of this is setting the
data now we need to get it and we need to get it in the index view because this is going to go to home index excuse me the home view so we'll go to our home view and right above this here
we want to call that message but we only want to call it if it's set all right so we have to do first do an if statement let's say if I'm gonna say if this session last data
and in here we want to put whatever that key was in our case was registered all right and I'm just doing the shorthand so that we can just use tags on every line here all right so what do we want
to do if that's if that's found if the registered flash data is found then we want to echo let's see we want to echo this session flash data so this session last data
registered and what I want to do is wrap this in the bootstrap classes for the alerts which we have somewhere in here where do we have it right here and copy that
you just paste this and you know like this all right so if we're registered actually you know what this shouldn't say danger because
this is a helpful message a success message so I think that that works I think that'll make it green alright so save that and get back to this deal with the model alright so what we need to do
is create a user model so in the models folder we want to create a new file and save it as user model dot PHP alright so this is similar to setting up a controller I'm gonna say a class user
model extends not CI controller but CI model all right so we need a public function called create member all right so what we need to do is we need to grab all the
fields that we submit and if you have any experience with PHP you probably know that this goes through post data all right and we can grab the post data if you're dealing with just PHP without
CodeIgniter we could get it in this this post superglobal so we could have like post first-name in coding NIDA we actually can use the input helper which we have Auto loaded and we can use this
input post and then and then whatever first-name and that's what we're gonna do all right and we could do this we're going to create an array of the post either we
could do this in the controller but it's good practice to try to keep your controller thin and then have most of the this the data manipulation go on in the model so it's I mean it's really
just preference but I'm just gonna put it in the model so I'm going to create an array well okay so in this array we want to we want
to set the database table name equal to the post values all right so the first one we have is first-name and that's going to come from this input post this input post first-name okay
and we'll actually just copy this all right so we have last name can these here these fields need to match your database your database fields okay which we have
right here okay last name email I'm what else username and password
now we don't want to store just a plain password which is which is what this will give us right here so what I want to do is I want to encrypt it and that's pretty easy to do I'm just going to cut
this out and still in this method I want to create a new variable called ENC password for encrypted password and we're going to set that equal to md5 and then wrap the input the post input
alright so very simple this md5 just encrypts this from plaintext to being encrypted so we're not we're not adding just regular text passwords into the database which would be really stupid so
it's easy enough to do so that's going to be Inc password that's what's going to be submitted into them into the database all right let's see let's make this look a little nicer
I take that last comma off all right so there's our data array all right so now what we want to do is create a variable you can call it anything I'm gonna say actually we'll call it query and what
we're going to do here is we're going to use active record which is basically a help our a database helper so that we don't have to write long database strings or SQL strings so I'm just going
to actually pull over the the documentation for the active record class and we can do all different types of crud we can do create read update and delete or select insert update and
delete all right so this the simplest of this class is the is to select fields from the database and we can do this with this DB get alright so here we're just creating a variable called query
we're setting it to this DB get and then the name of the table and it's the same as saying select all from whatever table all right and then we would just say we could return the the result which I'll
show you later on and we can add in where statements let's see join we can join different tables so for instance we're getting data from the
blog's table but we want also join the comments table alright so we'll be doing that as well with the lists and tasks this is this DV where you can basically just say this DV where name equals Joe
or whatever so that's kind of a lot of information but let's just take it slow so what we need to do now is an insert so let's find that alright inserting data pretty simple
we've already created our data array with the table names and the post values so basically this is all we need to do right here so if we just copy that well actually we want to say query
equals that and this will be the users table and actually let's just change this to insert all right and we just want to return insert alright so if this goes through and returns then the user
has been created right so let's save that and this return insert is going to allow us to have this if statement so if insert then you know just give a success message all right so let's save that
and let's try it out you may have a couple errors at first so let's actually just want to make sure I didn't insert anything no you can see our users table is totally empty alright
so user name will stay admin alright so the username needs to be at least 6 characters okay you know what let's change that
so username we'll say has to be four characters in the database I mean I'm sorry the password all right all right so looks like we have an error
and the reason you again this is we didn't load the user model all right so what we need to do is we could just say this load user model but I want to auto load it so I'm going to go to my config
folder and then auto load dot PHP and where it says auto load model we just want to throw in user model all right so hopefully it works now all right so you are now registered and
can log-in so let's go ahead to PHP myadmin and reload the users table and here's our our information we have first name last name email username and an
encrypted password alright so this definitely adds security to your your app and then we have the registration date which is the exact date and time that we made this submission all right
so that is a good example of an insert query so now what I want to do is look at a select statement because we need to log in which means we to validate our username and password against the
database and what I'm gonna do is paste in the login function and I'm pasting it in because we've already dealt with pretty much everything that's here so it's kind of redundant to type it all
out again all right so we're just setting a couple rules here validation rules if the form has not been submitted we're not going to do anything because this login controller doesn't have its
own view it's actually just inserted into the template through the layout so we don't need to show up for you we don't need to do anything alright so if it passes or if it's
submitted sorry then we're putting our post variables into a regular variable username and password and with the registration we did this in the model but you can I only did it this way to
show you that we can do it in the controller as well it's really preference and what we need to do is we're setting a user ID to this user model login user all right so this is
going to call the method login user and in order to to access these in our model we need to pass them through as arguments all right so then we're saying basically this is only going to happen
we're only going to get an ID if the login is correct so here we're saying if the logging is correct then we're creating a data of user or a user data array which includes the user
ID username and a boolean a logged in true or false and we're setting that to true because we validated it against the database and then we're just setting a session with setting user data in a
session so what this means is that as long as we have a session we can check to see if it's true and if it is then we can display data or pages that only someone that's logged in can see all
right we can actually just say if logged in do this if not do this so that's I mean session class is pretty easy and we've already actually used the session class in the flash data function all
right so let's see we can save that and what we need to do now is create the login user function in the model all right so we'll go to user model say public function
logging user and we also want to include the parameters here as well all right so the first thing we want to do is convert the password to to an encrypted password so we can match against the encrypted
value in the database all right so we're just gonna say E&C password is equal to md5 and then the plaintext password all right and let's see we just want to validate against the database so we have
to perform a where clause all right using active record so where Clause we're going to say this DB where and we're going to say where username in the database is equal to username that
we typed in all right and we'll do the same thing for the password except we want to use the encrypted version okay so those are the where clauses that
we want to check now we're gonna say result is going to be equal to this dB yet we want to put the name of the table which is users all right and then we want to say if results num rows is equal
to one there should only be one value one match because we're not going to be able to have duplicate usernames so obviously it'll be one all right so if result num rows is equal
to one then we want to return result row zero because it only be one which is zero because all objects and arrays start at zero all right we want to return the ID that's all we're doing
here is returning the user ID and we want to say else so if there isn't if there's not a role found we just want to return false all right so let's go over this we pass in a username of the
password to the function we encrypt the password we call to where clauses was sitting where the username the database is equal to the username we typed in and the password in the database is equal to
the encrypted password that we type in all right so now here we're saying this result variable is equal to this DB get users all right so this is going to be filled this num rows will be filled
it'll equal one if these match okay and then here we're just returning the the ID row all right so if the users ID is one he'll return a 1 else if there are no
rows found if none of these match then we're just going to return false and back in the controller let's see where our you log in which is right here so what we're setting user ID
equal to whatever we return here if it's the ID it's the idea if it's false then it's false we're saying here if it's not false if it's set then we're going to add the data to a session we're going to
start a session and redirect if it comes back false then we're just going to set a message saying login failed and redirect so in both cases we're redirecting to the home page so let's
save this in the model and let's just see if this is working okay so nothing happened but nothing happened but we don't have an
actual message we need to set a message here so when we set the session I want to set a message and we'll say login success we'll say you are now logged in and then
in home index which was using the home view let's copy that vlog in success all right let's see if that works
all right so we are logged in so obviously we're logged in we want to make it so that this box doesn't show the input fields when we're logged in we want it to just have a logout button
so first we need to create a logout button so let's go to the login view so I'm going to views users log in and we want to check if we're logged in or not and we can use the session the session
class to do this so we'll just say PHP if this session user data if this session user data logged in yeah so that's all actually all we need we
want an else HP else all right so if we are logged in we want to show a logout button if we're not then we want to show the form which I will grab from here cut it and put it in here all right
so let's save and now if we reload we shouldn't see the form all right good so what we need to do up here is just create a logout button I'm sorry not a logout button but a logout form so I'm
actually just gonna pay something in here alright so what this box is gonna show when we're logged in it's gonna say you are logged in as and then what we're doing is calling the username value from
the session okay so whatever the user's name is user name is gonna show and then we're just opening another form here that goes to users slash logout and it's just going to have one input which will
be a button and input button and that's really it so save that all right so now it just says you are logged in as admin now we need to create the controller function Lago
and a little off-topic but I would definitely suggest that you add comments to your cold each each controller I'd suggest you have a little comment saying what it does I'm not doing it just
because for time issues but it's definitely recommended alright so for logout what we want to do is we want to unset all the user data all right so the unset
session data and we can do this session unset user data and then we can have the value which will logged in so we're going to unset that login value and we're going to unset all the set all the
values which are the user ID and the user name all right and then we want to say this session Seth's destroyed
all right so that should kill all the all the session data in the session itself then we want a message let's just copy this here
this isn't going to be an error so this will say we'll name this logged out you've been logged out will redirect and now we just got to catch that flash data
message in the home view and change this to logged out all right so let's try this out if we reload log out all right it logged us out just didn't show the message
so with logged outs logged out oh you know what I think it has something to do with killing the session in this function so I mean we can tell
when we logs out because of the box here but I think that it's messing with the flash data when we kill a session so we'll just keep it as it is and just have I mean obviously we're logged out
now so that's really it actually one more thing when we're logged in I don't want this register link to show I wanted to say hello and then our username so that is in the layout the main layout
right here and I'm just gonna paste this in okay so if we're logged in it's gonna say welcome if not it'll have the register link so let's reload and you can see it says welcome head okay if you
log out register all right so our login and registration system is pretty much done what we need to do next is create the lists list and task functionality
Here we will add an authentication system to our CodeIgniter application
The full source code is at – https://github.com/bradtraversy/mytodo_ci