Cookie for first entered page. setcookie not working for logged out users?

I want to have a function that sets a cookie on the first page a user enters. So, if the user signs up, I can save the content of the cookie in his profile, so that I know which page a user enters before he signs up.

In the functions.php I have this function:

function process_post(){
    if (!is_user_logged_in()){
        if (!isset($_COOKIE["firstenteredurl"])) {
            $firstentered_url = $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
            if($firstentered_url!="" && strpos($firstentered_url,"admin-ajax")===false && strpos($firstentered_url,"wpuf_registration_activation")===false){
                setcookie("firstenteredurl", $firstentered_url, time()+86400*30, '/', '.myurl.com'); // 30 Tage
            }
        }

    }
}
add_action('init', 'process_post',10,2);

On the first page after signing up, I save the cookie in the user profile like this:

if ($current_user->firstentered_url==""){
                if (!isset($_COOKIE["firstenteredurl"])) {
                    update_user_meta( $current_user->ID, 'firstentered_url', "empty" );
                }else{
                    $firstentered_url = str_replace("%2F","/",$_COOKIE["firstenteredurl"]);
                    update_user_meta( $current_user->ID, 'firstentered_url', $firstentered_url );
                }
}

The problem is, that over 50% of the users get a “empty” entry, and I cannot imagine that 50% don’t allow cookies.

The strange thing is, if I remove the if (!is_user_logged_in()){ in the first code, it suddenly works for all users, but instead of the “entry” entries I get the first page a user enters, when he is logged in the first time.

So it looks like for some users, the setcookie only works if they are logged in, which is strange.

Do you know what is going on here?

thanks

Anyone?

Does someone have an idea what is wrong here, please?

another try.