简体   繁体   English

为什么 Chrome 扩展没有在跨域请求中设置 PHP Cookie?

[英]Why is PHP Cookie not set in cross-origin request by the chrome extension?

I have a chrome extension that uses XMLHttpRequest to my host server.我有一个 chrome 扩展,它使用 XMLHttpRequest 连接到我的主机服务器。 When I set a PHP cookie with XMLHttpRequest inside popup.js it is visible only in popup.js in next XMLHttpRequest.当我在 popup.js 中使用 XMLHttpRequest 设置 PHP cookie 时,它仅在下一个 XMLHttpRequest 中的 popup.js 中可见。 But when the extension is running on some internet page, background.js make XMLHttpRequest to my host server again but then the cookie is not visible.但是,当扩展程序在某个 Internet 页面上运行时,background.js 再次将 XMLHttpRequest 发送到我的主机服务器,但是 cookie 不可见。 How do I make PHP cookies visible by all requests to that server?如何使 PHP cookies 对该服务器的所有请求可见?

PHP when setting a cookie: PHP 设置 cookie 时:

setcookie("name","john", time() + 3600*24*1000, "/");

Background, content and popup are independent and don't share state, including cookies.背景、内容和弹窗是独立的,不共享 state,包括 cookies。 But you could communicate between them https://developer.chrome.com/extensions/messaging但是您可以在它们之间进行通信https://developer.chrome.com/extensions/messaging

So if you want to initialize your request inside popup then you have to pass the cookie to your background if you want to use it there.所以如果你想在弹出窗口中初始化你的请求,那么如果你想在那里使用它,你必须将 cookie 传递到你的后台。

I managed to fix the problem.我设法解决了这个问题。 Cookie did not recognize the host domain. Cookie 无法识别主机域。 I followed again instructions from PHP manual and then changed how I set a cookie to:我再次遵循PHP 手册中的说明,然后将我设置 cookie 的方式更改为:

setcookie($cookie_name,$cookie_value, time() + 3600*24, "/","mydomain.com", 1);

This time i used variabels $cookie_name and $cookie_value but this had no effect to solution.这次我使用了变量 $cookie_name 和 $cookie_value 但这对解决方案没有影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM