PHP Front To Back
Lección 17 de 22 · PHP Front To Back [Part 17] – Cookies Tutorial · TI y software
PHP Front To Back [Part 17] – Cookies Tutorial
hey guys in this episode of PHP front-to-back we're going to take a look at PHP cookies in the last video we looked at sessions now the difference
between sessions and cookies is that sessions the data is stored on the server with cookies the data is actually stored on your browser on the client so in almost all cases I would suggest
using sessions over cookies they're much more secure with cookies you can you can set the expiration date even after the browser closes but when you start getting into that you probably want to
look into persisting the data to a database but either way I figured it was a good idea just to show you how to do it just so you know so we're going to do this this is going to be pretty quick
I'm going to create a new folder in the PHP sandbox and we're going to call this website so what do we add 4 5 so website 5 and let's create a file and we'll save it as page 1 dot PHP and just like we
did with the sessions I'm just going to put in a form here real quick alright so it's just a form within a you surname field alright and then what I'm going to do is save the username to a cookie so
let's go up here and we'll put in our PHP tags and let's check for the submit ok so we're going to say if is set wait a minute where's my code highlighting why is this saved as an HTML or
displaying as HTML that's weird there we go so if is set and we want to check for a post and then submit because the submit button has a name of submit and right here what we'll do is create a
variable called username and we'll set that to HTML entities and let's pass in our post username ok so now we have that in a variable so what we do to set a cookie is we use the function set cookie
okay pretty simple and first parameter is going to be basically the name of the cookie I'm going to call it user name second is going to be the value which is going to be our username variable and
then the third is going to be the expiration so I'm going to set it to time using the current time function here and then we're just going to add on to it
3,600 and that's going to add it that's going to set this cookie for one hour okay so after an hour this is going to expire and now what we're going to do is use the header function to redirect to a
different page so it's that location to page two dot PHP all right so we submit the form we put the user name into a variable and we call set cookie with user name and then the value and then
the expiration so let's create a new file and save it as page 2 dot PHP all right and on page two what we're going to do is check to see if that cookie is set
let's when you our PHP tags okay so we're going to just say if is set and then we'll pass in here using the cookie superglobal and we want to check for the user name alright so if that's set then
let's echo out let's see we'll just echo user and then let's just concatenate here cookie we use your name and go back into a string and we'll say is set and a line break and let's put it else and for
that we'll just echo out username our user is not set all right so let's check that out we're going to go to localhost / PHP sandbox / what a website v / page one dot php'
unexpected set cookies what was I do wrong here oh I forgot my semicolon right here alright so let's add a name in here we'll just say it will just say John I
guess that's fine submit and what the hell what does I do here I forgot the semicolon again all right so let's reload that and we get user John is set we should probably put a space there
alright so now we have the cookie username set – John now if we want to check this out in the console down here if we click f12 and we go to application and you can see I actually have some
other stuff in here as well from doing the testing and all that so what I'm going to do is actually just delete these those two and then this is the one that we just added so it shows us the
name of the cookie the value the domain which is localhost the path which is the path to the folder that it's in and then the expiration which is an hour from now and the size okay you can also view
cookies if you go to if you're in chrome at least you can go to settings and there's a way to do this in all browsers but if we go out to privacy and then content settings and then all cookies
and site data and we're going to search for localhost okay so you can see this is all the stuff we have stored for localhost and local storage and all that we want this right here user that's
actually not the one I want just that no username that's what we want and here it is user name John domain path when it was created and you can see it expires at 10 so it was set at 9:40 it expires
at 10:40 and it's available to our script okay so that's how you can go your cookies from the browser all right and I would see what I want to do now is show you how to unset it okay so
we'll go we'll stay on page two and actually first I'll show you how to update it or how to change it so to change it we can just again call set cookie and make sure we use the same key
or the same name which is username and we'll set that to Frank and then we can set the expiration this time I'm going to set it for our day so we can use time and then plus and then in here we'll do
86400 and we want to multiply that by 30 and that will give that we'll set it for day okay so let's save that and reload page two and then we get user Frank is set and if we look down the console here
and see user name is now Frank now if we want to unset it what we can do is just set the time to something that's already passed all right so I'll actually just copy this and let's say here delete
cookie paste that in and then let's just change we're just going to change this to let's say time and let's say – 3600 which is the amount of seconds in an hour so this is now set to an hour ago
so we're going to set it and then we're going to unset it and see what happens okay so let's reload and then user is not set okay so it's set it unset it and then this fired off because it's not set
you can also check to see if there are any cookies so what I'm going to do is just delete our comment out the delete here so it gets set again user Frank is set and then right above this let's do
if and will say if count and then pass in cookie and will say if that is greater than zero that means there's cookies that are set
okay so let's echo out see well I go out there are and then we'll concatenate the count cookie alright so there are count cookies saved and then else else then we'll just echo there are no cookies
saved alright we'll save that and reload and we get but let's put a line break there alright so now we get there one cookie saved user Frank is set okay and if we were to delete that by
uncommenting this and reload twice and now there are no cookie saved user is not set alright so this is pretty simple stuff now sometimes you may want to store more than one piece of data in a
cookie and a good way to do that is using an array an associative array so I'm going to show you how to do that real quick so let's create another page save it as page 3 dot PHP and in here
what we'll do is create an array so let's create a variable called user and we'll set that to some square brackets and we'll have a name okay we'll set that to Brad and let's have an email and
we'll set that to let's just say test at test comm and let's say age and we'll set that to 35 all right so we have this array now let's see what happens if we do set
okey and we want to set this as the user and then we'll pass in the user array and then let's say time so I doing that time plots will say h6 400 times 30 so that'll be one day all right now let's
see what happens if we do that so let's go to page three and we get setcookie expects parameter two to be a string but there's an array given okay so it's not going to take our array it's looking for
a string now what we can do is we can use the function called serialized which will prepare the data to be stored so let's say user equals and then use the serialized function and pass in the user
array okay so now we'll save that and reload and we don't get anything because we're not outputting anything but you can see there's no error and if we look down here in cookies you'll see user and
then the value which looks really weird but that's what it what happens when you serialize it so what we want to do now is we want to grab let's say we want to get the name from that so what we'd have
to do is uncie realize it so let's say user equals actually first let's see what happens if we don't uncie realize so if i say echo user that's not going to work let's say echo cookie actually
we'll put that in user use your equals cookie user all right and then we'll try to echo out user which you would think would be the array and then name so if we do that we get a legal offset name
because it's this is an actual readable array so what we need to do is uncie realize it all right and then we'll save and reload and we get Brad okay we can get Brad we
can get the email all right we could do a print R of this and take a look at that and you'll see we have all of our values all right so that's how you can serialize data save it into a cookie and
then uncie realize it but again I would suggest in probably 95% of the time I'd recommend sessions over cookies unless there's some specific reason that you need to use cookies all right so thanks
for watching guys and I will see you in the next video
Sessions are recommended over cookies in most situations but it is good to understand how cookies work as well. In this video we will be using the setcookie() function to create cookies in PHP. We will also look at serializing data with serialize() and unserialize()
CODE: Code for this video
http://www.traversymedia.com/downloads/phpsandbox/phpsandbox15.zip
BROWSER EXTENSION: Momentum
https://chrome.google.com/webstore/detail/momentum/laookkfknpbbblfpciffpaejjkokdgca?hl=en
EDUONIX COURSES: Please use affiliate links from website below
http://www.traversymedia.com/eduonix-…
SUPPORT: We spend massive amounts of time creating these free videos, please donate to show your support:
http://www.paypal.me/traversymedia
http://www.patreon.com/traversymedia
FOLLOW TRAVERSY MEDIA:
http://www.facebook.com/traversymedia
Posts by traversymedia
http://www.linkedin.com/bradtraversy