28: How to Start a Session in PHP | PHP Tutorial | Learn PHP Programming | PHP for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're going to talk about something called a session inside PHP and the session is a little bit different than a session variable which we talked about in the last episode now let's pretend for a second that I want to create a session variable and I want to access this session variable and use it inside my page here now in order to do that we're going to have to have a session running inside our page because otherwise we can actually gain access to our session variable on every single page inside our website and an example you guys might want to use this let's pretend for a second that we have a website with a login system and I type in my username and my password and then I hit login now if I did actually you know choose the right username and password and I want to log in now in order to actually you know make the website remember that we're locked in we're going to have to save our username or something that our website will remember inside our session otherwise the website can actually remember that we're locked in so to give you guys an example let's actually go ahead and create a session variable inside my document here called index to PHP and I do want you guys to bear in mind that I do actually have a navigation in here and I have another page called contact the PHP so if I do actually go ahead and refresh my browser you guys can see we just have a navigation in here at home and I have contact and I can click between them if I want to now going back to my front page I'm going to go down to my body tag and I'm going to create a session variable so I'm going to say variable underscore session brackets and I'm going to set it equal to some kind of string now inside my session brackets I'm going to call this one username like so and it's going to be equal to Danny nine four eight a just because that kind of looks like a user name so now if I do actually go underneath here and echo this thing up here you know the actual name of the session like so inside my front page you guys will notice that we get Danny nine four eight a but if I were to go into my contact page now you guys can see we get nothing well we do actually need to include the echo inside our contact page of course so if I go into my contact page paste it in we should be able to you know spit out our username but we get an error message instead and that's because our website doesn't remember our session variable called username and we need to do that in order to be locked in so a way to get around this is to actually have a session started that saves our session variable and allows for us to access it on every single page inside a website so what I'm going to do in order to do that you know to actually start up a session is to go at the very top of my page here and create a short piece of code I'm going to start off my PHP tags at the very top and inside the PHP tags I'm going to say session underscore start parenthesis now that I have these I will actually have my session variable saved inside my session which I started at the very top of the page here so if I go ahead and copy my session variable and paste it on top of my contact page I should actually be able to remember the actual user name so if I go back to my home page you guys to see okay now we just set it as Danny nine four eight a if I go to my contact page now it's in there as well so using the session start function we can actually start up the session on every single page and set our website and it will remember our user name which we need in order to be locked in so just to give you guys a small tip for creating session starts now a typical thing to do would be to split your you know website into different documents for example am i split my entire header inside a document for itself and then link it or maybe included on top of my pages with an include function that we do actually have inside PHP and by doing that and then starting up the session inside the header the PHP but do actually have a session running on every single page that has a header attached to it inside our website so we will automatically have a session started every single page you don't have to copy paste it in the page just like I just did here but I'm Lacey in this episode so I just kind of did that instead so this is the basic idea behind sessions and this is actually very important if you want to create something like a locking system because without a session you can actually be locked in or at least only on the front page or wherever you actually do log in so you need to have this word to remember that you're locked in inside the whole website now just to show you guys and this is more of a bonus thing but an example of you know being locked in could actually be to create for example a let's say we have the front page here and I make a check that says you are locked in so I can actually go down here and say if and execrate an if statement whoops that was too many I can say if is set inside my condition in here I can say if is set and we have something called a session username set like so then I wanted to do this down here now also let's say for you guys always check for errors first so I'm going to say if the username has not been set then do this otherwise we're going to do something else underneath here so if the username has not been set inside our session then echo you I'm totally screwing up here you are not locked in now underneath Dave statement I'm going to say else and I'm going to say echo you are locked in so this would actually be an example of if you actually locked in you display one type of content inside your website if you're not locked in you're going to display another type of content so I'm going to go and save this go to my website refresh and let's actually make sure we have everything like it should be well sign out in the right page there we go I'm inside my front page now and does actually say you are locked in because we do X have this one set inside our session now if I were to actually go in and delete my session here like so and actually delete my session start up here like so refresh the website it does exactly say you are not locked in because we are in fact not locked in but now we do actually have a session you know set in here again and it will say you are locked in so and again this was more of a bonus to show you guys you know to give you guys an idea about how the whole locked in thing works now we're actually getting to the point where we can start creating a login system and we will actually do it after a couple more episodes we just need to talk about a few more things such as how to actually use tables to create something like a locking system where you type in your username and password and we also need to talk about how to create an actual user table inside a real database and that's going to be a very short episode we're not going to go very in-depth in databases in PHP but it will actually at some point start up a tutorial series on something called SQL and databases where we will actually talk about how to set up you know complicated databases so hope you guys enjoyed this episode and I'll see you guys next time
Info
Channel: Dani Krossing
Views: 254,374
Rating: undefined out of 5
Keywords: session start, php session start, session start php, php session, php session tutorial, session php, php start session, session in php, php session login tutorial, php sessions tutorial, session start in php, php sessions, session tutorial in php, how to start a session in php, create a session in php, PHP tutorial, php, mmtuts, php programming for beginners, php for beginners, learn php, learn php sessions, learn to start a session
Id: 3CS-eQdcMLU
Channel Id: undefined
Length: 8min 6sec (486 seconds)
Published: Mon Nov 09 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.