Safari中的PHP cookie识别

Hi i'm encountering this strange bug in safari, i have an age-checker on a website en whenever the visitor is older than 18 i create an cookie with javascript and use window.location.href to forward him to the homepage.

In the homepage i have the following script:

if(!$_COOKIE['legal'] && icl_object_id(get_page_by_path('age-checker')->ID, 'page', false, ICL_LANGUAGE_CODE ) != $post->ID) {
   header('Location: '. get_permalink(icl_object_id(get_page_by_path('age-checker')->ID, 'page', false, ICL_LANGUAGE_CODE )));
}

Which makes the user redirect if the cookie "legal" is not set and he's not already on the age-checker page.

Now on Chrome and firefox everything works fine (haven't checked IE yet), but in safari whenever i go through the age-checker and land on the homepage it says that the cookie "tuned" is not defined yet, however i do see it in the storage pannel in my console. After i reload the page it does recognise the cookie.

Anyone have any idea how to fix this?

EDIT:

Javascript does recognise cookies on first request, php doesn't.

It's not the nicest solution but this worked for me:

Replacing this little script:

if(!$_COOKIE['legal'] && icl_object_id(get_page_by_path('age-checker')->ID, 'page', false, ICL_LANGUAGE_CODE ) != $post->ID) {
    header('Location: '. get_permalink(icl_object_id(get_page_by_path('age-checker')->ID, 'page', false, ICL_LANGUAGE_CODE )));
}

By:

if(!$_COOKIE['legal'] && icl_object_id(get_page_by_path('age-checker')->ID, 'page', false, ICL_LANGUAGE_CODE ) != $post->ID) {
    header('Location: '. get_permalink(icl_object_id(get_page_by_path('age-checker')->ID, 'page', false, ICL_LANGUAGE_CODE )));
} else if($_COOKIE['legal'] && icl_object_id(get_page_by_path('age-checker')->ID, 'page', false, ICL_LANGUAGE_CODE ) == $post->ID) {
    header('Location: '. get_home_url());
}

And in JavaScript where i set the cookie: I simply reload the page with

window.location.reload();