php set cookie -javascript读取cookie
我正在开发游戏。
我需要在PHP和JavaScript之间交换数据。
php生成一个值(秘密文本),这是 游戏。
php通过base64_encode()编码值,enduser找不到 解决方案(很容易)。
php将编码值存储在cookie中。
javascript读取cookie值。
javascript解码值,window.atob()
javascript检查用户条目与cookie生成的秘密文本。
这种方法可用于LocalHost,但在Web服务器上不起作用。
以下是我正在使用的代码。 ”参数设置为“ mydomain.com”没有解决
问题
function setMyCookie($name, $value) {
setcookie($name, $value, [
'expires' => time() + 86400,
'path' => '/',
'domain' => '',
'secure' => true,
'httponly' => false,
'samesite' => 'None',
]);
}
)
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
(在PHP中,将“域 如何在网络服务器上解决我的问题?
问题2 : 您可能会推荐另一种方法(除饼干外)吗?
谢谢 BM
I am developing a game.
I need to exchange data between PHP and JavaScript.
PHP generates a value (a secret text), which is the solution of the
game.PHP encodes the value via base64_encode(), enduser should not find
the solution (easily).PHP stores the encoded value in Cookie.
JavaScript reads the cookie value.
JavaScript decodes the value, window.atob()
JavaScript checks user entry vs. generated secret text from the cookie.
This approach works on localhost, but not on web server.
Following is the code I am using.
(in PHP, setting "domain" param with my website such "mydomain.com" did not solve the issue)
PHP
function setMyCookie($name, $value) {
setcookie($name, $value, [
'expires' => time() + 86400,
'path' => '/',
'domain' => '',
'secure' => true,
'httponly' => false,
'samesite' => 'None',
]);
}
JavaScript
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
Question 1:
How can fix my issue on webserver?
Question 2:
Is there another approach (other than cookie) which you may recommend?
Thanks
BM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论