删除用户但仍登录后销毁会话 [英] destroy session after user is deleted but still logged in

查看:69
本文介绍了删除用户但仍登录后销毁会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 login.php 中,我检查一切是否正常,如果我创建会话并重定向用户

In the login.php i check if everething is ok and if its i creat the session and redirect the user

 $success = $dbh->prepare("SELECT UserId FROM users WHERE username =:username 
AND password=:password");
    $success->bindParam(':username', $username);
    $success->bindParam(':password', $password);
    $success->execute();
    $rowSuccess = $success->fetch();
    $user_id = $rowSuccess['userid'];

    $_SESSION['user_id'] = $user_id;
    $_SESSION['loggedIn'] = 1; 

如果他仍然登录,那么当我删除他时如何销毁用户会话

So how to destroy the user session when i delete him if he is still logged in

推荐答案

只要你调用 session_destroy() 你的 $_SESSION['user_id']$_SESSION['loggedIn'] 将不复存在.所以当用户刷新页面时,有一个if条件来检查userId并调用

As soon as you call on session_destroy() your $_SESSION['user_id'] and $_SESSION['loggedIn'] will cease to exist. So when the user refreshes the page, have an if condition to check for the userId and call

 if(!isset($_SESSION['userId'])) { //if the session variable for this doesn't userId exist

   print "Sorry, no recognized account";   
 } 

这听起来更像是 AJAX 解决方案,因为您想在用户被删除时立即执行某些操作.在您的 JavaScript 中设置一个 ajax 请求,该请求调用执行此删除操作的 PHP 脚本.然后,当操作完成时,提醒用户并使用 window.location 将他/她带到不同的页面.总结

This sounds more like an AJAX solution since you wanna do some action immediately when the user is deleted. In your JavaScript set up an ajax request that calls the PHP script performing this delete action. Then, when the action is completed, alert the user and take him/her to a different page using window.location. To summarize

  $.ajax({
 url:"deleteUser.php",
 cache:false,
 success:function(data){
       alert("Sorry you have been deleted. Re-routing to home page");
               window.location="homepage.php";
       }       
});

这篇关于删除用户但仍登录后销毁会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆