Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

PHP and Cookies, A Good Mix!

By Dennis Pallett
2005-05-26


Checking if cookies are enabled

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.

<?php
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 <b>NOT</b> 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.

Tutorial Pages:
» Introduction
» Cookies in a nutshell
» Using Cookies
» Checking if cookies are enabled
» Storing Arrays
» In Conclusion...


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1

Ask A Question
characters left.