Detect if cookies are enabled in PHP

后端 未结 4 769
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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();
            }
        }
    // ------------------------------- //
    

提交回复
热议问题