Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7562137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:20:22+00:00 2026-05-30T13:20:22+00:00

I am trying to detect if a user on my page has cookies enabled

  • 0

I am trying to detect if a user on my page has cookies enabled or not. The following code performs the check, but, I have no idea on how to redirect the user to the page they came from.

The script starts a session and checks if it has already checked for cookies. If not, it redirects the user to a test page, and since I had called session_start() in the first page, I should see the PHPSESSID cookie if the user agent has cookies enabled.

The problem is, ths script might be called from any page of my site, and I will have to redirect them back to their selected page, say index.php?page=news&postid=4.

session_start();
// Check if client accepts cookies //
if (!isset($_SESSION['cookies_ok'])) {
    if (isset($_GET['cookie_test'])) {
        if (!isset($_COOKIE['PHPSESSID'])) {
            die('Cookies are disabled');
        } else {
            $_SESSION['cookies_ok'] = true;
            header(-------- - ? ? ? ? ? -------- -);
            exit();
        }
    }
    if (!isset($_COOKIE['PHPSESSID'])) {
        header('Location: index.php?cookie_test=1');
        exit();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:20:23+00:00Added an answer on May 30, 2026 at 1:20 pm

    I think its better to make one file set cookie and redirect to another file. Then the next file can check the value and determine if cookie is enabled. See the example.

    Create two files, cookiechecker.php and stat.php

    // cookiechecker.php
    // save the referrer in session. if cookie works we can get back to it later.
    session_start();
    $_SESSION['page'] = $_SERVER['HTTP_REFERER'];
    // setting cookie to test
    setcookie('foo', 'bar', time()+3600);
    header("location: stat.php");
    

    and

    stat.php

    <?php if(isset($_COOKIE['foo']) && $_COOKIE['foo']=='bar'): 
    // cookie is working
    session_start();
    // get back to our old page
    header("location: {$_SESSION['page']}");
    else:            // show the message ?>
    cookie is not working
    <? endif; ?>
    

    Load cookiechecker.php in browser it’ll tell cookie is working. Call it with command line like curl. It’ll say, cookie is not working


    Update

    Here is a single file solution.

    session_start();
    
    if (isset($_GET['check']) && $_GET['check'] == true) {
        if (isset($_COOKIE['foo']) && $_COOKIE['foo'] == 'bar') {
            // cookie is working
            // get back to our old page
            header("location: {$_SESSION['page']}");
        } else {
            // show the message "cookie is not working"
        }
    } else {
        // save the referrer in session. if cookie works we can get back to it later.
        $_SESSION['page'] = $_SERVER['HTTP_REFERER'];
       // set a cookie to test
        setcookie('foo', 'bar', time() + 3600);
        // redirecting to the same page to check 
        header("location: {$_SERVER['PHP_SELF']}?check=true");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to detect when a user has finished scrolling a web page
I'm trying to detect if the user has selected the All Users or the
I'm trying to figure out how to detect when the current user has changed
I'm trying to detect any touch on the iPhone's UIStatusBar but its not working.
I'm trying to detect when the user has touched a link within a web
Trying to detect when a user clicks Cancel but the $_POST on using var_export($_POST)
I am trying to run the following code, to assign user selected text in
I was trying to do a user.js to /messages page in facebook, but looks
I'm trying to detect if the user landed on my page through a google
I am trying to detect touches, but the touchesBegan method is not being called.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.