Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Writing Cookies4:56 with Alena Holligan
There are often times when we want to keep some information available for a longer period of time. We can do this by using cookies. A cookie is a piece of data sent from a website and stored on the user's computer by the user's web browser.
Setting a Cookie
Here is a list of available properties. View more on setcookie() documentation
name: The name of the cookie. You can store cookies as an array by adding square brackets to the end of the name.
// cookie values as array setcookie('cookiename', 'value1'); setcookie('cookiename', 'value2');
value: The value of the cookie. This value is stored on the clients computer; do not store sensitive information.
// assuming the name is 'cookiename', retrieve array values echo $_COOKIE['cookiename']; echo $_COOKIE['cookiename']; // will display value1value2
expire (optional): default = 0
The time the cookie expires. This is a Unix timestamp, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).
// set the cookie to expire in 30 days. // 60 seconds, multiplied by 60 minutes, multiplied by 24 hours, multiplied by 30 days setcookie('cookiename', 'value', time()+60*60*24*30); //set the cookie to expire with session setcookie('cookiename', 'value', 0);
path (optional): default = the current directory in which the cookie is being set.
The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain.
domain (optional): default = works for all subdomains as well
The (sub)domain that the cookie is available to. Older browsers may require a leading . to match all subdomains.
// available to a single subdomain setcookie('cookiename', 'value', 0, 'www.example.com'); // available to all subdomain setcookie('cookiename', 'value', 0, '.example.com');
secure (optional): default = FALSE
Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client. When set to TRUE, the cookie will only be set if a secure connection exists. On the server-side, it's on the programmer to send this kind of cookie only on secure connection (e.g. with respect to $_SERVER["HTTPS"]).
httponly (optional): default = FALSE
We have our story game working.
We can step through the game as many times as we'd like, and
the story changes with each play.
Sessions provided an easy way to keep the required variables available
throughout the site.
One of the main features and limitations of sessions
is that they are only available for the current user session.
They terminate when the browser is closed, or
after a set amount of time with no activity.
There are often times when we want to keep some information available for
a longer period of time.
Storing a zip code preference for location based searches or to calculate shipping.
Storing a referrer reference, so
affiliates get credit when someone purchases a product or service.
Storing shopping cart data for people who are not yet logged in.
All of these require that we keep data persistent for
longer than a user's session.
We can do this by using cookies.
A cookie is a piece of data sent from a website and
stored on the users computer by the user's web browser.
Let's take a look at how PHP accesses cookies
by adding the ability to save a story.
Create a new file in the inc directory and we'll name it cookie.php.
We want to grab our session variables,
so add session_start(); at the top of the file.
We'll be using this file to write, read, and delete a cookie.
So we need to know which action we're performing.
If we're saving a cookie, first we need to come up with a unique name for the cookie.
If the name is not unique, it will write over the cookie that already exists.
We could just use a timestamp, but
we want people to be able to distinguish the story they want to read.
I probably won't remember what story I wrote at which time, but
I'll be more likely to remember if I'm shown the name that I used with the story.
So let's use the name from the story, along with the timestamp.
The name should be a simple string of letters.
But we'll use urlencode just to be sure.
To set a cookie we use setcookie.
The first parameter is the name, and the second parameter is value.
Value has to be stored a string.
This could be a serialized version of an array, a JSON object,
or any other string representation of an object or an array.
We can also turn a cookie into an array by
adding square brackets to the end of our name.
But's let use implode to create a string from our session variables.
We'll separate them with a colon.
That's technically all you need to set a cookie.
But it won't work for what we want.
The third parameter is an expiration time.
If this is not set, the cookie will expire when the session is closed.
So we aren't gaining anything there.
Let's set this to 30 days.
String to time will turn this string into a time stamp.
The fourth parameter is the path on the server
in which the cookie will be available.
By default the current path is used, which would mean
that the cookie would only be accessible from within the inc folder.
We could make that work but let's set the path to the root of our site.
These are the parameters we need for our project, but
let's check out the documentation for the last three parameters.
These can help with securing the cookie.
The fifth parameter is the domain.
This will allow you to control which domain and
subdomains can access the cookie.
The sixth parameter will allow us to set a cookie
that is only available over a secure connection.
The seventh and final parameter will allow us to set the cookie to HTTP only.
This means that the cookie won't be accessible
Let's go back to workspaces.
Let's add a redirect to the welcome page.
Now we can update the link on our story page to save the story.
Our story is ready to be saved to a cookie, but
we have no way of reading that cookie yet.
In the next video, we'll read the cookie and use that data to rewrite the story.
You need to sign up for Treehouse in order to download course files.Sign up