1

I learned that when you set cookies, you give them a name, a value, and an expiration. This is an example I got from a book.

setcookie('test', 45, time()(60*60*24));

I understand the purpose of setting a name, so we have a way of referring to it. And I understand the purpose of setting the expiry date, but what is the purpose of setting a value? Why would we need to set a value?

Jon
439k85 gold badges757 silver badges820 bronze badges
asked Mar 20, 2011 at 0:32

3 Answers 3

2

The value is the very reason for which you are setting the cookie: so that the next time the browser makes a request, it repeats the value you gave it earlier back to you.

In some occasions, even repeating just the name would be useful (in essence you are getting back one bit of information by the presence or absence of the name). Adding a value lets you keep more than one bit.

So why keep the name if there's going to be a value anyway? Because cookies with different names can have different expiration times, and because it can be convenient for separate components of an application or system to have a cookie dedicated to each one of them. This way, you don't need to account for what component A did to the cookie value when you are setting (overwriting) it from component B. You have a value all to yourself, distinguished by its name.

For completeness, I should mention that there are also other cookie attributes that can be set per-cookie (i.e. per distinct name):

  • Domain (cookie is valid only on specific domain and subdomains)
  • Path (cookie is valid only on specific request path and below)
  • Secure/HttpOnly (however, these cookies can have no value)
answered Mar 20, 2011 at 0:33
Sign up to request clarification or add additional context in comments.

11 Comments

but why couldn't it just repeat the name you gave it, if you're seeking a way to identify it. Why do you need that value? What do you do with it? Please explain more if you can. thank you.
@Michael - It could repeat the key in the value; the value is just a setting you can get by calling the name you gave it (key). It can be nothing, it can be same as the key (name), it can be anything you set it to (within reason).
actually, it says that values are optional, so I don't see why your answer is correct. php.net/manual/en/function.setcookie.php
@Jared Farrish, but why do you want the value. If the importance of the cookie is presence, and that can be achieved by name, then what's the purpose of the 45 in the OP for example?
@Michael - It's not wrong, he's just giving you the reason you would set a value, so that you can retrieve it.
|
1

Cookies are key = value pairs. Just the presence of a cookie could be useful (even if nothing), but it's ultimately meant to store a value retrievable by key (name).

And technically you don't need the value or expiry: http://php.net/manual/en/function.setcookie.php

answered Mar 20, 2011 at 0:34

Comments

0

The value of the cookie. This value is stored on the clients computer; do not store sensitive information. Assuming the name is 'cookiename', this value is retrieved through $_COOKIE['cookiename']

Also Check: PHP setcookie Manual; PHP setcookie Function

answered Mar 20, 2011 at 0:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.