我在配置文件(main.php)中的会话配置是:
'session' => array(
'class' => 'CDbHttpSession',
'timeout' => 60*60, // 1 hour
),
我把这段代码放在布局文件中:
<?php if (!Yii::app()->user->isGuest) { ?>
<meta http-equiv="refresh" content="<?php echo Yii::app()->params['session_timeout'];?>;"/>
<?php } ?>
到目前为止,会议结束时没有公告。当我刷新浏览器时,会话已经结束。
在会话(或差不多)过期后,我想告诉用户会话将像倒数计时器对话框一样过期。
我尝试了此扩展程序http://www.yiiframework.com/extension/timeout-dialog/。但它配置不适用于我的网站。当会话还剩1分钟时,扩展显示对话框,但是当我单击按钮以恢复会话时,它不起作用,因为会话仍在运行。我知道,因为当我刷新浏览器时,会话已经过期了。
那么,我必须做什么?
至少,我想知道如何在浏览器刷新时获得会话的剩余时间,所以我可以自己修改公告。
谢谢:)
答案 0 :(得分:0)
我想我自己解决了这个问题。
这里有解释:
知道Session在服务器上工作很重要。当浏览器访问服务器时,将重新启动会话超时。没有办法知道会议的剩余时间。
因此,警告用户会话将过期必须使用javascript或jQuery在客户端中处理。我创建了处理程序,当网站空闲(没有活动)或会话超时几乎到期时,它将警告用户。将通过确认对话框警告用户,该对话框询问用户是继续登录还是注销。当用户决定继续登录时,客户端将执行ajax来执行服务器中的任何脚本(因为这会重新启动会话超时)。
因为我的Yii网站使用bootstrap 3,所以我创建的对话框面板将使用bootstrap代码。
这是在视图布局中:
<script>
/* configuration */
var count = <?php echo Yii::app()->session->getTimeout(); ?>; //session
var countdown = 60; //warning 60 seconds
var idle = 10; //warning when idle 10 seconds
var urluser = <?php echo "'".Yii::app()->createUrl('site/lock', array('id'=>Yii::app()->user->id))."'"; ?>;
var idlecountdown = countdown;
var idletime = idle;
var show = true;
var counter = setInterval(timer, 1000); //1000 will run it every 1 second
function timer(){
count = count-1; idletime = idletime-1;
if (count <= 0 ){
clearInterval(counter);
// counter ended, go to the lockscreen
window.location.href = urluser;
return;
}
if (count <= countdown || idletime <= 0){
count = count<=countdown?count:countdown;
// show warning
if(show){
document.getElementById('tombolmodal').click();
show = false;
}
document.getElementById('sisadetik').innerHTML = count;
}
window.onmousemove = function(){ idletime=idle };
window.onclick = function(){ idletime=idle };
window.onmousedown = function(){ idletime=idle };
window.onscroll = function(){ idletime=idle };
window.onkeypress = function(){ idletime=idle };
// when button stay signin cliked
document.getElementById('staysignin').onclick = function() {
// access properties using this keyword
if ( this.click ) {
count = <?php echo Yii::app()->session->getTimeout(); ?>;
show = true;
idletime = idle;
}
};
document.getElementById('forcelogout').onclick = function() {
// access properties using this keyword
if ( this.click ) {
window.location.href = urluser;
return;
}
};
}
</script>
<!-- design of modal -->
<button id='tombolmodal' style="display:none" type="button" class="btn btn-info btn-lg" data-backdrop="static" data-keyboard="false" data-toggle="modal" data-target="#myModal">Open Large Modal</button>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content panel-danger">
<div class="modal-header panel-heading" style="border-radius: inherit;">
<h4 class="modal-title"><span class="glyphicon glyphicon-warning-sign"></span>
ATTENTION!</h4>
</div>
<div class="modal-body text-center">
<h3 class="text-danger">
It seems you are in inactive
</h3>
<p>
<b><span id="sisadetik"></span> seconds</b> left to logout your account
</p>
</div>
<div class="modal-footer">
<!-- From this line, I Used YiiBooster Extension -->
<?php $this->widget('booster.widgets.TbButton',
array(
'buttonType' => 'ajaxSubmit',
'context' => 'primary',
'label' => 'Keep Sign In',
'url' => CController::createUrl('site/keepalive'),
'ajaxOptions' => array(
'type' => 'GET',
),
'htmlOptions' => array(
"data-dismiss"=>"modal",
'id'=>'staysignin',
)
)
);
?>
<?php $this->widget('booster.widgets.TbButton',
array(
'context' => 'default',
'label' => 'Logout',
'htmlOptions' => array(
"data-dismiss"=>"modal",
"id"=>"forcelogout"
)
)
);
?>
<!-- End of YiiBooster Extension -->
</div>
</div>
</div>
</div>
这是我的SiteController:
public function actionKeepalive()
{
if(Yii::app()->request->isAjaxRequest){
Yii::app()->session->open();
}
}
public function actionLock($id)
{
$user = User::model()->findByPk($id);
Yii::app()->user->logout();
$this->render('lock', array( //refers to lockscreen page
'user'=>$user,
));
}
并且不要忘记来编写上述问题中的代码。
该代码在我的网站上运作良好。
您还可以通过此链接查看Yii2中的实施:http://www.hafidmukhlasin.com/2014/10/01/yii2-create-lock-screen-web-application-with-javascript/
感谢。如果我错了,请纠正我