我正在使用 jQuery 来让用户投票赞成/反对文章。当用户投票时,cookie 被设置为用户只能对一篇文章投票一次(不刷新 cookie)。这工作正常,除非用户投票并在投票后立即刷新页面:
jQuery:
$.post('vote.php',{id:id,field:field},function(){
// change button graphics
});
PHP:
setcookie(User::$cookieStart . User::$db_fields[$field_index],
$cookie . ':' . $id, time()+60*60*24*30,'/');
// This calls a function that effects the db
User::actionArticleTable($id,$field_index,1);
当用户刷新时,cookie 未设置但 mysql-info 已更改,即使 db 代码在 cookie 代码之后也是如此。
最佳答案
Cookie 只是 HTML 页面中的标题项,因此在加载 vote.php 页面之前,cookie 不会发生任何变化。如果用户在 vote.php 页面返回之前刷新主页,则不会设置 cookie。下次用户访问此页面时,cookie 没问题,但如果您想立即阻止投票,请考虑通过 javascript 设置 cookie。
function setCookie(c_name,value,exdays) {
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
关于php - 未设置 Cookie 但数据库已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13702302/