Detect if cookies are enabled in PHP

后端 未结 4 754
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 23:43

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 the

相关标签:
4条回答
  • 2020-12-02 00:23

    There's no need to save the original URL and redirect to it afterwards. You can perform a transparent redirect via AJAX which doesn't trigger a page reload. It's very simple to implement. You can check my post here: https://stackoverflow.com/a/18832817/2784322

    0 讨论(0)
  • 2020-12-02 00:25

    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 讨论(0)
  • 2020-12-02 00:43

    I think this is easiest solution. Doesn't require separate files and allows you to proceed with script if cookies are enabled:

    $cookiesEnabled = true;
    
    if (!isset($_COOKIE['mycookie'])) {
        $cookiesEnabled = false;
        if (!isset($_GET['cookie_test'])) {
            setcookie('mycookie', 1, 0, '/');
            @ob_end_clean();
            $_SESSION['original_url'] = $_SERVER['REQUEST_URI'];
            $uri = $_SERVER['REQUEST_URI'];
            $uri = explode('?', $uri);
            $q = (isset($uri[1]) && $uri[1])?explode('&', $uri[1]):array();
            $q[] = 'cookie_test=1';
            $uri[1] = implode('&', $q);
            $uri = implode('?', $uri);
            header('Location: '.$uri);
            die;        
        }
    } else if (isset($_GET['cookie_test'])) {
        @ob_end_clean();
        $uri = $_SESSION['original_url'];
        unset($_SESSION['original_url']);
        header('Location: '.$uri);
        die;    
    }
    
    // if (!$cookiesEnabled) ... do what you want if cookies are disabled
    
    0 讨论(0)
  • 2020-12-02 00:47

    HTTP_REFERER did not work for me, seems like REQUEST_URI is what I need.

    Here is the code I finally used:

    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;
                $go_to = $_SESSION['cookie_test_caller'];
                unset( $_SESSION['cookie_test_caller'] );
                header("Location: $go_to");
                exit();
                }
            }
        if( !isset( $_COOKIE['PHPSESSID'] ) ){
            $_SESSION['cookie_test_caller'] = $_SERVER['REQUEST_URI'];
            header('Location: index.php?cookie_test=1');
            exit();
            }
        }
    // ------------------------------- //
    
    0 讨论(0)
提交回复
热议问题