|
Http is stateless. Each http request is independent of all others. The server doesn't view its interactions as a bunch of phone calls; rather each http-request is a post-card. There is no inherent way of knowing, when reading a postcard, what previous postcards it may be referring to.
One solution is that the person sending the postcard (the client) includes a “re-cap”, reminding the recipient (the server) who they are, as well as anything else that they've already agreed upon. That's a cookie!
Of course, the recipient needs be wary of whatever the sender claims was previously agreed-upon. We'll (help) address that issue with sessions, next lecture. In particular, we'll use sessions that are based on cookies.
Summary: Cookies.
In addition to the name/value pair, a cookie also has:
an expiration date, a domain, and a directory path.
Whenever a browser requests, a page, it attaches all cookie key/value pairs,
and sends those to the server — if the domain and path match.
So if you visit a page that performs a
setcookie('hamburger-price', 2.70, '/~ibarland', 'itec-php01.radford.edu'),
and later you visit
https://itec-php01.radford.edu/~ibarland/someDir/somefile.php,
your browser will attach the hamburger-price cookie.
But if you visit
https://itec-php01.radford.edu/~jcdavis/someDir/somefile.php,
the browser won't attach the cookie.
Similarly if you visit
https://rucs.radford.edu/~ibarland/someDir/somefile.php,
the browser won't attach the cookie either.
(Note that setting a cookie for '.radford.edu' (note the initial .),
this refers to all machines within the radford.edu domain.)
Don't make two different cookies with the same path, but different domains
(one a superset of the other).
Different browsers may choose differently, which one gets sent.
(AFAICT: the more specific path wins; but for same paths with
two applicable domains, the first cookie set made wins.)
It's not exactly advisable to make two different cookies with the same name but different paths either, though that may not be enforceable, e.g. /~ibarland and /~jcdavis may each contain different scripts that happen to use the cookie “monster”. Note also that if I set a cookie's path to be /, then this is potentially a security flaw: if somebody visits my script and I set a cookie secret-code-word with server&path being radford.edu & /, and then that person visits (say) radford.edu/~jcdavis, his script will be sent the cookie that I had saved. (He's a sly one, that jcdavis!) Upshot: don't set the cookie's server&path to include URLs that others control.
path and domain (optional) parameters are used to limit a cookie to a specific folder in a Web site (the path) or to a specific domain, so this might be used to limit a cookie to a subdomain, such as learn.radford.edu.
Using the path option, you could limit a cookie to exist only
while a user is in, say, the user/jellybeans folder of
the domain:
setcookie('name','value',time() + 3600, '/user/jellybeans').
Now, that cookie won't be shared even w/ most other scripts on the same host
(provided the browser elects to use cookies as-intended).
secure value (optional) dictates that a cookie should
only be sent over a secure HTTPS connection. A value
of 1 indicates that a secure connection must be used,
whereas, 0 indicates that a secure connection isn't
required.
setcookie(name,value,time()+3600,'','',1);
By default, you should use this option unless you have a specific reason not to.
httponly (optional) — can be used to restrict access to the cookie (for example, preventing a cookie from being read using Javascript) but isn't supported by all browsers.
By default, you should use this option unless you have a specific reason not to.
This last flag reminds us: if the browser has a bug where it might give out cookies to other sites, or an attacker can gain other access to the folder where cookies are stored), then there is a privacy vulnerability.
It is essential to remember: Cookies are created by the server, but stored on the client machine.
This explains why you have to call setcookie (rather than just assign $_COOKIE['hamburger-price'] = ...): You actually want to send cookies to the browser to remember (which is what setcookie(…) does, and what assigning to an array can't do).
It also explains why: You must call setcookie before sending any other HTML — the set-cookie info is sent to the browser as part of the http header, which must be sent before any of the HTML data.
There is a knee-jerk reaction that cookies are bad, because they somehow spy on you, magically telling hackers and the NSA every site you visit. While there are valid concerns, it’s a bit subtle as to how this might be achieved.
remember: a cookie can only be used to store information that the server gives you, and that your browser chooses to keep, and later sends (back?) to a server.Limiting third-party cookies, using secure (https-only) cookies, and disallowing javascript access (httponly) to cookies are all wise ways of limiting access. (And, they should be the defaults unless the programmer requests otherwise, but that’s not the case.)
There are reasonable examples where developers might want third-party cookies: For example, kongregate.com is a common portal for javascript games, but individual developers (who have their games on several different platforms, and have their own game-state-servers) want to let a visit to kongregate.com also share info with their servers. ... To ponder: How do other solutions (like oauth and kerberos) compare?
Example:
Remember, (hosted) images are often stored on a different server than
the page's text/html data.
Cookies can be set on any http request, including retrieving images!
Note that separately, just knowing a large chunk of browser history can be suprisingly specific, when you include specific-amazon-products-looked-at, which takeout-restaurant-phone-numbers you're looking up, what political-candidate-webpages you're viewing, and what medical-info-pages you look at — from this it is a not-unreasonable-step that one could conceviably narrow down, with decent confidence, somebody's neighborhood, diseases, how they vote, and what their favorite pizza topping is.
BUT, it would require a single company to be hosting banners/ads for lots and lots of different companies, so perhaps this isn't too big a worry? Well, one last thought: huge numbers of websites outsource to google-analytics, to get info about usage. These google-third-party cookies can be combined with the exact google searches you make and your gmail contents, which can give that company a vast trove of highly specific information. It's a good thing they use their power for good only! (… until NSA gives a court-order, or just plain steals the data from wiretaps placed on intercontinental data trunks, or a hacker-or-disgruntled-employee gets access to their database, …).
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |