重定向回到引用者

I am trying to get the referrer url passed as a variable so that I can link back to that page if a cookie isn't already set when a user lands on said page.

Currently looking into $_SERVER['HTTP_REFERER'] but not too sure how that works and how I would pass that in the below function.

Any help would be greatly appreciated.

function cookie_check() {
    $post_ID = get_the_ID();
    $parent_ID = wp_get_post_parent_id( $post_ID );

    if($parent_ID == 9 || $post_ID == 9) {

            if(!isset($_COOKIE["mycookie"])) {
                header("Location: http://test.dev");
                die();
            }
    }
    if($parent_ID == 7 || $post_ID == 7) {
            if(!isset($_COOKIE["diffcookie"])) {
                header("Location: http://test.dev");
                die();
            }
    }
}