PHP-and-MySQL/C4/Sessions/Gujarati

From Script | Spoken-Tutorial
Revision as of 14:44, 21 January 2013 by Krupali (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time Narration
0:00 Hello and welcome to this tutorial on php sessions.
0:05 Sessions are quiet similar to cookies.
0:08 However sessions only have a temporary time - expiry time.
0:12 They will be destroyed as soon as the browser is closed - all the connection to the page is lost.
0:19 So sessions are not like cookies because you can't set a particular expiry time.
0:24 And they are not stored in the same way.
0:28 I mean, the "id" of the session can be stored in the cookie.
0:34 Or you might have seen in a browser URL something like this.
0:40 I don't remember the name - something equals and a lot of numbers and alphabets as well.
0:47 So basically sessions are very similar to cookies.
0:50 However they are not stored for a long period of time - only until the user closes the browser.
0:57 Okay - So sessions are different.
1:00 First of all, we need to declare or call this function called 'session_start'.
1:09 Now this needs to be at the top of every page that you use sessions in.
1:14 So if you don't have this and you are trying to echo out a session value or you're trying to set a session, it won't work.
1:22 You need the session start code there.
1:24 Now I'll show you the error that comes up when you don't use this so you'll remember it.
1:30 Creating a session is very easy.
1:34 Use 'dollar underscore session' and in square brackets give the name of the session.
1:40 I will type name and equal this value to something.
1:44 It could be a string data or the new written data.
1:48 Okay so our session is set here.
1:50 Lets run this for the first time.
1:53 So lets refresh.
1:56 Okay nothing has happened.
1:58 I'll comment out this code like I did in my 'Cookies' tutorial.
2:01 If you haven't seen that please do so.
2:04 Next I'll echo out the value of the session that I have set.
2:08 So that's 'name'.
2:11 Please note that this is not going to be executed.
2:15 This will be on a completely new page, for all you know.
2:19 But here I'm just starting my session.
2:21 I'm getting a session called 'name' that has already been stored by our server.
2:26 So lets refresh we can see that is equal to 'Alex'.
2:29 So you can add this and this code onto any page.
2:33 So you can have your session start and echo your session name onto your page as long as this has been started on any page of the current session of the browser.
2:44 So for example if I create a new page, add my php code here and say session start.
2:49 And then echo out the session 'name'.
2:56 And I'll save this in my sessions folder as new page or new dot php.
3:03 So when we come back to our page here and we click here, we type new dot php.
3:10 We get exactly the same value, even though we are not working on the same page that we created our session in, we are still able to access it.
3:18 However if I was to close my browser and reopen it, this session probably would not exist.
3:25 Hope that is clear. Now let me show you what happens if you don't put the session start in.
3:31 You get something similar to this.
3:33 Lets go back and check it.
3:36 What happened here is that we are not getting any output because we haven't started our session.
3:44 When we type 'session_start', you can see we get our value as output.
3:51 The reason I don't have an output is because I don't have that type of error reporting on.
3:56 But if you did have a specific type of error reporting on, I have a tutorial on that as well, then you would probably get an error.
4:06 So you can close this now and I'll show you how to 'unset' a session.
4:10 There area 2 ways to do this is.
4:12 Either unset and then session in brackets to unset our session.
4:16 Or use a completely different command altogether and that is 'session_destroy'.
4:27 And the difference between these two commands is 'sessions_destroy' will completely destroy sessions that you currently hold.
4:35 And 'unset' will unset a particular session.
4:40 So its your choice - You can log the user out and say 'session_destroy'.
4:46 That would clear all current session variables that you hold at the present time.
4:50 Or otherwise you can just 'unset' a specific one.
4:53 So what are sessions useful for?
4:55 If you've come into a website and see a box like 'Remember me' and you don't check this box, then you would probably be using sessions.
5:03 Because once the users' browser has been closed, you would be logged out.
5:09 And when you come back to the website you'll have to re-type your details like your user name and password to login to the website.
5:17 But its different if you are using cookies because you set an expiry time - which means that your username will be logged in or this cookie will be held until you decide to destroy it.
5:30 And we have to create a code to destroy our cookie, like I showed in my 'Cookies' tutorial.
5:35 So really its your choice whether you use sessions or cookies.
5:40 Sessions are good for short term - Cookies are good for long term - specific amount of time that you want a piece of data to be held.
5:49 But if you've gone through my php Project - 'Register and login', you'll see that I use sessions.
5:56 This is because I need to use sessions when I'm creating tutorials.
6:00 However you can take any form of this.
6:03 It could be a cookie, it could be a session, it is really your choice whether you want to keep the user logged in for a long period of time or not.
6:11 So if you've got any questions on this, feel free to contact me.
6:16 Make sure you subscribe to phpacademy.
6:20 Thanks for watching. This is Evan Varkey dubbing for the Spoken Tutorial Project.

Contributors and Content Editors

Krupali