如何使用php在注销时删除浏览器的本地存储项目

时间:2016-06-10 02:44:57

标签: javascript php jquery twitter-bootstrap local-storage

如何使用php登出时删除浏览器的本地存储项目。

我想使用php删除注销时保存的所有浏览器数据。

因为我只想在每次登录时显示引导警报框,但不会在每次刷新时显示。

当用户退出时,它将再次重置。当用户再次登录时,它将显示。

我的代码如下所示

$(document).ready(function() {
  if (localStorage.getItem('message1') != 'shown') {
    $('#message1').toggleClass('in');
    window.setTimeout(function() {
      $("#message1").fadeTo(500, 0).slideUp(500, function() {
        $(this).remove();
      });
    }, 10000);
    localStorage.setItem('message1', 'shown')
  }
});
.flyover {
  margin-right: -400px;
  overflow: hidden;
  opacity: 0.9;
  z-index: 1050;
  transition: all 1s ease;
  position: fixed;
  top: 100px;
  right: 20px;
  width: 300px;
  z-index: 2000;
}

.flyover.in {
  margin-right: 10px;
}

.alert {
  color: white ! important;
}

.alert.close {
  font-size: 18px ! important;
  font-weight: 300 ! important;
  line-height: 18px ! important;
  color: white ! important;
}
<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">
</head>
<body>
  <div id="message1" class='alert bg-green flyover'><a href='#' class='close close-x' data-dismiss='alert' aria-label='close'><i class="fa fa-times text-white" aria-hidden="true"></i>
</a><strong>Successfully Logged In<br></strong>
    <h3>Welcome,
      <?php echo ucwords($adminRow['admin_name']); ?>
    </h3>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <!-- Latest compiled and minified JavaScript -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</body>
</html>

5 个答案:

答案 0 :(得分:1)

如果要清除登录中的存储空间:

$('.logout').click(function() {
  $.ajax({
    type: 'GET',
    url: 'logout.php',
    data: {
      id: '123'
    },
    datatype: 'html',
    cache: 'false',
    success: function(response) {
      localStorage.clear();
    },
    error: function() {

    }
  });
});

答案 1 :(得分:0)

由于PHP是服务器端语言,因此使用PHP无法对客户端进行任何操作。你需要javascript来完成这项工作。你可以用PHP做的就是在需要的时候调用javascript。

例如,您可以编写js代码以重置为您想要的任何内容,并在登录页面onload上调用它。因此,每当用户访问登录页面以登录时,您的js将会调用。或者您可以将其绑定到注销按钮上的点击事件。或者您可以将其保留在登录页面中,并在注销时将用户重定向回登录页面,这将调用所需的js。 如果您需要其他人在答案中发布的代码示例。

答案 2 :(得分:0)

我找到了回答我的问题,这可以通过PHP&amp; amp;任何数据库。

在dashboard.php中编写此代码。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dashboard</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

    <style>
       .flyover {


margin-right:-400px;
   overflow: hidden;
   opacity: 0.9;
   z-index: 1050;
   transition: all 1s ease;
   position: fixed;
        top: 100px;
        right: 20px;
        width: 300px;
        z-index: 2000;
}

.flyover.in {
  margin-right: 10px;

}
      .alert{
        color: white ! important;
      }
      .bg-green{background-color: green ! important;}
      .alert.close{
        font-size: 18px ! important;
        font-weight: 300 ! important;
        line-height: 18px ! important;
        color: white ! important;
      }
        </style>
    </head>
    <body>
         <?php 
          $message = $adminRow['message'];
         if($message == 1){
         <div id="message1" class='alert bg-green flyover'><a href='#' class='close close-x' data-dismiss='alert' aria-label='close'><i class="fa fa-times text-white" aria-hidden="true"></i>
    </a><strong>Successfully Logged In<br></strong> 
                <h3>Welcome, <?php echo ucwords($adminRow['admin_name']); ?></h3>
             </div>
          <?php }
           $admin_id = $adminRow['admin_id'];
            $query = mysql_query("UPDATE admin SET message = '0' WHERE admin_id = '$admin_id'");
          ?>
         <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
              <!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
            <script>
               $('#message1').toggleClass('in');
         window.setTimeout(function() {
        $("#message1").fadeTo(500, 0).slideUp(500, function(){
            $(this).remove(); 
        });
        }, 10000);
             </script>
            </body>


    </html>

在logout.php中编写此代码

$admin_id = $adminRow['admin_id'];
$query = mysql_query("UPDATE admin SET message = '1' WHERE admin_id = '$admin_id'");

结果:警报框仅在管理员登录时显示。

逻辑:

当管理员登录检查消息== 1如果是打印消息并设置消息== 0。 当管理员注销时设置消息== 1.因此,当管理员再次登录时,它将显示消息。

答案 3 :(得分:0)

我的解决方案是在登录页面上添加脚本标签

<script type="text/javascript">
    localStorage.clear();
</script>

答案 4 :(得分:0)

注销 script tag

上添加onclick
<a href="{{ route('logout') }}" 
    onclick="event.preventDefault();
             document.getElementById('logout-form').submit();
             localStorage.clear();"
    class="dropdown-item notify-item"> 
  <i class="pe-7s-power"></i>
  <span>Logout</span>
</a>
相关问题