PHP Front To Back [Part 16] - Sessions Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to your next PHP front-to-back video in this video we're going to be looking at PHP sessions so sessions are a way to carry information or data across multiple pages I'm sure you know by now that if we set a variable on one page it's not going to be accessible on the next okay that's where sessions come in now unlike cookies session data is not stored on the user's computer it's stored on the server okay we'll also be looking at cookies later on so let's jump right in here I have my PHP sandbox folder open here I'm going to just create a new folder and let's just call this website 4 and we're going to have a few pages here I'm going to create one and call it page 1 dot PHP all right now you can set session variables however you want but I'm just going to put a form here that we can submit and then we'll save the values to a session ok so let's just put in some standard HTML tags and put a form down here I'm just going to grab it and paste it in one second all right so we have a form and as the method of post we've gone over all this the action I set to serve our PHP self so it's going to get submitted to this file we have a name field and an email field okay so let's go up here and let's catch this form submission ok so I'm just going to say if is set and we want to look at post submit because I gave the the submit button a name so we can check for that and then let's just test it out by just saying echo one two three all right so I'm going to go to local host PHP sandbox slash website for splash page one dot PHP okay so there's our form if I submit it it's going to echo out one two three so what we're going to do is catch the values and put them into session variables now in order to work with session variables you have to start a session on every page every page that you want to use that data you have to do session start all right so let's discussion underscore start and that will start the session alright and then we're going to just um set a session variable using the session superglobal and let's set name and then I'm going to set it to our post name value alright and just to make it a little more secure we'll use HTML entities around that alright and then we'll do the same thing for the email so I'll just copy that let's set that to email all right so it's taking the post values and putting them into session variables now I also want to redirect to a new page and I don't think we've gone over this yet but there's a function called header and that will allow us to redirect from PHP ok so what we have to do is say header and then in here we want to say location colon and then whatever page or link we want to go to so I'm going to say page two dot PHP alright so let's save that and then we're going to go and create page two and then I'm just going to put some HTML tags in here and let's just put actually we'll go up here first and put the PHP in okay and then what I'm going to do is again say session start and then we're going to set name equal to the session variable name and same with email okay and then down here we'll do a little message so let's just put an H five and I'll say thank you and then we just want to echo out the name and then we'll just say you have so subscribed with the email and then we'll echo out the email alright so let's go ahead and save that and just going to reset this page one and let's just put something in here we'll just put my name and for the email I'm just going to say test at test comm and let's submit and we get thank you brad traversée or email or you have subscribed with email test at test comm so we carried the variables over to this page and if we reload this without making the post request you'll see it's still there it's stored in the session so what I'm going to do now is put a link down here and let's set this to go to page three dot PHP alright I'll just say go to page three okay and then we'll create page three and in here let's again we'll set a name variable I'll just grab it from here now I want to show you what happens if we don't use session start all right so let's just put out we'll just put an h1 here and let's just say hello name all right so let's save that and then if we reload this we go to page three you see we get this undefined variable session okay it's doing that because we didn't include session start so let's do that up here and reload and now we get hello Brad Traverse City okay so make sure that you always put this at the top a lot of times you could you put this in a some kind of configuration file and then you'll just include that at the top of each page that you want the sessions now we can change these anytime we want so if we go to page two and right here we say session name and we set that equal to John Doe and then let's go back to page two and you can see it says thank you John Doe and if I go to page three that that variable has been changed all right so you can update it at any time okay and if you want to see what's in the session array you can simply use a print our statement so let's go to page three and then we're just going to let's do print underscore R and let's take a look at session reload and you'll see that it gives us the array with the values inside now if you want to unset one of these you can use session unset so let's go ahead and create a page for and then in here put our PHP tag and then we'll do a session start and if we just want to unset one value let's say the name then we use unset just unset and pass in session name okay so let's save that and then we're going to run page four okay no output but let's go back to page three and now you'll see that we get undefined index name the email still there but we just unset the name okay so if we wanted to get rid of this error what we could do is go to page three and right here let's say is set wrap that we use a ternary and we'll say session name okay if it's if this is set then we want to put this inside of the name variable and then if not okay if not then I guess we'll just say guest alright so reload and now we get hello guests because there's no session name now if you want to completely destroy the session you can do that with session destroy' so let's go back to page four comment that out and then let's say session web session destroy' okay so we'll go to page four and now if we go back to page three you can see that we now have undefined index email because there's nothing in the session array and again if we wanted to get rid of that error we could do the same thing and for this we just say something like not subscribed alright so reload and the air is gone alright so that's going to do it for sessions they're pretty easy just you just need to remember to do this session start and and do that on every page that you want to use sessions on all right so that's going to be it for this video guys thanks for watching and I'll see you in the next one
Info
Channel: Traversy Media
Views: 73,705
Rating: undefined out of 5
Keywords: php, php sessions, sessions, php tutorial, php front to back, learn php, php header, php redirect
Id: W4rSS4-LdIE
Channel Id: undefined
Length: 10min 30sec (630 seconds)
Published: Thu Mar 23 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.