PHP-and-MySQL/C4/Cookies-Part-1/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:00 Welcome to this tutorial on php cookies.
00:04 Cookies are a very important part when creating special websites where you store information about a user.
00:11 The definition of a cookie is - a set of data stored on your computer or the user's computer by the web-server.
00:18 This means that when we go to a website, our details are stored and are used when we visit it again, provided we select an option like 'Remember me'.
00:30 So that you don't have to keep logging in.
00:32 But if you didn't check a button like 'remember me', you will probably be dealing with sessions which close as soon as the user closes the browser.
00:42 So, sessions are killed straight away; however, cookies are stored for later use.
00:50 So let's begin right away and see how to create a cookie.
00:53 You do this by using the setcookie() function.
00:55 The function takes 5 parameters but I will use just 3.
01:00 The first vital one I will use is the name of the cookie which I will set to 'name'.
01:05 The second one is the data that needs to be stored inside this cookie and I'll type "Alex" here.
01:12 Now the next one's a bit more tricky.
01:15 It's the time in which it expires.
01:18 Now this needs to be set in seconds.
01:21 And to represent this, I am going to create a variable called '$exp' for expire and this will be equal to the time().
01:28 Let me add some value here.
01:31 At the moment I am adding zero.
01:33 So, if I echo this out and get rid of this cookie function for now.
01:39 I'm just echoing out the time here just to show you what it does.
01:43 So, let's refresh. So you can see quite a lot of digits here.
01:47 Now this is the unique time-stamp.
01:50 And the unique time-stamp is the number of seconds before January the 1st 1970.
01:56 So, January the 1st at 12 am ummmm...... in the year 1970.
02:02 So you can see it here - I think the number of seconds in here equate to a date in the future.
02:10 So for example, at this moment you can see this 88, now 89 and as I keep refreshing, by every second this increases.
02:20 So yes, this is quite useful way of adding a specific value here.
02:28 Now we need to find out the time in seconds of a day because I want this cookie to expire in a day.
02:34 So, I multiply 24 by 60 to get the number of minutes in a day.
02:39 And then multiply the answer by 60 to get the number of seconds in a day which is 86,400.
02:47 So, if I replace zero with 86400, we have the variable "expire" that now holds the time in the future by a day.
02:56 To save time, I am copying this and I will add my 'expire' variable here.
03:02 So, this function will set our cookie called 'name' with a value of 'Alex' and it will expire in a day - read in seconds using the 'time' function here.
03:13 So let's refresh this page and hey! we can see we have no errors which means it has worked.
03:19 Now what I'll do is I'll use block commenting to comment out all of these.
03:23 And below this I'll echo out this cookie.
03:26 But the reason I have commented this is because you don't need to set a cookie every time the user comes into the page.
03:33 If you are using a log-in script and you let the user log into your website, you would need to issue this only once and then the cookie will be stored.
03:41 And you could use it within this time that we have set here.
03:46 So, what I'll do is I'll set echo and I'll use a dollar sign, sorry, 'underscore cookie'.
03:52 Inside here is the name of the cookie so I'll type 'name'. Refresh and you can see 'Alex'.
03:59 You can check it out. Even if I close my browser, restart my computer and came back into this page, it will still read Alex because it's been stored into the computer.
04:11 Okay, now if I had to set another cookie, let's say, I set another cookie here and this will be 'age' and my age is 19.
04:24 And my expiry time I'll just keep as this.
04:29 So, let's just put this up here.
04:31 We can replace the block commenting with line-comment to keep it neat.
04:36 So, for our expiry time, I'll set another cookie in here.
04:41 And that will have the same expiry time. Let's see if I can get it right.
04:46 Okay, so we'll get rid of this.
04:48 We’ve set another cookie with the same expiry time.
04:51 Let's refresh. Okay that has been set.
04:55 So, what I'll do is comment this out and here I will echo it out.
05:01 So, you see we can set more than one cookie in a page. Let's refresh that and we can get 19.
05:07 Now we can also set a cookie in a single sentence.
05:11 For that, I'll type: echo underscore cookie, "name" and the concatenate "is" and then concatenate my "age".
05:27 Therefore we will have a sentence saying "Alex is 19", just from the cookies that we've stored.
05:34 And again if I close my browser or restart my computer or come back two hours later, this information will still be there stored on this computer, ready to be used by this page.
05:44 So, you can see that they are really very useful to use and really easy to create as well and easy to echo out the user.
05:53 Now there's a function we can use called print r() or 'print underscore r'.
05:58 And we can echo out 'dollar underscore cookie' here. We can align it a bit later...
06:05 Refresh this and you can see we've got an array here and we have a different value.
06:12 We have got 'name' and that is equal to "Alex" and we've got an 'age' which is equal to 19.
06:22 So, these are cookies and they have been set and these are the values of the cookies.
06:27 This can be very useful if you want to echo this out to yourself.
06:31 Okay, now there is another function which I will cover in the second part of this tutorial and I'll use an if statement to find out if a cookie is set or not.
06:41 And I'll also show you how to unset a cookie.
06:45 So join me in part 2 and thanks for watching. This is Evan Varkey, dubbing for the Spoken Tutorial Project. Bye.

Contributors and Content Editors

Gyan, Pratik kamble, Sandhya.np14