PHP Cookies [email_address]
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.
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).
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
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
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
Discussion Advantages of using cookies Disadvantages of using cookies

PHP - Getting good with cookies

  • 1.
  • 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 Createa 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 Retrievea 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 Deletea 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 ofusing cookies Disadvantages of using cookies