【发布时间】:2017-12-06 21:14:07
【问题描述】:
我设置了一个通用 cookie,它适用于一次运行的情况。但是,我需要做的是通过用户名设置一个 cookie,以便每个用户只显示一次消息。我正在使用 php 为链接到 mysql 的用户名设置会话。
我已经发布了我用于通用 cookie 的代码,想知道是否可以修改它以针对用户名而不是通用 cookie。
如果有人能提供帮助,我将不胜感激。谢谢
<script>
$(document).ready(function() {
if (document.cookie.indexOf("runOnce") < 0) {
$("#message3").dialog({
modal: true,
buttons: {
'Confirm': function() {
document.cookie = 'runOnce = true; expires = Fri, 31 Dec 9999 23:59:59 GMT; path = /';
$(this).dialog("close");
}
/* ,
No: function () {
$( this ).dialog( "close" );
}*/
}
});
// Hide the close button
jQuery("a.ui-dialog-titlebar-close").hide();
//Disable the confirm button on page load
$(":button:contains('Confirm')").attr("disabled", true).addClass("ui-state-disabled");
// Uncheck the 'agree' checkbox
$(function() {
$('#message3 input[type=checkbox]').attr('checked', false);
});
// Hide red message paragraph
$('#agreemsg').hide();
// Function to enable and disable confirm button based on checkbox click event
$("#checkme").click(function() {
var isChecked = $("#checkme").is(":checked");
if (isChecked) {
//alert("CheckBox checked.");
$(":button:contains('Confirm')").removeAttr('disabled').removeClass('ui-state-disabled');
$('#agreemsg').show();
} else {
//alert("CheckBox not checked.");
$(":button:contains('Confirm')").prop("disabled", true).addClass("ui-state-disabled");
$('#agreemsg').hide();
}
});
//}
};
});
</script>
【问题讨论】:
-
唯一不同的是cookie的名称,因此您可以识别它。您需要用户名本身,无论如何它都是公开的,所以这不是问题。我看到你正在使用 jQuery。你所要做的就是
$.cookie('cookie_name', 'value')。 -
我认为您最好在第一次登录或加载页面时在数据库中为每个用户保存一次运行并将该信息发送到浏览器,否则它将为每个用户每个浏览器运行一次或如果他们使用隐私浏览或删除 cookie,则更频繁。
-
我在这里没有看到任何 php
-
@Loren 有一个很好的观点。这样就安全多了。但是,请考虑比对每个请求执行 1 个额外的数据库查询。这并不理想。
-
“我正在使用 php 为链接到 mysql 的用户名设置会话。” - 我们如何知道其中的某些内容是否也没有失败?
标签: javascript php jquery cookies