|
home—lects—hws
D2L—breeze (snow day)
From PHP Visual Quickstart Guide by Larry Ullman
Originally based on notes by Jack Davis (jcdavis@radford.edu)
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, and what they've already talked about or 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.
As larger more complex web sites are being built the limitation of http as a stateless protocol becomes a problem. Web developers have no built in (html) method of remembering data from one page of an application to the next. This is a serious short-coming, e-commerce systems, user registration and login systems, and other online services rely on this functionality. Fortunately, maintaining state from one page to another is fairly simple using PHP.
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
(Note that setting a cookie for
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
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:
By default, you should use this option unless you have a specific reason not to.
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
It also explains why:
You must call
Cookies have gotten a bad rap because some users believe cookies magically allow anybody to learn any information. While there are valid concerns, a cookie can only be used to store information that you give it. 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, …).
1Kinda like emails that start with repeating/quoting the entire previous thread. ↩
2
Although php's
home—lects—hws
D2L—breeze (snow day)
©2016, Ian Barland, Radford University Last modified 2016.Oct.24 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |