Welcome back. Just to summarise - in the first part of the cookie tutorial, we learnt how to create cookies, how to give an expiry date to the cookie and how to print out specific cookies.
Using this command here, we also learnt how to print out every cookie that we had stored.
So assuming that we have created these cookies, the next thing I'll do is use this specific cookie here that I have created, to check whether it does exist or not.
To do so, we will use a function called “isset”.
This basically returns a true or false value depending on whether something is set or not.
For example- a cookie ,I'll put a dollar sign then underscore cookie.
And I'll put 'name' in here.
So if I read this out in English language then I'll say -
If the cookie name is set then we say echo “Cookie is set”.
Otherwise we will echo out to the user that "Cookie is not set".
Assuming that I have set my cookie and everything is working, when I refresh this I'll get the message that the "Cookie is set".
Now I'll teach you how to 'unset' a cookie.
So lets say over here - just before our 'if' statement, I wish to unset my cookie.
So unset a cookie.
So just to name one, I'll unset this cookie,
presuming that if you learn to unset this one you can unset this one too.
So I'll unset this name cookie.
So to unset we use the same command and that’s 'setcookie'.
So we are resetting a cookie.
This doesn’t seem to make any sense but it will soon.
Now we will set the cookie name to nothing.
And our expiry date here...
I'll create a new one with "exp unset"
And that is gonna equal to the time minus 86400.
Here we said plus which meant that the time is in the future.
Now by setting the cookie to this variable which represents a time in the future, we are actually unsetting the cookie.
So if I were to say - set a cookie that already exists called name, to 'no value' at all.
And use “exp unset” variable to set it to a time in the future, thereby 'unsetting' our cookie.
So I'll get rid of this code for now and run this page, okay?
Nothing has happened presuming my cookie is unset.
Now I want to get rid of this code - so I'll comment this out.
And I'll put my 'if' statement back into my page.
Now presuming this says - is the cookie set name? I'm going to unset the cookie which should get the result "Cookie is not set".
Lets refresh and we got "Cookie is not set".
And then from here you can set it again if you like and you can change the values of the cookie.
To change the value of a cookie, you'll have to use 'setcookie' command again.
You’ll say - set cookie name and here just type a new value
So its not hard to work with cookies.
Its quiet an easy process.
And it’s a very very useful thing in php
So use it to your heart’s content. Okay. Thanks for watching.
If you have any questions, please let me know.
This is Evan Varkey dubbing for the Spoken Tutorial Project. Bye.