2017-01-25 93 views
0

长话短说,我需要在5次不成功的登录尝试和30分钟(这是多久用户被阻止登录)之后销毁会话,然后重新加载页面,以便登录窗体再次出现。我正在使用SESSIONS,并且大部分事情都在我的网站上运行。如果他们达到登录尝试的限制,我的表单将消失,SESSION看起来会破坏,但页面不会重新加载(我正在使用JavaScript)。如何在会话销毁后重新加载页面?

这里是PHP代码的网页上的生活与形式:

<?php 
include "includes/auth.php"; 
$_login_count = $_SESSION['login_count']; 
?> 

<!DOCTYPE html> 
<html> 
<body> 
<? 

$_show_form = false; 

if($_login_count < 5) { 
    $_show_form = true; 
} else { 
    if(isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 30)) { 
     // last request was more than 30 minutes ago 
     session_unset();  // unset $_SESSION variable for the run-time 
     session_destroy(); // destroy session data in storage 
     header('Location: http://localhost:8000/login.php'); 
    } 
} 

$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp 

if ($_show_form) {?> 
    <div class="loginBox" id="login-box"> 
     <form id="login-form" action="" method="post" > 
      <label for="uname">Username:</label> 
      <input type="text" name="uname" required autofocus/> 

      <label for="pwd">Password:</label> 
      <input type="password" name="pwd" required/> 

      <input type="submit" name="submit" value="Login" /> 

     </form> 
    </div> 
<?} 
?> 
<script defer src="funcs.js"></script> 
</body> 
</html> 

这是我的JS FUNC:

var checkPage = setInterval(
    function(){ 
     console.log('Checking page.'); 
     var isFormThere = document.getElementById('login-box'); 

     if(isFormThere) { 
      console.log('Found form!'); 
     } else { 
      window.location.reload(true); 
     } 
    }, 
    33000 
); 

我知道我如果JS不正是它应该如何但我只是用这种方式来确保页面正在做我想做的事情。有什么建议么?

+0

我不知道如果我理解正确。所以你想在会话过期时重新设置页面?你能提供步骤和条件吗? – ChaudPain

+0

你等了多长时间JS重新加载?你有33秒的时间间隔。尝试减少此值(例如2000而不是33000)。或者JS代码没有正确加载。控制台中是否有错误? –

+0

@Łukasz - 是的,我已将计时器更改为40秒,使其等待的时间比Cookie的寿命长。它现在重新加载,显示错误消息,但没有形式。第二次在40秒后重新加载(因为它仍然没有看到表单)我在屏幕上看到了表单,但是错误信息仍然存在。 –

回答

0

您使用<? ?>

是您的服务器上启用open_short_tags

否则,<?php ?>

+0

是的,它已启用。 –

0

更换<? ?>你可以在PHP中做到这一点没有JavaScript的。

如果发送PARAM早在头重定向并把它捡起来的响应,如果它的存在,再次重定向:

header('Location: http://localhost:8000/login.php?refresh=true');

if(isset($_GET['refresh']) && $_GET['refresh'] == 'true') 
    header('Location: http://localhost:8000/login.php');