WEBVTT 00:00:07.110 --> 00:00:09.940 In this video we're going to learn cookies in BHB. 00:00:09.960 --> 00:00:11.850 So what is a cookie. 00:00:11.850 --> 00:00:17.220 A cookie is a text file which is going to store some data on the user's computer. 00:00:17.220 --> 00:00:23.280 We can use a cookie to store information about the user that we can use the next time the user visits 00:00:23.340 --> 00:00:25.050 our Web page. 00:00:25.050 --> 00:00:28.060 So once we use that request our page to the server. 00:00:28.440 --> 00:00:29.600 OK. 00:00:30.060 --> 00:00:34.340 All the data in the cookie is going to be sent to the server with a request. 00:00:34.710 --> 00:00:35.200 OK. 00:00:35.490 --> 00:00:38.430 So let's see how we can set a cookie in BHB. 00:00:38.610 --> 00:00:43.660 So there is a function called Set cookie. 00:00:44.100 --> 00:00:48.120 If you click on control you can explore details about this function. 00:00:48.120 --> 00:00:49.070 So. 00:00:49.460 --> 00:00:56.490 So this function is going to send a cookie is going to return a boolean and it is going to take a few 00:00:56.490 --> 00:00:57.650 parameters in them. 00:00:57.960 --> 00:01:03.230 So the main problem is these are going to be looking at the name of the cookie and the name we can choose 00:01:03.240 --> 00:01:07.580 for our cookie the value for that cookie. 00:01:07.750 --> 00:01:10.400 The expiry time of that as an integer. 00:01:10.410 --> 00:01:15.670 So is this going to be the time stamp OK of the expiry time. 00:01:15.670 --> 00:01:20.670 And there are other promises like the bath which is just telling us where the cookie is available on 00:01:20.670 --> 00:01:21.400 the server. 00:01:21.630 --> 00:01:22.200 OK. 00:01:22.440 --> 00:01:34.070 The domain just saying the domain for which our cookie is available and I would be a problem to secure 00:01:34.090 --> 00:01:34.200 . 00:01:34.280 --> 00:01:34.850 OK. 00:01:35.040 --> 00:01:42.690 So if we said that's true that means that the cookie can only be sent using an anti CPS connection anyway 00:01:42.690 --> 00:01:45.400 so we're going to use only the first three promises. 00:01:45.420 --> 00:01:48.010 OK the name value of the expired sign. 00:01:48.450 --> 00:01:50.590 So it's sets our cookie. 00:01:51.420 --> 00:01:59.190 So let's set a cookie and give it a name user name for example and give it a value. 00:01:59.790 --> 00:02:05.600 For example what does this mean for the expiry time. 00:02:05.610 --> 00:02:07.650 So we need to put a time stamp. 00:02:07.800 --> 00:02:08.440 OK. 00:02:08.640 --> 00:02:13.950 So let's say we want our cookie to expire in one week time. 00:02:14.070 --> 00:02:18.170 OK so we're going to start from the current time as a time stamp. 00:02:18.250 --> 00:02:23.740 Going to use the time function for that and then we're going to add one week in seconds. 00:02:23.760 --> 00:02:24.170 OK. 00:02:24.300 --> 00:02:29.560 So it's going to be seven days multiplied by 24 hours multiplied by six. 00:02:29.560 --> 00:02:32.060 I mean it's six seconds. 00:02:32.730 --> 00:02:35.090 So now we'd have a six hour cookie. 00:02:35.280 --> 00:02:38.040 So now how do we access this a cookie. 00:02:38.250 --> 00:02:43.950 So in piece there is another super global variable that we call cookie which is going to store all the 00:02:43.950 --> 00:02:45.130 cookies. 00:02:45.210 --> 00:02:48.940 So this variable is going is an array is an array. 00:02:49.230 --> 00:02:56.340 So the store is storing the names of the cookies in the Keys and the values of the cookies and the corresponding 00:02:56.340 --> 00:02:57.680 values of those keys. 00:02:57.930 --> 00:03:02.460 OK so if we print that's 00:03:06.150 --> 00:03:08.910 cookie or that cookie variable 00:03:11.970 --> 00:03:15.030 we can see what he's got. 00:03:15.090 --> 00:03:21.990 So it's just save other fresh page and now we can see that the variable at the moment has caused only 00:03:21.990 --> 00:03:27.210 one element because we have only one cookie and it can see the key is name which is the name of our 00:03:27.210 --> 00:03:32.200 cookie and the value is Brinsley is the value of our cookie. 00:03:32.340 --> 00:03:34.000 OK. 00:03:35.310 --> 00:03:41.320 OK so now let's write some simple code checking if the cookie user name exists. 00:03:42.000 --> 00:03:43.840 OK. 00:03:44.010 --> 00:03:49.820 To do that we're going to access you Cookie with the key username. 00:03:50.070 --> 00:04:05.490 So if it exists we are going to echo a paragraph on the screen saying for example the value of the user 00:04:05.560 --> 00:04:07.320 name cookie 00:04:10.550 --> 00:04:26.680 I would just say what the value is access access that's using the cookie rate of the user. 00:04:28.030 --> 00:04:28.970 OK. 00:04:29.040 --> 00:04:30.750 Else we're just going to say 00:04:35.880 --> 00:04:36.840 Cookie not it's 00:04:42.370 --> 00:04:43.850 OK. 00:04:43.970 --> 00:04:49.440 So let's try this and you can see the first 00:04:52.260 --> 00:04:52.950 paragraph. 00:04:52.980 --> 00:04:53.550 OK. 00:04:53.760 --> 00:04:59.140 Because our cookie is set and we can see the value of the using the cookie is honesty's. 00:04:59.160 --> 00:05:02.020 So now I wanted to see how we can delete a cookie. 00:05:02.760 --> 00:05:07.600 So to release the cookie we're going to use the same function. 00:05:08.970 --> 00:05:13.500 Ok but the trick is going to be so. 00:05:14.420 --> 00:05:19.850 So you know we're going to still use the same name of a cookie. 00:05:20.310 --> 00:05:22.550 OK. 00:05:22.770 --> 00:05:23.730 We don't need a value 00:05:26.680 --> 00:05:28.020 at this time. 00:05:28.020 --> 00:05:31.520 We're just going to give an expiry time in the past. 00:05:31.710 --> 00:05:32.190 OK. 00:05:32.310 --> 00:05:39.110 So if we start from the current timestamp and just say minus whatever for example minus 1000. 00:05:39.300 --> 00:05:44.910 So this means that we are sitting again this cookie but we are saying that the expiry time is in the 00:05:44.910 --> 00:05:51.030 past which means that for BHB this cookie is already expired. 00:05:51.030 --> 00:05:52.870 And each is going to get deleted. 00:05:53.180 --> 00:05:55.980 OK so now if we save and refresh 00:06:00.240 --> 00:06:03.350 we can see that our cookie array now is empty. 00:06:03.780 --> 00:06:14.400 And we can see the second paragraph saying about the cookie is not it's OK.