通过小部件设置第 3 方 cookie,以便我以后可以识别用户
Setting a 3rd party cookie through a widget so I can recognize the user later
我正在尝试通过用户添加到其网站的小部件设置第 3 方 cookie。该小部件位于 user-site.com,托管代码位于 admin-site.com。我的目标是为它们设置一个 cookie,以便小部件可以在安装了小部件的任何网站上识别它们。我不想使用弹出窗口让他们通过我的网站登录。我只是想通过小部件为他们设置一个 cookie,然后能够识别他们。
- 需要2行代码才能使小部件出现; 1 个 .js 文件和 1 行 html.
将小部件添加到 user-site.com 后,会出现两个文本字段; 1 个用于您的姓名,1 个用于您的电子邮件。然后单击按钮提交。然后我为用户 return 一个 user_id 并尝试通过小部件设置 cookie。这不起作用...
user-site.com 需要在 html 中添加 2 行代码才能使小部件显示
<script src="http://example.com/widget.js" type="text/javascript" async="true"></script>
<div id="widget-container" class="8AORPIY0"></div>
widget.js
// Create the input fields and submit button
var login_name = '<input type="text" name="name" />';
var login_email = '<input type="email" name="email" />';
var login_button = '<input type="button" value="Login" />';
var add_fields_to_html_snippet = login_name + login_email + login_button;
$('#widget-container').html(add_fields_to_html_snippet);
$.ajax({
url : "http://example.com/widget_update.php",
type: "GET",
data: { name:name, email:email },
dataType:"jsonp",
jsonp:"mycallbackupdate",
success:function(data) { // The server side sends back the users id which will become the value of the 3rd party cookie
// This is the value *user_id returned from server side
var value = data.user_id;
// I'm trying to set a cookie but it doesn't work
document.cookie = 'cookie1=' + user_id + '; expires=Fri, 3 Aug 2016 20:47:11 UTC; path=/'
}
});
如何通过嵌入式小部件成功地为用户设置cookie?我希望能够识别来自使用此小部件的任何站点的用户。
更新
我也试过从服务器端设置 cookie:
$_COOKIE['user_id'] = $user_id;
$expire = time() + 60 * 60 * 24 * 365;
setcookie('cookie1', $_COOKIE['user_id'], $expire);
但是,我无法从 widget.js 获取 cookie1
的值。我试图通过使用此获取 cookie 值:
alert(document.cookie.indexOf("cookie1")); // This doesn't return the same value I set the cookie to from the HTTP header
但是,这 return 与我在 HTTP header 中设置 cookie 的值不同。如何在 widget.js 中调用相同的值?
JavaScript 始终在其嵌入的 HTML 文档的上下文中运行,因此您不能使用 JavaScript.
设置第三方 cookie
在 JSONP 响应中使用 HTTP headers 设置 cookie(请记住,许多浏览器都配置为完全阻止第三方 cookie)。
设置来自 widget_update.php
的 cookie。
您应该设置“安全”属性(假设 admin-site.com 通过 HTTPS 提供服务)以防止在最终用户使用 public WiFi 时窃取该信息。
您还需要将 SameSite 属性设置为“None”。现代浏览器正在努力提高互联网安全性并防止数据泄露,因此如果您没有明确说明第三方网站允许使用“samesite=none”.
,cookie 可能会被阻止
我正在尝试通过用户添加到其网站的小部件设置第 3 方 cookie。该小部件位于 user-site.com,托管代码位于 admin-site.com。我的目标是为它们设置一个 cookie,以便小部件可以在安装了小部件的任何网站上识别它们。我不想使用弹出窗口让他们通过我的网站登录。我只是想通过小部件为他们设置一个 cookie,然后能够识别他们。
- 需要2行代码才能使小部件出现; 1 个 .js 文件和 1 行 html.
将小部件添加到 user-site.com 后,会出现两个文本字段; 1 个用于您的姓名,1 个用于您的电子邮件。然后单击按钮提交。然后我为用户 return 一个 user_id 并尝试通过小部件设置 cookie。这不起作用...
user-site.com 需要在 html 中添加 2 行代码才能使小部件显示
<script src="http://example.com/widget.js" type="text/javascript" async="true"></script>
<div id="widget-container" class="8AORPIY0"></div>
widget.js
// Create the input fields and submit button
var login_name = '<input type="text" name="name" />';
var login_email = '<input type="email" name="email" />';
var login_button = '<input type="button" value="Login" />';
var add_fields_to_html_snippet = login_name + login_email + login_button;
$('#widget-container').html(add_fields_to_html_snippet);
$.ajax({
url : "http://example.com/widget_update.php",
type: "GET",
data: { name:name, email:email },
dataType:"jsonp",
jsonp:"mycallbackupdate",
success:function(data) { // The server side sends back the users id which will become the value of the 3rd party cookie
// This is the value *user_id returned from server side
var value = data.user_id;
// I'm trying to set a cookie but it doesn't work
document.cookie = 'cookie1=' + user_id + '; expires=Fri, 3 Aug 2016 20:47:11 UTC; path=/'
}
});
如何通过嵌入式小部件成功地为用户设置cookie?我希望能够识别来自使用此小部件的任何站点的用户。
更新
我也试过从服务器端设置 cookie:
$_COOKIE['user_id'] = $user_id;
$expire = time() + 60 * 60 * 24 * 365;
setcookie('cookie1', $_COOKIE['user_id'], $expire);
但是,我无法从 widget.js 获取 cookie1
的值。我试图通过使用此获取 cookie 值:
alert(document.cookie.indexOf("cookie1")); // This doesn't return the same value I set the cookie to from the HTTP header
但是,这 return 与我在 HTTP header 中设置 cookie 的值不同。如何在 widget.js 中调用相同的值?
JavaScript 始终在其嵌入的 HTML 文档的上下文中运行,因此您不能使用 JavaScript.
设置第三方 cookie在 JSONP 响应中使用 HTTP headers 设置 cookie(请记住,许多浏览器都配置为完全阻止第三方 cookie)。
设置来自 widget_update.php
的 cookie。
您应该设置“安全”属性(假设 admin-site.com 通过 HTTPS 提供服务)以防止在最终用户使用 public WiFi 时窃取该信息。
您还需要将 SameSite 属性设置为“None”。现代浏览器正在努力提高互联网安全性并防止数据泄露,因此如果您没有明确说明第三方网站允许使用“samesite=none”.
,cookie 可能会被阻止