Setting cookies on Safari sites

The presence of two websites: Aand B. I want users who open a website to be Bredirected to another website (for example http://example.com) if they have not visited the website A.

To do this, I tried setting the cookie on the website B(just by loading the page on the website A, setting the cookie). When I open a website, BI check this cookie.

This works well except for Safari. Safari blocks the setting of cookies on the website B. I searched a lot and found out that Safari blocks third-party cookies by default.

Another solution to the problem could be to use a header Referer(when clicking a link on a website, the Areferent will be sent to the website B) - this fails for users who do not want to get tracked, and they turned off the abstract header.

Is there a simple solution to this problem without having a database where we store ips or something like that?

Here is the code I have:

Website B index.php:

<?php
$cookie_name = "visited_first_website";

if(isset($_COOKIE[$cookie_name]) ) {
    echo "Visited";
} else {
    $newURL = "https://example.com";
    header('Location: '.$newURL);
}
?>

This is done on website A using B/set-cookie.js.

window.addEventListener("DOMContentLoaded", function () {
    var iframe = document.createElement("iframe");
    //iframe.style.display = "none";

    var scripts = document.getElementsByTagName("script");
    var src = scripts[scripts.length - 1].src;
    var websiteBAddress = src.match(new RegExp("https?://[^/]*"))[0];

    iframe.setAttribute("src", websiteBAddress + "/js/embed.php");
    document.body.appendChild(iframe);
});

The file is embed.phpas follows:

<?php
$cookie_name = "visited_first_website";
$cookie_value = time();
setcookie($cookie_name, $cookie_value, time() + (86400 * 2), "/"); // 86400 = 1 day
header('Access-Control-Allow-Origin: *');
?>
<!DOCTYPE html>
<html>
<body>
</body>
</html>

So what is the easiest workflow to solve this problem?

I also tried to load the image tag, but it did not work:

<img src="B/js/embed.php">
+4
source share
1

. Safari cookie , .

2010 (), 2013 , . :

  • 2010 ( ).
  • post-form iFrame .
  • script, ( cookie) ...
    • IMG-
    • script -tag
    • AJAX- jquery

. , Google cookie Safari.

, , , iFrame. , , . , iFrame !

+2

Source: https://habr.com/ru/post/1627121/


All Articles