By default sessions in pay HP stirrer cookie in the browser setting and retrieving the cookie value
is handled After Matty Klee by the session functions in P eight p that we just saw.
We can also manage cookies manually.
So what exactly are cookies.
Cookies are basically small strings of text that a website can store in a browser.
The Web site can set a value in a cookie and later retrieve that value in subsequent requests.
Cookies are specific to a particular website are more specifically domain.
One website can not read the cookies stored in the browser they were sent from a different domain.
Cookies can contain any text you like but they're easily viewed by anyone who has access to the browser.
They're not hidden are encrypted in any way so we don't steal anything sensitive in there like an email
address of a user idae.
To set a cookie in P HP we use the set cookie function and once they've been set they can be read back
in the cookie array.
Let's look at an example.
Here we have a script called Set cookie in which we're setting a cookie.
The first argument to the set cookie function is the name of the cookie And the second argument is the
value.
So here we're setting a cookie called example with the value of hello.
In this script we are just printing out the contents of the cookie array and in the index page we just
have a link to both files.
As we can see from the storage inspector in this browser There are currently no cookies for this domain.
If I run the set cookie script then there's the cookie we just set.
If I go back and open the read script we can see the cookie value in the P 8 p cookie array.
By default the cookie is set to expire at the end of the session.
This means that when the browser is closed the cookie is deleted.
We can change this by adding an expiry using the third argument of set cookie.
This value is a Unix timestamp which means it's an integer number of seconds since the 1st of January
1970.
The easiest way to set this is to use the time function and add a number of seconds to it.
For example here I am adding two days worth of seconds to the current time meaning the cookie will expire
two days from now.
Now if I set the cookie we can see it has a specific expiry date and time.
To delete a cookie.
We set the expiry to a time in the past.
For example here we're setting the expiration date to 1 hour ago.
Which is three thousand six hundred seconds.
So if I open this delete cookie script the cookie is deleted and it's no longer present in the cookie
array on the server.
Cookies also have a path attribute this defaults to the current directory.
The script is running in.
In this script.
In a subfolder of the root we're setting a cookie as before but with a different name.
When I run this there's the cookie and X path value is set to the folder.
The script is in cookies.
It's set in subdirectories are only available to scripts in those directories.
So if I open the script to read cookies which is in the root we can't see it's in the cookie array.
We can specify that the cookie should be available in the whole domain by adding a value for the path
argument of the set cookie function.
Now if I set the cookie from the subdirectory its path is set to the root instead of the subfolder.
If we view the cookies array we can now see the cookie set from the subdirectory.
As we just saw although session cookies are handled are to magically baby HP.
So simply using the session functions like session starts.
We don't need to do anything manually with the session cookie.