13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Underst<strong>and</strong>ing Basic Session Functionality<br />

511<br />

when the user visits the next page in your site (or reloads the current page), you will<br />

have access to the cookie via $_COOKIE[‘mycookie’].<br />

You can delete a cookie by calling setcookie() again with the same cookie name<br />

<strong>and</strong> an expiry time in the past.You can also set a cookie manually via the header()<br />

function <strong>and</strong> the cookie syntax given previously. One tip is that cookie headers must be<br />

sent before any other headers; otherwise, they will not work. (This is a cookie limitation<br />

rather than a <strong>PHP</strong> limitation.)<br />

Using Cookies with Sessions<br />

Cookies have some associated problems: Some browsers do not accept cookies, <strong>and</strong> some<br />

users might have disabled cookies in their browsers.This is one of the reasons <strong>PHP</strong> sessions<br />

use a dual cookie/URL method. (We discuss this method shortly.)<br />

When you are using <strong>PHP</strong> sessions, you do not have to manually set cookies.The session<br />

functions take care of this task for you.<br />

You can use the function session_get_cookie_params() to see the contents of the<br />

cookie set by session control. It returns an array containing the elements lifetime,<br />

path, domain, <strong>and</strong> secure.<br />

You can also use<br />

session_set_cookie_params($lifetime, $path, $domain [, $secure]);<br />

to set the session cookie parameters.<br />

If you want to read more about cookies, you can consult the cookie specification on<br />

Netscape’s site: http://wp.netscape.com/newsref/std/cookie_spec.html<br />

(You can ignore the fact that this document calls itself a “preliminary specification”; it’s<br />

been that way since 1995 <strong>and</strong> is as close to a st<strong>and</strong>ard as a document could be without<br />

actually being called a st<strong>and</strong>ard!)<br />

Storing the Session ID<br />

<strong>PHP</strong> uses cookies by default with sessions. If possible, a cookie will be set to store the<br />

session ID.<br />

The other method it can use is adding the session ID to the URL.You can set this to<br />

happen automatically if you set the session.use_trans_sid directive in the php.ini<br />

file. It is off by default.You should use caution when turning this directive on as it<br />

increases your site’s security risks. If this is set to on, a user can email the URL that contains<br />

the session ID to another person, the URL could be stored in a publically accessible<br />

computer, or it may be available in the history or bookmarks of a browser on a publically<br />

accessible computer.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!