<?php
//Set session var as null
session_start();
if(!isset($_SESSION['view'])){
$_SESSION['view']= 1;
}else{
$_SESSION['view']++;
}
//If it is the first time the site is being opened
if ($_SESSION['view'] == 1) {
//Show the dialog & reset the var as not null
echo "<script type=\"text/javascript\">";
echo "jQuery(document).ready(function($){";
echo "$(\"#dialog-1\").dialog({";
echo "draggable: false,";
echo "modal: true,";
echo "autoOpen: true,";
echo "});";
echo "});";
echo "</script>";
$_SESSION['view']++;
?>
<div id="dialog-1">
Registration now open
</div>
<?php
}
unset($_SESSION['view']);
?>
当用户重新加载或退出页面时,会话需要重新设置。我已经显示了对话框,但$ _SESSION ['view']变量没有增加。有没有更好的方法来做到这一点?
答案 0 :(得分:3)
你可以在jquery中使用html5 localstorage。
这是一个解决方案
jQuery(document).ready(function($){
if (localStorage.getItem("view") === null) {
localStorage.setItem("view", "true");
//your code here
$("#dialog-1").dialog({
draggable: false,
modal: true,
autoOpen: true,
});
}
});
并将其重置为下次打开
$(window).on('beforeunload', function(){
localStorage.removeItem('view');
return '';
});
答案 1 :(得分:-1)
我没有使用localStorage,而是使用了sessionStorage,这解决了这个问题。