PHP - Getting good with cookies

999 views

Published on

Getting good with cookies

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
999
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
23
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

PHP - Getting good with cookies

  1. 1. PHP Cookies [email_address]
  2. 2. What is Cookies? <ul><li>A cookie is a small file that the server embeds on the user's computer. </li></ul><ul><li>A cookie is often used to identify a user. </li></ul><ul><li>Each time the same computer requests a page with a browser, it will send the cookie too. </li></ul><ul><li>With PHP, you can both create and retrieve cookie values. </li></ul>
  3. 3. How to Create a Cookie? <ul><li>setcookie(name, value, expire, path, domain); </li></ul><ul><li>The setcookie() function must appear BEFORE the <html> tag. </li></ul><ul><li>The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received (to prevent URLencoding, use setrawcookie() instead). </li></ul>
  4. 4. Example - Cookies <?php setcookie(&quot;user&quot;, “mni&quot;, time()+3600); ?> <html> <body> </body> </html> Create a cookie named user Assign a value to the cookie (user) The cookie will expire in ONE hour
  5. 5. How to Retrieve a Cookie Value? <html> <body> <?php if (isset($_COOKIE[&quot;user&quot;])) echo &quot;Welcome &quot; . $_COOKIE[&quot;user&quot;] . &quot;!<br />&quot;; else echo &quot;Welcome guest!<br />&quot;; ?> </body> </html> Retrieve a value from a cookie called user
  6. 6. How to Delete a Cookie? <?php setcookie(&quot;user&quot;, &quot;&quot;, time()-3600); ?> Set the expiration date to ONE hour ago Reset the value of cookie
  7. 7. Discussion <ul><li>Advantages of using cookies </li></ul><ul><li>Disadvantages of using cookies </li></ul>

×