PHP-and-MySQL/C4/Cookies-Part-1/English
From Script | Spoken-Tutorial
(Redirected from PHP-and-MySQL/C4/Cookies-(Part 1)/English)
Time | Narration |
---|---|
0:00 | Welcome to this tutorial on php cookies. |
0:04 | Cookies are a very important part when creating special websites where you store information about a user. |
0:11 | The definition of a cookie is a set of data stored on your computer or the user's computer by the web server. |
0: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'. |
0:30 | So you don't have to keep logging in. |
0: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. |
0:42 | So sessions are killed straight away however cookies are stored for later use. |
0:50 | So lets begin right away and see how to create a cookie. |
0:53 | You do this by using the setcookie function. |
0:55 | The function takes 5 parameters but I will use just 3. |
1:00 | The first vital one I will use is the name of the cookie which I will set to 'name'. |
1:05 | The second one is the data that needs to be stored inside this cookie and I'll type Alex here. |
1:12 | Now the next one's a bit more tricky. |
1:15 | Its the time in which it expires. |
1:18 | Now this needs to be set in seconds. |
1:21 | And to represent this I am going to create a variable called 'exp' for expire and this will be equal to the time. |
1:28 | Let me add some value here. |
1:31 | At the moment I am adding zero. |
1:33 | So if I echo this out and get rid of this cookie function for now. |
1:39 | I'm just echoing out the time here just to show you what it does. |
1:43 | So lets refresh. So you can see quite a lot of digits here. |
1:47 | Now this is the unique time-stamp. |
1:50 | And the unique time-stamp is the number of seconds before January the 1st 1970. |
1:56 | So January the 1st at 12 am ummmm...... in the year 1970. |
2:02 | So you can see it here - I think the number of seconds in here equates to a date in the future. |
2:10 | So for example, at this moment you can see this 88, now 89 and as I keep refreshing, by every second this increases. |
2:20 | So yes, this is quite useful way of adding a specific value here. |
2:28 | Now we need to find out the time in seconds of a day because I want this cookie to expire in a day. |
2:34 | So I multiply 24 by 60 to get the number of minutes in a day. |
2:39 | And then multiply the answer by 60 to get the number of seconds in a day which is 86,400. |
2:47 | So if I replace zero with 86400, we have the variable "expire" that now holds the time in the future by a day. |
2:56 | To save time, I am copying this and I will add my expire variable here. |
3: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. |
3:13 | So lets refresh this page and hey! we can see we have no errors which means it has worked. |
3:19 | Now what I'll do is I'll use block commenting to comment out all of these. |
3:23 | And below this I'll echo out this cookie. |
3: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. |
3: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. |
3:41 | And you could use it within this time that we have set here. |
3:46 | So what I'll do is I'll set echo and I'll use a dollar sign, sorry, underscore cookie. |
3:52 | Inside here is the name of the cookie so I'll type 'name'. Refresh and you can see 'Alex'. |
3: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 its been stored into the computer. |
4:11 | Okay now if had to set another cookie, let's say, I set another cookie here and this will be 'age' and my age is 19. |
4:24 | And my expiry time I'll just keep as this. |
4:29 | So lets just put this up here. |
4:31 | We can replace the block commenting with line-comment to keep it neat. |
4:36 | So for our expiry time I'll set another cookie in here. |
4:41 | And that’ll have the same expiry time. Lets see if I can get it right. |
4:46 | Okay so we'll get rid of this. |
4:48 | We’ve set another cookie with the same expiry time. |
4:51 | Lets refresh. Okay that has been set. |
4:55 | So what I'll do is comment this out and here I will echo it out. |
5:01 | So you see we can set more than one cookie in a page. Lets refresh that and we can get 19. |
5:07 | Now we can also set a cookie in a single sentence. |
5:11 | For that I'll type echo underscore cookie, name and the concatenate "is" and then concatenate my age. |
5:27 | Therefore we will have a sentence saying Alex is 19 just from the cookies that we've stored. |
5: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. |
5: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. |
5:53 | Now there's a function we can use called 'print r' or 'print underscore r'. |
5:58 | And we can echo out 'dollar underscore cookie' here. We can align it a bit later... |
6:05 | Refresh this and you can see we've got an array here and we have a different value. |
6:12 | We have got name and that is equal to Alex and we've got an age which is equal to 19. |
6:22 | So these are cookies and they have been set and these are the values of the cookies. |
6:27 | This can be very useful if you want to echo this out to yourself. |
6: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. |
6:41 | And I'll also show you how to unset a cookie. |
6:45 | So join me in part 2 and thanks for watching. This is Evan Varkey dubbing for the Spoken Tutorial Project. Bye. |