ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

PHP and Cookies; a good mix!

(Page 3 out of 5)

Before you start using cookies, you must make sure your visitor has cookies enabled. This can be done with a simply PHP checking script. Unfortunately, the PHP page needs to reload to check for cookies. But this can be done very transparently, and your visitor should hardly notice anything.

The following example will first set a test cookie, then reload the page, and finally check whether cookies are enabled.


error_reporting (E_ALL ^ E_WARNING ^ E_NOTICE);

// Check if cookie has been set or not
if ($_GET['set'] != 'yes') {
        // Set cookie
        setcookie ('test', 'test', time() + 60);

        // Reload page
        header ("Location: checkcookies.php?set=yes");
} else {
        // Check if cookie exists
        if (!empty($_COOKIE['test'])) {
                echo "Cookies are enabled on your browser";
        } else {
                echo "Cookies are NOT enabled on your browser";
        }
}
?>

Run the code above, and see what the output is. Check if cookies are enabled in your browser. If they're not enabled, then you can enable them by going to your browser's options. Unfortunately, this is different from each browser, so I can't give you exact instructions. But Google can.

« Previous: Using Cookies
Next: Storing Arrays »



One Response to “PHP and Cookies; a good mix!”

  1. Herojit Says:

    This site is very helpful to me.
    I expect more examples about session and cookies from this site.
    Thanks a lot.

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction
  2. Using Cookies
  3. Checking if cookies are enabled
  4. Storing Arrays
  5. In Conclusion...
Bookmark Article
Download Article
PDF
Download this article as a PDF file