PHP - Getting good with cookies

  • 681 views
Uploaded on

Getting good with cookies

Getting good with cookies

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
681
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
6
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. PHP Cookies [email_address]
  • 2. What is Cookies?
    • A cookie is a small file that the server embeds on the user's computer.
    • A cookie is often used to identify a user.
    • Each time the same computer requests a page with a browser, it will send the cookie too.
    • With PHP, you can both create and retrieve cookie values.
  • 3. How to Create a Cookie?
    • setcookie(name, value, expire, path, domain);
    • The setcookie() function must appear BEFORE the <html> tag.
    • The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received (to prevent URLencoding, use setrawcookie() instead).
  • 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. 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. 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. Discussion
    • Advantages of using cookies
    • Disadvantages of using cookies