How to create a cookie in PHP
In this lesson we look at how to create a cookie in PHP.
We will start off with a very simple cookie called 'colour' which stores a value of 'orange'.
So, in this example, this could represent the favourite colour of the visitor to your website.
Line 1: this is our opening PHP code block declaration.
Line 2: we set the cookie and pass in 2 arguments. The first is the name of the cookie and the second is the value.
In this example, the cookie will never expire. It will remain on the user's computer until such time as they delete it themselves.
Setting a Cookie Expiry Time
We can pass a third argument into the cookie, which is the expiry time of the cookie.
To do this we can get the current time using the time() function and then add a number of seconds.
In this example, I have added 600 seconds (10 minutes) to the current time.
So, the cookie will expire 600 seconds after it has been set.