Headers
- PHP and Apache identifies the document as HTML, calculating the length of the HTML page, and so on.
- Most web applications never need to set headers themselves.
- if you want to send back something that's not HTML
- set the expiration time for a page
- redirect the client's browser
- generate a specific HTTP error
Expiration
- A server can explicitly inform the browser, and any caches of a specific date and time for the document to expire.
- Proxy and browser caches can hold the document until that time or expire it earlier.
- Repeated reloads of a cached document do n ot contact the server.
- However, an attempt to fetch an expired document does contact the server.
Authentication
- HTTP authentication works through request headers and response statuses
- A browser can send a username and password (the credentials) in the request headers
- If the credentials aren't sent or aren't satsifactory, the server endsa "401 Unauthorized"
Content Dispositions
- Let the user be prompted to save a generated PDF file
- Content-Disposition header is used to supply a recommended filename and force the browser to display the save dialog box
Session
A session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping items, etc).
However, session information will be deleted after the user has left the website.
If you need a permanent storage you may want to store the data in a database.
Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID.
The UID is either stored in a cookie or is propagated in the URL.
Cookies
A cookie is often used to identify a user.
A cookie is a small file that the server embeds on the user's computer.
Each time the same computer requests a page with a browser, it will send the cookie too.
The setcookie() function is used to set a cookie.
•setcookie("user", "Alex Porter", time()+3600);
The PHP $_COOKIE variable is used to retrieve a cookie value.
•php
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookiesprint_r($_COOKIE);
?>
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookiesprint_r($_COOKIE);
?>
When deleting a cookie you should assure that the expiration date is in the past.
•php
// set the expiration date to one hour agosetcookie("user", "", time()-3600);
?>
// set the expiration date to one hour agosetcookie("user", "", time()-3600);
?>
No comments:
Post a Comment