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;
}