Code Igniter - Login and Session handling

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello what's up everybody how is it going welcome to another video of tutorial series learning PHP I mean learning code ignitor from scratch in previous video we covered how to load the view from the controller and also how to load the sub views from inside views and we also learned how to generate a dynamic URL for the links right we learned about the helpers and how to load the helpers using auto load and I think that was it for learning the basics about code ignitor now what we can do is we can plan a simple app and start developing it from the scratch so that it becomes easier for us to understand code ignitor using real time tasks so what we'll be doing today is we will be building let us say a simple news application we will be adding editing and deleting news from the admin panel and we'll display those news in the front-end I hope that will at least give us a basic scenario on how to use various libraries various classes various functions of CodeIgniter so log in itself will include the session handling process and news module will include the crude operations involved in mmm code igniter so let us start building our app so let us today start with a basic admin module so we'll whenever we type localhost let's see I slash admin it should open up our admin panel which will come in a kit which will first of all load the login screen and whenever we type the correct username and password as per our database it should allow us to load okay now in order to do that what we can do is let's just open our sublime first perfect our CI is already there let me close everything the few things that we are going to do today is first of all build our admin panel so in order to build our admin panel we can create a folder called admin side our controller it's a new folder admin ok so all of our codes related to admin panel will be inside our admin folder and that way we can separate our front-end with our back-end so first of all I would like to create a controller called login inside our admin panel okay so simple class class login extends see i underscore controller so here's our controller and it's index function should load me a view file with login page with login form right so I'll just load a simple view over here and further for loading loading the view I need to create a view first and what I want to do is I want to place all the views of admin panel inside admin folder so this way so inside admin folder now I would like to create a login dot PHP okay so I have login dot PHP for my admin panel and this field I want to load inside my index function of login controller so what I want to do is if it was just a file I would have written login but since it is inside admin folder so I should say admin slash login so this means I need to load the login view which is inside admin folder so if you see in my views folder I have call admin and I'm logging the law I'm loading the login dot PHP so let's say this is a login page so I need to create a simple HTML document and I would also like to use some bootstraps so maybe we can download a new bootstrap or maybe I can just grab it from local projects which we did last time from previous playlist we had a folder called PHP space shells PHP spacious which is inside www PHP specials here so I need I just need to copy this and paste it inside my C I which is here so you can paste it on the root of your project which is just inside my CI folder I can see bootstrap is here inside my bootstrap I have a dist folder and scientists we have CSS and I just need to link this bootstrap CSS inside my document so title would be admin - login ok so in order to link the bootstrap or CSS I'll just write link href now in inside our href in previous in previously while we worked on core CI we used to give some let's say absolute path or absolute URL or maybe relative URL but here since CodeIgniter does not work on folders on on the basis of folders or on the basis of files it works with the URI segment so our part to the CSS file is localhost less CI which is our base URL and we need to get inside bootstrap and we to get inside this CSS and bootstrap CSS so in order to generate our base URL I just call a function called base underscore URL you can also use site URL if you want and pass the parameter inside it but for linking files are generally prefer base URL so base URL and it has slash at the end so I don't need to place another slash after the base URL I need to get inside bootstrap and get inside dist and CSS then we will get our bootstrap dot CSS right so we have linked our bootstrap perfectly now I want to create a div called and give it a class called login and also I would like to give a class called cold-ass MD - let's say for so that it only occupies the width of let's say 4 by 12 one third of the entire page and I want to bring this day of to the center having equal space on the left and equals space on the right so I can just add a class called MX - Auto and that will bring the entire tip to the center now I want to create a form inside the Steve my login form so my method would be post then my action would be let's say now since this is coordinator we don't give the path to our file rather we give the path to our controllers function so what I'll do here is so site underscore URL and pass the parameter called admin slash login for the login controller so this is our admin folder inside our controller my login controller and Simon login control I would like to create a new function called verify that would be used to verify our login so what I will do is I'll create a new function called verify so function verify will be the place place where we'll be writing our codes for the verification okay now I'll let you create some form groups class equals to form - group I'd like to give a rule class equals to rule I like to give to Dave's one deep for let's just one day maybe I don't need a rule I just need input type equals to text name equals let's say username for the username and let us also give a placeholder saying username and that's a class called forum - control these are all for the CSS proposed styling proposed you don't have to worry much about the classes so input type becomes - password for the password name would be password and password and my placeholder would be password and my class would be forum - control finally for submit button I'll give a clear input type because to submit name equals to let's say submit and value equals to login and a class called etn and BTN - primary may be so let us see how our form looks like so after we go to admin / since we are inside admin is a folder name so let us give the controller name and after typing this match it will automatically load the index function so it is now loading our let's say what's this this is a form login form it does also give a heading called login so h1 let's say admin login ok so you can see it's there why don't we just give a text center class text - shinta so that everything is aligned up in the center okay maybe we can get also get and get some spacing here by actually we had to place the form group for each and every input so one form for username another form group for class equals to form this group for password and another form group for button so now we have little bit clean I know for all those who are designers out there this might not be looking so good but as a developer we can start with this so this is good enough for us to start with so we have designed our login form now once we submit this it actually submits the data to login dot PHP okay so logins so login dot PHP verify but what I told you is one we don't we don't generally tableau admin slash login so once ever we once we hit as less admin if we are not logged in it should take us to admin login and if we are already logged in it should take us to the dashboard okay so what I will do is I will just write a little bit of route which is inside application and inside config and routes okay so what I want to do is I would like to introduce you new way of defining our URL so route inside the index we can just type in whatever we want to denote our outwait so if somebody types admin in the route after the base URL if there is a beam like this okay so after the base you are localhost last CI we have just we just have admin so if somebody tries to access admin after the base URL so we can say this means admin login okay so this means admin folder login controller we can also give the function if you like so index so admin actually means admin login index so this way we are defining our own URL so this is this is actually denoting that if somebody types in admin this is equivalent to admin login index now if you see if you just hit admin now it is opening admin login index because as per our outs slash admin refers to admin login index so this way we can create our own URLs using code igniters routes okay now since this is submitting our form to admin login verify so if you see here it is submitting our form to admin control and login admin folder login controller verify function which is here so what we generally did previously in Col PHP we grabbed the username and password using post right so we will do the same thing here but this way so username equals two dollar underscore post username as per our previous approach but dollar underscore post username is not how we do it in core igniter it will still work but d'harans who post has lots of compromises for example we need to trim the username we might have to check for their school injection and maybe use MySQL real escape string in order to get rid of SQL injection so in Corinne ITER just a function will take care of all these things and that is this input post so this is exactly equivalent to Darin scope post but instead of passing the index we now pass the parameter to this function the parameter is the name of the input which is username so if you want to grab the user name from the form you just type this input post and it will take care of all the stuffs like my SQL injection and trimming maybe so I don't think it will trim but it will take care of my SQL injection so we don't have to clean up our input anymore so we grabbed our input we similarly would like to grab our password so this input password there are also like process of validating the form which I don't want to discuss right now so I just want to guide you about basic cruder operation checking from the database so we have username and we have our password now what I want to do is I want to check if this username and password existence inside our database table so first of all let us create a table for admin let's just call the table add mints okay and maybe we can have one you for ID username password and let's say for name okay so ID name username and password so name would be back our username would be var car by car and password will also be Barker just give the length to 5 5 to 5 5 to 5 5 for a car now let us make this auto increment and primary goal save it it does also make a dummy entry for now just to check the login so I would like to insert few name name would be my name unwarily my user name would be admin and a password would be something which i want to encrypt so it is just in generate an encrypted password we can use md5 you can use sha-1 or you can use other advanced encryption but before now I'm just using md5 so in order to generate an empty file just like to create a new file maybe save it in somewhere else not in this CI so maybe I would like to save it inside www and just name it let's say a dot PHP I already have okay let's go replace it PHP echo I just want to generate md5 for let's say 1 2 3 4 5 6 ok so if if you want to generate the md5 for one two three four five six just say md5 is a function name so just pass the parameter like this but just be aware that since our input grabs the format in text format so you also have to place the code in here because md5 for the number and the text is different so just type in code like this so we are generating md5 for a string not number so 1 2 3 5 sees a string so I am placing it inside code let's see what's the md5 4 1 2 3 4 5 6 çal host less a door PHP and so this is my md5 let me just copy it and paste it over here so this is my password one two three four five six in its encrypted format in md5 so my username is admin and the password is one two three four five six okay so what I need to do is I need a check maybe I would forget it so I just write it in a comment username here's my admin and my password is one two three four five six okay so what we what we need to do is once we grab the passo from the user we also need to encrypt it so that we can check the encrypted password in our database so let us just encrypt the password provided by the user by just passing it to md5 so we have the encrypted password and they username okay so this way we are also guaranteeing the user that we are not accessing their username and password as in the user the password submitted by them is encrypted so even an administrator will not be able to say it okay so empty file secondly since now we need to communicate with database there is one more thing that we need to do and database is something that is really often used in the project so we generally autoload it but if you don't want to auto load what you can do is you can just load the library wherever you want so we have a library called database inside CodeIgniter which can be used only after we load the library for example if you want to load the database what you need to do is this just like you know the view this load library okay then you can pass the name of the memory which is database so after loading the library database now you can use the database throughout this function but since database is very commonly used we can just auto load this library just like helper like like last time we discuss about the helper helper is a set of functions in a common file similar database is library now this is not a set of just set of functions but it is an entire class so lever is generally a class with lots of function in it which makes our task really easy we can perform really big jobs in few lines so lots of cores are all written for us inside libraries so what we can do is we can just or to load that database library so libraries are here we can just auto the library data base and since we are season is also library and we are now about deal with season so I just can just a comma separate and load another library called sis and over here so we are ready to use the database library and sis and library throughout our website now so what I'm going to do is I want to check if my username and password is there in the database now as you remember in our first class of this playlist we discussed that all of the things related to database is done inside our model so what I want to do now is I want to create a new model inside my models folder and more are generally named after that table tables name so if our table is admins I generally my practice is writing the table name in plural writing the model name in singular and controller can be as you wish but I generally write controller and model in plural form and I write the model controller and the database name table name in polar form and I write the model name in singular form and that is maybe the standard practice of all the frameworks so you can practice that from now so inside my models folder I'll create a new file for that table which is admins table so I'll just call it admin dot PHP this way so now and this would be capital the casing of model name should be capital admin ok let us create a model so class admin that this will extend not CI controller but CI on the school Moodle this is the difference between controller and model now all the tasks related to admins table will be dealing inside this mullen now we can create our own functions here like for example function validate okay so this will what this will do is actually in our controller we grab our username and password we can grab this inside our Moodle ok so what we will do is we'll just call this but first of all we load our Moodle so load what the model will just write model and we load it by its name so our name of the model is admin so now while loading the model will be using lowercase not uppercase okay so this load model admin now we can use all the functions defined inside admin mode inside this function because we loaded the model inside this function right so what I want to do here is I just want to let's say check create a function called check and this and we'll I'll assign something that is returned by this admin model and my function will be validate validate okay this way so this admin validate some admin model is here now I would like to validate so I'm first of all grab the username and password and check these these password exist in the database so what I'll do is now since we have always loaded our database library I can use the functions of database library now in order to check if something is and there in database that means running select all from the admins where username equals to this username and password equals this password what I can do is I can say this TB get under school where so get the SKU here is a function that allows us to pass in some parameters then one of its parameter is the first parameter is table name and our table name is admins the second parameter is my conditions inside array so my username should be username and a password should be password so instead of doing this we generally write this in area for meso a RR and field name should be inside array so a RR basswood should also be inside area so this means now if I pass in array over here what this will do is select all from admins where username equals to this username and password equals this password so all of the field that I want to check from the table name I need to pass it inside array in this format so after passing the area it will automatically check where username equals this and password equals to this okay so I just grab it now in order to fetch it I just need to write a result all row so if you want multiple result you have to write result but since I know the combination of this username and password cannot be multiple what I can do is I can just write rule so Row is generally for fetching single entry and result is for fetching multiple entry in array format so since now I'm just I'm just trying to check if any of the one row has the username this and password this so instead of writing a result I'm writing a row so I can just check fetch it and return it so if this fetches any of the entry from database it will return that entry and if it does not fetch any of the entry or does not find any entry it will just return false so I need to check if this is false or has some entry so what I'll do is if it has some entry it will of course validate if but if it is returning false it will not get inside if rather it will get inside else right so if I find saying any injury it will get inside if but if I don't find any entry validate will return false and I need to say invalid username or password which I'll control later on all let us just do it right now it has just get back to the login page login index and load the view again so if something is wrong it's a username and password is not correct I need to redirect to admin back to index right so what I'll do is redirect to admin so admin so just calling the Jetta function will redirect you to the respected place so as per our routes admin means admin means admin login index so by just writing admin I'm actually redirecting the user to add admin login index right so let us see mmm if everything is working fine so if my username and password is incorrect okay like this it's again take me and get me back to the same page so I had me log in there to see and I'm back to admin again because my username password was not correct so let us try to type in the correct username and password and check to see where everything works fine so let us say if if the username and password validates and returns some entry I just want to echo the username correct credential okay so I just want to echo correct in Korean chills if it is correct and if it is incorrect I just want to get it back to the admin let's see so incorrect and back to admin and if it is correct which is admin and that's one two three four five six login it is same correctly credentials that means it is working fine so when you are typing in the correct username and password and this is taking me back it is displaying me echo correct credential so what I'm basically doing is first of all for in order to check to communicate with database I created a modal call admin inside my models now what I'm doing is I'm loading that model inside my verify now I can use all the functions and of admin modal inside my this function so what I did I created a function called validate which grabs the user name perhaps a password from the user and check to see if that username and password is is in the database and whatever it finds it returns so it finds if it finds any entry from the database it will return that entry and if it does not find it will return false so I'm checking to see if this is false or true so if this finds some entry I'm just writing eco correct credential and if it does not find any entry I'm saying read it back to the admin now instead of displaying correct cadential i want to redirect the admin to the dashboard redirect the one who log is trying to log into the dashboard so I would like to create another controller called admin and dashboard so inside my admin folder I want to create another controller called dashboard ok so inside my controller inside my admin I want to create another controller called dashboard and take the user to the dashboard let me just quickly copy the login controller and piss it here so I don't have to write so much so I just replace login with dashboard and inside index I we don't need very fine set dashboard and just want to load a view called admin dashboard ok so inside a view file I want to create another file called dashboard or PHP inside my admin folder that is just display this is dashboard now we receive if if we are doing everything fine so let me just verify this again so as per our code now it should take me to the dashboard view and it is saying admin dashboard is not found let's see why did indeed find admin dashboard because the spinning of dashboard here is incorrect so it should be - both submit and catch after s right so dashboard let us now check if it is working it's saying unable to load the trick use the file admin dashboard PHP let's see again something must be wrong with our again we missed out s H after s inside our view file just add H now let's try to refresh it again now it is saying this is dashboard so everything is working fine but before taking it back to dashboard what I want to do is I want to check the SIS and I want to create a session and then take a dashboard before that I want to create a constructor here let's function underscore underscore construct for the constructor and I want to do what I want to do here is if somebody is already logged in I don't want to show them I don't want to show them the login form or verify their login because they are already logged in so what I'm going to do here is I want to check if somebody's already logged in and if they are all wrong already logged in I want to take them to dashboard similarly inside that's where I want to create a constructor so public underscores who construct inside my dashboard and what I want to check here is if somebody is not logged in and they are trying to access the dashboard I want to take them back to login so these are two things I want to do but before that I need to create and Sisson and before directing them to the dashboard so this in all the criticism we have a function coordinator that says this Sisson sit under school user data so user data is set user data is a function of CSUN library now we are if you remember we have already Auto loaded library called Sisson here so now we can use all the functions of sass and library inside in our entire site so I'm just checking if I if the username and password is correct I want to set a session called admin and place a value any value maybe let's say for now I would like to place the value let's say m1 inside my admin ok we can actually place the value of the and the value that we just fetched from the database to validate but for now just to check I just placed one here so if the username and password is correct I'm setting the session for that user okay and what I'm trying to do here is I will just want to show and we just longer than getting desperate but it could get back to admin we are still we can see still see it says undefined property loading admin login 10 let's see admin login 10 so this load view admin login ok so after you create a constructor what you need to do is we need to extend we need to mention the parent of this constructor and the parent is the constructor of CI on a sub controller so we need to do is underscore underscore construct okay so this is must for coordinator if you are defining a constructor what you need to do is you need to mention parent underscore mass or constructor otherwise your functions might not work properly now let's see see if I am if I am trying to access the login even after I have logged in it is taking me to login and even if I type dashboard without logging in it is taking me dashboard so I need to handle this um session over here so what I'm doing here now is after somebody submits the login button I'm creating a system for him for him and my session is admin and espero is one now what I want to do inside my dashboard is if somebody is not logged in I want to take him back to the login and inside my login if somebody's already logged in I will take into the dashboard so what I want to do is I would like to check if dollar this sis and now I just say user data in order to get the value of the session I just need to use it at a and in order to set the user data ID to say set under squeezer data so here we are generating a creating a session and here we are checking the season so I'm just writing user data over here so I'm trying to check if the user if the season called admin exists so if the season called admin already exists I want to take this user to the dashboard this way right so I will take the user to the will add user to the dashboard similarly inside my dashboard I want to say if not dollar this CSUN user data and the maintain user data is admin so it says an area is not set I want to redirect the user back to admin which is logging right so if so it should be administered no distress would so if the user is already login I need to redirect the user to admin / dashboard and if the user is not logged in I want to let the user to the admin okay now let us try to access the dashboard since the system is not set yet so if I try to submit it it is taking me to the admin so now it is not letting me to go to dashboard see it is taking me back to that having because in the constructor of dashboard we have checked if the system does not exist take it back to the login now since the season is not set yet let us create a session by passing the Kollek username and password admin and 1 2 3 4 5 6 it is login now it is taking me in dashboard now if I try to get back to the login page it will force and relate me to - but because my season is already set so this way we can handle the session in code ignitor by creating the session and in the constructor what we can do is because constructor is a function that gets called before calling before any other functions get called so inside a constructor what we are doing is we are checking the session if the admin session already exists and if it exists we are taking to dashboard and inside out that's what we are checking before anything else if the session does not exist take the user back to the admin or the login page so this is it for today I think we have learnt quite a few things we have learned how to grab data from the database we learn more about about the crude operation tomorrow and how to after fetching it how to get the values of the different fields using the variable how to use loops for the multiple entries today my my main intention was to cover up the session library and just basic of database library which we have covered I hope you understood what we discussed today and if it did not if you find anything confusing you can mention it in the comment below and if you liked the video don't forget to hit thumbs up button and if you have not subscribed yet click on subscribe button and don't forget to hit on the bell icon so that you can receive notification of all the videos I upload I hope you enjoyed the video and I will see you guys tomorrow with a new video with a new topic till then have a nice time goodbye [Music]
Info
Channel: Kodiary Technologies
Views: 32,867
Rating: undefined out of 5
Keywords: PHP, Code ingiter, Codeigniter, Code Ignitor, Codeignitor, Learn Code Igniter, Code igniter from scratch, Code igniter from basic, Code igniter session, code igniter session handling, code igniter login, Kodiary Technologies, Kodiary, Anwar Ali, Code igniter admin panel, code igniter session library
Id: 8tgZHNBp070
Channel Id: undefined
Length: 37min 46sec (2266 seconds)
Published: Thu Mar 29 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.