Programming Basics

  Home arrow Programming Basics arrow Page 2 - Cookies in PHP
PROGRAMMING BASICS

Cookies in PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 57
    2004-01-27

    Table of Contents:
  • Cookies in PHP
  • Basic Cookie
  • Cookie Attributes
  • Words Of Caution

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Cookies in PHP - Basic Cookie


    (Page 2 of 4 )

    Let's look at an example that uses a very basic cookie:

    <?php
    if (!isset($_COOKIE["name"])) {

      
    setcookie("name"$_POST["name"]);
      
    setcookie("visits"1);

      echo 
    "Hello $_POST[name]. &lt;br /&gt;";
      echo 
    "It appears that this is your first visit!";

    } else {
      
    setcookie("visits", ++$_COOKIE["visits"]);

      echo 
    "Welcome back, $_COOKIE[name]. &lt;br /&gt;";
      echo 
    "You have visited us $_COOKIE[visits] times!";
    }
    ?>

    The two points of interest are $_COOKIE and setcookie.

    Cookie information sent by the browser is accessed with $_COOKIE, a super global array containing cookie values keyed by name.

    The setcookie function is used to assign a key/value pair to be sent back to the client. The client uses the new values to update its cookies if the information already exists. If the information doesn't exist, new cookies will be created.

    Remember that cookie information is exchanged within HTTP headers; cookies must be sent before the script generates any output.

    More Programming Basics Articles
    More By bluephoenix

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP
    - Calendar Construction with PHP
    - PHP`s Calendar Package
    - Getting Modified Versions and Correct Dates ...
    - Combining Date Functions in PHP
    - Using PHP for Date and Time in Programming
    - More Exception Handling with PHP
    - Exception Handling in PHP
    - Error Logging and Handling Exceptions
    - Configuration Directives for Error and Excep...
    - Error and Exception Handling
    - Python Modules for Games


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap