Building SQL Queries
How to Store Different Types of MySQL Data
PHP Syntax

Check Whether PHP Cookies Are Enabled

You use the setcookie() function in PHP to set a cookie in the browser. Then if your program can read that cookie, you know that cookies are enabled. The setcookie() function accepts several arguments to define the behavior of the cookie.

For example, you set the name of the cookie, but you can also set how long the cookie will be active and whether it will be used over secure connections only, along with several other options.

Simply set the name of the cookie and a value. You can follow these steps to check if cookies are enabled in your browser.

  1. Open your text editor and create a new empty file.

  2. Within the file, enter the following code:

    <?php
    if (isset($_GET['cookiecheck'])) {
        if (isset($_COOKIE['testcookie'])) {
            print "Cookies are enabled";
        } else {
            print "Cookies are not enabled";
        }
    } else {
        setcookie('testcookie', "testvalue");
        die(header("Location: " . $_SERVER['PHP_SELF'] . "?cookiecheck=1"));
    }
    ?>
  3. Save the file as cookie.php in your document root.

  4. Point your web browser toward http://localhost/cookie.php and you'll see a page like this one.

    image0.jpg

    If cookies aren't enabled in your browser, you'll see a page like the following one.

    image1.jpg

Note: If you'd like to test the page with cookies disabled, you can do so. First, close the browser and then reopen it (without going to the cookie.php page). In Internet Explorer, go to Internet Options. On the Privacy tab, slide the Settings slider up to block all cookies.

In Firefox, go to Options, and select the Privacy tab. Within the History section, select Use custom settings for history and then uncheck the "Accept cookies from sites" check box. Now load the cookie.php page.

Now that you know that cookies are enabled you can safely begin to use sessions.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Write a Class Statement
Special Characters Used in Patterns in PHP and MySQL Web Development
Setting Up Your Computer for PHP and MySQL Web Site Development
How to Build an Object in PHP with Object-Oriented Programming
How to Use the mysql Client
Advertisement

Inside Dummies.com