在JS中使用php设置cookie给我一个错误,在第29行,从PHP代码返回一个变量? [英] Setting a cookie with php within JS gives me an error on line 29, returning a variable from PHP code?
问题描述
我想尝试使用下面的PHP文件获取和设置Cookie。我有一个JavaScript函数检查(与 is_set()
)如果cookie存在;如果是这样,使用 access()
获取信息并删除cookie(使用 delete_cookie()
)否与 write()
。
I'm trying to experiment with getting and setting cookies with the PHP file below. I have a JavaScript function for checking to see (with is_set()
) if the cookie exists; if so, get the information with access()
and delete the cookie (with delete_cookie()
) else write a cookie with write()
.
我遇到的问题是这一行,
The problem I'm getting is on this line(s),
SETTINGS [0] =<?
SETTINGS[0] = <?php echo $HTTP_COOKIE_VARS["set_1"].?>;
SETTINGS[1] = <?php echo $HTTP_COOKIE_VARS["set_2"].?>;
错误。
解析错误:语法错误,在第29行的get_set_cookie.php中出现意外的';'
这里是整个代码,随时告诉我,如果有什么不对劲。
Here's the entire code, feel free to tell me if anything else is wrong.
<html>
<head>
<script language = "JavaScript">
var SETTINGS = new Array();
function check_cookie()
{
if(if_set() == true)
{
alert("cookie found!");
access();
delete_cookie();
}
else
{
alert("set cookie");
write();
}
}
function is_set()
{
if((true == <?php if( isset($_COOKIE["set_1"]))?>) && (true == <?php if( isset($_COOKIE["set_2"]))?>))
return true;
else
return false;
}
function access()
{
SETTINGS[0] = <?php echo $HTTP_COOKIE_VARS["set_1"].?>;
SETTINGS[1] = <?php echo $HTTP_COOKIE_VARS["set_2"].?>;
var OUTPUT_TAG = document.getElementById("out_1");
OUTPUT_TAG.innerHTML = SETTINGS[0];
document.getElementById("out_2").innerHTML = SETTINGS[1];
}
function write()
{
SETTINGS[0] = "nothing";
SETTINGS[1] = "much";
setcookie("set_1", "SETTINGS[0]", time()+31000000, "/","", 0);
setcookie("set_2", "SETTINGS[1]", time()+31000000, "/", "", 0);
}
function delete_cookie()
{
setcookie("set_1", "", time()-31000000, "/","", 0);
setcookie("set_2", "", time()-31000000, "/", "", 0);
}
</script>
<style type = "text/css">
#out_1, #out_2
{
color = black;
}
body
{
background-color: white;
}
</style>
</head>
<body onload "check_cookie()">
<p id = "out_1"></p>
<p id = "out_2"></p>
</body>
</html>
推荐答案
替换
<?php echo $HTTP_COOKIE_VARS["set_1"].?>;
<?php echo $HTTP_COOKIE_VARS["set_1"];?>;
//^----- Should be a semicolon !
这篇关于在JS中使用php设置cookie给我一个错误,在第29行,从PHP代码返回一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!