Check Whether PHP Cookies Are Enabled - dummies

By Steve Suehring, Janet Valade

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:

    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.


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


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.