To log out users from all devices when the password is changed in PHP, here we will create a good and easy approach to create the script.
Here are some major steps which we will follow,
- At the time of login, we will store a random number associated with the user id in the DB and session.
- Store
IS_LOGIN
key as true at the time of user login. - At the time of changing the password, if you select logout from all devices it will delete a random values from DB.
- Create a JS file where it will check the status of the user every 5 seconds.
- Create a check status file where a query will check whether the random value is available or not.
- At last, when we will log out it will destroy all session stored data and delete the random values from DB.
Note: You can download complete file at the end of the article
Now we will start with some core file’s source code which we use to process the above steps,
DB Connection
1 2 3 4 |
<?php session_start(); $con = mysqli_connect('localhost','root','','logout_session'); ?> |
Login Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php require('db.php'); $msg = ''; if(isset($_POST['submit'])){ $email = mysqli_real_escape_string($con,$_POST['email']); $password = mysqli_real_escape_string($con,$_POST['password']); $res = mysqli_query($con,"select id from users where email='$email' and password='$password'"); if(mysqli_num_rows($res)){ $row = mysqli_fetch_assoc($res); $id = $row['id']; $rand_no = rand(1111111,9999999); mysqli_query($con,"insert into users_login(user_id,rand_no) values($id,$rand_no)"); $_SESSION['IS_LOGIN'] = true; $_SESSION['UID'] = $id; $_SESSION['UID_RAND'] = $rand_no; header('location:dashboard.php'); die(); }else{ $msg="Please enter valid login details"; } } ?> |
Here we store the random integer and user ID in the database. And will store the same data on session to manage further operations.
Dashboard
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php require('db.php'); if(!isset($_SESSION['IS_LOGIN'])){ header('location:index.php'); die(); } ?> <a href="logout.php">Logout</a> <a href="change_password.php">Change Password</a> <script src="js/jquery-3.5.1.min.js"></script> <script src="js/custom.js"></script> |
After login, you will redirect to the dashboard page. Here we have included our custom JS, log out, and change password page.
Custom JS with Check Status PHP file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
setInterval(function(){ checkUserStatus(); },5000); function checkUserStatus(){ jQuery.ajax({ url:'check_status.php', success:function(result){ var result = result.trim(); if(result != 1){ window.location.href = 'logout.php'; } } }); } |
Here this file will check the user status every 5 seconds.
1 2 3 4 |
<?php require('db.php'); echo mysqli_num_rows(mysqli_query($con,"select * from users_login where user_id='".$_SESSION['UID']."' and rand_no='".$_SESSION['UID_RAND']."'")); ?> |
On Change Password
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php require('db.php'); $msg=''; if(isset($_POST['submit'])){ $password = mysqli_real_escape_string($con,$_POST['password']); $update_sql = ''; if(isset($_POST['logout'])){ mysqli_query($con,"delete from users_login where user_id='".$_SESSION['UID']."' and rand_no!='".$_SESSION['UID_RAND']."'"); } mysqli_query($con,"update users set password='$password' $update_sql where id='".$_SESSION['IS_LOGIN']."'"); $msg = "Password updated"; } ?> |
At the time of changing the password, if the user checks on log out from all devices then it will delete that random value for that particular user.
Download complete file
A complete working script of implementing logout from all devices in PHP.
[wpdm_package id=’4616′]
Hope you all will understand the complete concept of Logout From All Devices on Password Change using PHP.
To know more about PHP, you can check PHP’s official site.
Please let me know if you will face any issues with implementation.
Happy Coding..!
[…] PHP: Logout From All Devices on Password Change March 3, 2022 […]
[…] PHP: Logout From All Devices on Password Change March 3, 2022 […]