1 00:00:00,335 --> 00:00:05,801 Cookies can be a bit tricky to work with using only the built-in PHP functions. 2 00:00:05,801 --> 00:00:09,457 By using the symfony HTTP foundations package, 3 00:00:09,457 --> 00:00:14,628 we not only have access to flash messages in the session, we also have 4 00:00:14,628 --> 00:00:19,997 an easier way to access the cookie on the request and response objects. 5 00:00:19,997 --> 00:00:23,093 Cookies work a little differently than sessions. 6 00:00:23,093 --> 00:00:26,515 For this session, we just need to turn on sessions and 7 00:00:26,515 --> 00:00:31,096 then we can read and write a session variable at any point in our script. 8 00:00:31,096 --> 00:00:32,514 For cookies, first, 9 00:00:32,514 --> 00:00:36,542 we need to create a cookie with all the details we wish to set, and 10 00:00:36,542 --> 00:00:41,407 then we must send that cookie to our browser by passing it through a response. 11 00:00:41,407 --> 00:00:44,411 Let's see how this works in our application. 12 00:00:44,411 --> 00:00:47,962 We're saving user data in the functions_auth file, 13 00:00:47,962 --> 00:00:50,518 under the saveUserSession function. 14 00:00:50,518 --> 00:00:54,664 Well let's start here, we aren't going to modify anything right now. 15 00:00:54,664 --> 00:00:57,029 We're just going to add some cookies. 16 00:00:57,029 --> 00:01:01,810 To create a cookie, we'll use the cookie class from the symphony package 17 00:01:01,810 --> 00:01:04,907 that we already included in this application. 18 00:01:04,907 --> 00:01:09,316 We'll add a new cookie and 19 00:01:09,316 --> 00:01:12,925 we'll set it equal to 20 00:01:12,925 --> 00:01:21,960 new\symfony\component httpfoundation\cookie 21 00:01:24,087 --> 00:01:29,977 The two required pieces of information are the cookie name and the value. 22 00:01:29,977 --> 00:01:35,928 We'll start with our auth_user_id. 23 00:01:35,928 --> 00:01:39,752 We'll set this equal to our auth_user_id above. 24 00:01:52,311 --> 00:01:57,256 There we go, now let's copy this cookie and 25 00:01:57,256 --> 00:02:02,747 we'll make a second cookie named auth_roles, 26 00:02:05,237 --> 00:02:07,530 And set this to the role_id. 27 00:02:09,139 --> 00:02:15,756 Now we can name the cookie, cookieID and cookieRoles. 28 00:02:15,756 --> 00:02:20,487 Now that we have our cookies, we're ready to pass them through a response. 29 00:02:20,487 --> 00:02:22,872 We can do this on our redirect, but 30 00:02:22,872 --> 00:02:28,401 we'll need to adjust the redirect function to add those cookies to our header. 31 00:02:28,401 --> 00:02:32,985 In the settings file, We'll 32 00:02:32,985 --> 00:02:37,094 scroll down to our redirect, and we'll add a new parameter. 33 00:02:37,094 --> 00:02:42,127 This optional parameter will be in array of extra details we want to accept. 34 00:02:42,127 --> 00:02:46,634 So we'll name it extra and we'll set it equal to an empty array. 35 00:02:46,634 --> 00:02:49,575 Next we need to add these cookies to our response. 36 00:02:49,575 --> 00:02:55,422 So after the response, we'll check if key_exist, 37 00:02:57,680 --> 00:03:01,363 Cookies in our extra array. 38 00:03:06,035 --> 00:03:08,616 Since we're passing multiple cookies, 39 00:03:08,616 --> 00:03:11,794 we want to add each of those cookies to our headers. 40 00:03:11,794 --> 00:03:20,477 We can use a foreach extra Cookies as cookie, 41 00:03:23,803 --> 00:03:27,861 And then we'll modify our 42 00:03:27,861 --> 00:03:33,027 response to add response headers 43 00:03:33,027 --> 00:03:38,018 setCookie equal to our cookie. 44 00:03:38,018 --> 00:03:41,570 Finally we're ready to pass our cookie to our redirect.