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