Okay, let’s get though this before the new year! Finally this chapter will conclude with Header control, accessing cookies, and modifying sessions.
Headers
- It’s important to note at first that when writing your own headers, they need to be done BEFORE any output to the screen!
- Headers can be modified by using the header($string) function. Headers are usually in the format "Key: Value"
- The most basic example is the redirect:
header(“Location: http://www.google.com/”);-
exit();
-
- Note the exit() call after the headers are displayed. This is required to prevent your script from outputting anymore data. Because with a header redirect, the browser is actually making the redirect. Therefore, you don’t want to send anything else to the page (like admin panel information) in the event a hacker decides to ignore the redirect.
- You can also write caching directly into the browser by using the Cache-Control and Expires header. For more information, check out this.
Cookies (nom nom)
- Cookies cannot hold a lot of information. And it’s important NOT to store security information in them. After a cookie is stored on the client, they can read/modify/or remove it, so never trust important information to them.
- Cookies can set by using the setcookie($name, $value, [$expires]). The name can be done the same as GET and POST data with the arrays[]. And the expires is a basic UNIX timestamp time(). There are additional optional information (path – location on your site they are accessible, domain – what sub/domain they are accessible from, and secure – transmitted only over https?), but those aren’t really important right now.
- They can be read (after the next page load) via the $_COOKIE superglobal.
- To delete a cookie, the only way is to reset that cookie to expire before now:
setcookie(“skip_trailer”, false, time() – 1000);-
</li>
-
</ul>
-
-
<h3>Sessions</h3>
-
-
<ul>
-
<li>Sessions are a basic form of state awareness between HTTP requests.  The webserver doesn't know or care who the client is and which connections belong to him.  Therefore, PHP uses session IDs, passed usually via cookie (can be changed), to identify users.  This session ID is then related to a server-side storage of information writable and accessible via the <strong>$_SESSION</strong> superglobal. </li>
-
-
<li>Sessions can be started with <strong>session_start()</strong> function, which <strong><em>MUST </em>be run before any output to the browser</strong> (as it uses header data).
-
-
<br />
-
-
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:887EC618-8FBE-49a5-A908-2339AF2EC720:cb57cdf4-32da-4352-be79-b9117d7b4038" class="wlWriterEditableSmartContent"><pre lang="php">session_start();
-
-
if (isset($_SESSION['user_id'])) {
-
$_SESSION['last_access'] = date('Y.m.d H:i:s');
-
echo "You are logged in!";
-
} else
-
echo "Please login!";
-
Well, there was the Web Programming chapter. Tomorrow will be the start of Object-Oriented Programming.
It's going to be the New Year soon (yeah twenty-ten) and that means I only have 20 days before my exam! Oh man, seven chapters to go, so I better start cramming!

