0

What is wrong with this code?

Code detects that user is coming from the expected referral site and adds cookie, but upon page refresh the code does not see the cookie and runs the !isset code and recreates the cookie again. not sure why. any ideas?

Just trying to insure that if its the right referral site, then create the cookie so that that user can use the entire site since they have a cookie and access now is on the cookie and not http_referrer.

below code is in functions.php in child theme and runs under init hook, so it runs on each page. That is what I was told was best.

add_action('init', 'bbb_referral_check' );

function bbb_referral_check() { 

if (!is_admin()){

$allowed_host = 'bbb.com';
$theurl =  $_SERVER['HTTP_REFERER'];
//$theurl =  wp_get_referrer();
$host = parse_url($theurl, PHP_URL_HOST);
$ishost = false;
if (strpos($host, $allowed_host) !== false) { $ishost = true; }

$javascript_ouput = "<script>console.log('Debug Info: " .$ishost. "');
</script>";
echo $javascript_ouput;

//echo "The allowed host: ".$allowed_host; exit;
if ( !isset($_COOKIE["bbb-referral"])) {
$javascript_ouput = "<script>console.log('Debug Info cookie not set: ');</script>";
        echo $javascript_ouput;

        if ($ishost)
    {
        $javascript_ouput = "<script>console.log('Debug Info is host 
match: " .$host. "');</script>";
        echo $javascript_ouput; 

        setcookie( "bbb-referral", "bbb-referral", time() + 1800, COOKIEPATH, COOKIE_DOMAIN);


        } else {        
        $javascript_ouput = "<script>console.log('Debug Info not host match: " .$host. "');</script>";
        echo $javascript_ouput; 


        //Redirect 
                wp_redirect('http://bbb.com' ); exit;
            }

}//isset

    $javascript_ouput = "<script>console.log('Debug Info cookie is set: ');</script>";
        echo $javascript_ouput;

}//admin


    }//function

1 Answer 1

0

You cannot use setcookie() after you've already started outputting stuff to the body (in this case your javascript-debugging, but possibly something else outside that function).

Are you suppressing PHP warnings? It should complain with "Cannot send headers; headers already sent" and tell you where the output was started.

To get rid of the problem regarding the javascript in this function, you could append the script-tags to a variable and echo them in one go after you've set the cookie (or redirected). Something like this (not tested, may contain typos).

function bbb_referral_check() { 
    $javascript_ouput = "";
    if (!is_admin()){

        $allowed_host = 'bbb.com';
        $theurl =  $_SERVER['HTTP_REFERER'];
        //$theurl =  wp_get_referrer();
        $host = parse_url($theurl, PHP_URL_HOST);
        $ishost = false;
        if (strpos($host, $allowed_host) !== false) { $ishost = true; }

        $javascript_ouput .= "<script>console.log('Debug Info: " .$ishost. "');</script>";

        //echo "The allowed host: ".$allowed_host; exit;
        if ( !isset($_COOKIE["bbb-referral"])) {
            $javascript_ouput .= "<script>console.log('Debug Info cookie not set: ');</script>";
            if ($ishost) {
                $javascript_ouput .= "<script>console.log('Debug Info is host match: " . $host . "');</script>";
                setcookie( "bbb-referral", "bbb-referral", time() + 1800, COOKIEPATH, COOKIE_DOMAIN);
                } else {        
                    $javascript_ouput .= "<script>console.log('Debug Info not host match: " .$host. "');</script>";
                    //Redirect 
                    wp_redirect('http://bbb.com' ); exit;
                }

        }//isset

        $javascript_ouput .= "<script>console.log('Debug Info cookie is set: ');</script>";
    }//admin
    echo $javascript_ouput;
}
9
  • I will incorporate this change and see what happens. Im on wpengine and debug is on in the wpconfig.php so I would expect to see header already sent errors, but now that you mention it, I have not seen that. WPE is kind of different than other hosts. :-) Commented Nov 21, 2017 at 2:23
  • I took your advice and changed the code, but that was not it. the code works as it should. if someone logs in from the target site, it recognizes and lets them in, but then it will also let everyone else in, UNTIL I clear the browser caches and cookies. Then it will not let everyone else in and redirect as it should, but then it stays that way even for those from the target site, until I once again clear the cache and cookies. So code works but then hard codes the access and is not dynamic and flexible as it needs to be. I hope this makes sense. Commented Nov 21, 2017 at 18:26
  • "Anybody" from your local browser? Otherwise, your cookies & cache shouldn't matter to other users. Does your host employ some caching mechanism? Maybe they don't take cookies into account (they should)?
    – janh
    Commented Nov 21, 2017 at 18:33
  • I do use WPEngine, and they do some serious caching, in fact every time I change the code, in production, I have to clear the cache. But, since the issue disappears when I clear the browser cache and cookies, then i is not likely the host or ISP caching. And I believe you are right, the caching should work fine on other computers, BUT, if the user goes direct, they will be denied initially and if they THEN login and try, they will then still be denied due to their local caching issue. Either this is possible or not, I need someone who is an expert in these cookies and browser caching matters. Commented Nov 21, 2017 at 20:45
  • I have found that the underlying issue is that the browser seems to be caching http_referrer to my site and if there is none, then it uses the last known referrer to the same site of mine. so its caching http_referrer for websites and if there is none it uses the last one. one. THIS is why the links that should redirect are not. I have no idea how to deal with this. Any suggestions anyone? Commented Nov 22, 2017 at 2:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.