问题设置包含文件的PHP cookie
我有下面的脚本,它创建一个cookie并更新数据库记录中的站点计数器,如果用户一天内没有访问该网站,否则,它将只显示当前的访问次数。当前,每当我重新加载索引页时,cookie都会显示NULL,因此表的更新次数会超过其应更新的次数。包含此脚本时,如何在索引页上维护cookie值问题设置包含文件的PHP cookie,php,cookies,Php,Cookies,我有下面的脚本,它创建一个cookie并更新数据库记录中的站点计数器,如果用户一天内没有访问该网站,否则,它将只显示当前的访问次数。当前,每当我重新加载索引页时,cookie都会显示NULL,因此表的更新次数会超过其应更新的次数。包含此脚本时,如何在索引页上维护cookie值 if (empty($_COOKIE["visits"])) { // increment the counter in the database mysql_query("
if (empty($_COOKIE["visits"])) {
// increment the counter in the database
mysql_query("UPDATE visit_counter ".
" SET counter = counter + 1 ".
" WHERE id = 1");
/* Query visit_counter table and assign counter
value to the $visitors variable */
$QueryResult = mysql_query("SELECT counter ".
" FROM visit_counter WHERE id = 1");
// Place query results into an associative array if there are any
if (($row = mysql_fetch_assoc($QueryResult)) !== FALSE) {
$visitors = $row['counter'];
} else {
// else if this is the first visitor set variable to 1
$visitors = 1;
}
// Set cookie value
setcookie("visits", $visitors, time()+(60*60*24));
} else {
$visitors = $_COOKIE["visits"];
}
cookie脚本包含在索引文件中,因此以下是索引文件
<?php include("Includes/cookie.php"); ?>
var_dump($_COOKIE["visits"]); /* Always returns NULL on this page but
returns the cookies real value if
run straight from cookie.php script */
/* Some main page content goes here */
/* The cookie value is echoed in the footer file that is included by
creating a statement that says echo "total visitors: ".$visitors; */
<?php include("Includes/footer.php"); ?>
变量转储($_COOKIE[“访问”]);/*此页上始终返回NULL,但
返回cookies的实际值,如果
直接从cookie.php脚本运行*/
/*这里有一些主页内容*/
/*cookie值将在包含的页脚文件中回显
创建一个声明,说明echo“总访客:.”访客*/
问题出在设置cookie
将其设置为:
<?php
// Set cookie value
setcookie("visits", $visitors, time()+(60*60*24));
?>
到
祝你好运你昨天不是已经问过这个问题了吗?@Sébastien他一定是误读了“微笑,又是新的一天”这句话。在你加入cookie.php之前,设置
$visions=$\u cookie['visions']
,而不是在其他文件中使用它。@skobaljic感谢你的建议,但它似乎仍然不起作用,我只是不明白问题是什么。正如安东尼奥在下面所说的,可能是cookie示波器的问题。尝试找出cookie的可用位置。您可以使用一些开发人员工具,或者只是在浏览时运行此document.cookie.split(“visions=”).pop().split(“;”).shift()浏览器检查器中的代码>或Firebug。另一种可能性是:服务器只允许HttpOnly cookie,这意味着:除非客户端发送HTTP请求,否则不会设置cookie。我认为这个参数可以工作,并且已经尝试过了,但仍然不工作,我被难倒了。
<?php
// Set cookie value
setcookie("visits", $visitors, time()+(60*60*24),'/'); // Define the cookie path to be used on this domain
?>