18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 16<br />

The one restriction on setting cookies in PHP is that it must be done before anything is output to the<br />

client. This is similar to the way the header() function works. <strong>For</strong> instance, this code won’t work:<br />

<br />

<br />

<br />

To retrieve the value of a cookie in PHP, use the $_COOKIE associative array with the name of the cookie<br />

as the key:<br />

<br />

Note that you cannot retrieve the value of a cookie created with setcookie() until the next page loads,<br />

meaning that is the following code is impossible:<br />

<br />

To delete a cookie in PHP, just use the setcookie() method and set the expiration time to 0:<br />

<br />

Passing cookies between client and server<br />

To communicate to <strong>JavaScript</strong>, often the server creates a cookie (perhaps in a servlet or other application<br />

running on the server) just before a response is sent out. The page that is loaded now has <strong>JavaScript</strong><br />

designed to retrieve the cookie value.<br />

<strong>For</strong> example, suppose you are creating a feedback form on a <strong>Web</strong> site where the user is required to enter<br />

a name, e-mail address, and the feedback message. You can make this more user-friendly by allowing<br />

the users to save their names and e-mail addresses so the next time they visit and want to leave feedback,<br />

both fields are already filled in. Typically, you do this with a Remember Me check box, as in the<br />

following form:<br />

<br />

Name: <br />

E-mail Address: <br />

488

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

Saved successfully!

Ooh no, something went wrong!