• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
seegatesite header

Seegatesite.com

Seegatesite.com - Programming Tutorial , Sharing , How and Learn Together

  • TOOLS
    • Bootstrap Navbar Online Generator
    • Customize Sidebar Menu Bootstrap 3
    • Bootstrap Demo
  • ADVERTISE
  • CATEGORIES
    • Android
    • Blogging Tips
    • Database
    • CSS
    • Info Gadget
    • Javascript
    • Linux
    • PHP
    • Various
    • WordPress
  • Q&A
  • PHP
  • JAVASCRIPT
  • JQUERY
  • ANGULAR
  • WORDPRESS
  • SEO
  • REACT
🏠 » Javascript » How To Force User To Log Out When The User Login On Other Sessions In PHP

How To Force User To Log Out When The User Login On Other Sessions In PHP

By Sigit Prasetya Nugroho ∙ August 19, 2016 ∙ Javascript, PHP ∙ 1 Comment

Share : TwitterFacebookTelegramWhatsapp

Seegatesite, how to force user to log out I apply some project website that I created. This article is a continuation of the articles that I have written before How Do I Check the PHP Session Realtime Using Jquery And Mousedown Event. In addition, this technique can also be used to disable / do banned user via admin panel.

The essence of this method is to add a unique value to the database, then the script will do the checking every time the user clicks on the HTML element. The system will always perform to update unique values into the database when a user login in the system. And each time the user clicks on the HTML element, system checks to see if a unique value in the database same with the value stored in the global variable ($_SESSION). If different, then the entire session will be destroyed. The following is the complete tutorial.

How To Force User To Log Out When The User Login On Other Sessions In PHP

My simple way to force user to log out when the user login on other sessions

1. Add new field on your user table with the name “unique_auth” (data type : varchar(20))

How To Force User to Log Out In Php

2. To create a unique value on the field unique_auth with the following code

1
$unique_auth = base64_encode(uniqid().strtotime(date('Y-m-d H:i:s')));

3. Save the above unique value into the field unique_auth and global variables $_SESSION[‘unique_auth’] When user do log in.

1
2
3
$uniqlogin = base64_encode(uniqid().strtotime(date('Y-m-d H:i:s')));
$sv->updateUniqLogin($iduser,$uniqlogin); // save unique_auth to DB
$_SESSION['unique_auth'] = $uniqlogin; // Send unique_auth to $_SESSION Variable

4. I’m using AJAX Jquery and MouseDown event to check session and value unique_auth whether the same or different from those in the database. If the both value is different, the system will force log out an active  user. The following code is my example script, Please customize your application

session_checker.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$(document).on("mousedown",function(event){
if(event.which == 1){
$.ajax(
{
url : "session_checker.php",
type: "POST",
success: function(data, textStatus, jqXHR)
{
var data = jQuery.parseJSON(data);
if(data.result == -1)
{
     window.location.replace(data.url);
}
},
error: function(jqXHR, textStatus, errorThrown)
{
$.notify({
message: 'Error : '+jqXHR.status+' - '+textStatus+' '+errorThrown
},{
type: 'danger',
delay: 10000,
});
}
});
}
})

session_checker.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php session_start(); include "config.php"; if (!isset($_SESSION['myapp_nama_user']) or !isset($_SESSION['myapp_id']) or !isset($_SESSION['myapp_level']) or !isset($_SESSION['myapp_uniqid']) or !isset($_SESSION['myapp_h_menu']) ) { $data['result'] = '-1'; $data['url'] = $sitename.'aplikasi/utama/login.php?error=session_die'; }else{ include "../aplikasi/model/dbconn.php"; include "../aplikasi/model/myapp.php"; $id_user = $_SESSION['myapp_id']; $myapp = new myapp(); $ceklogin = $myapp->CheckLoginAutentikasi($id_user); // check unique_auth value on DB
if($ceklogin[1]['unique_auth'] == $_SESSION['unique_auth']) // check if unique_auth same with $_SESSION['auth']
{
$data['result'] = '1';
$data['url'] = 'access granted';
}else{
// if value from field unique_auth is different with $_SESSION['unique_auth']
$data['result'] = '-1';
                session_destroy();
$data['url'] = $sitename.'aplikasi/utama/login.php?error=99';
}
 
}
echo json_encode($data);
 
?>

Please try on your browser and see the results.

Thus my simple way to force log out active users. If you have a better solution, please give me feedback on the comment form below.

Another Javascript Related Post :

  • Tutorial Create Simple POS Using ReactJS And Laravel Lumen Part 1
  • Adept Using Datatables Plugin In 10 Minutes For Beginners (Tutorial)
  • The Using Of Reactstrap And React-Table, Suitable For Beginners
  • Tutorial Create Simple Block UI Using React JS
  • How To Implement Login Page And Protected Route ReactJS
  • Complete Tutorial React Router Dom For Beginners (Easy-To-Understand)

Avatar for Sigit Prasetya Nugroho

About Sigit Prasetya Nugroho

This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Reader Interactions

Comments

  1. Avatar for shinichishinichi says

    February 3, 2017 at 10:50 am

    gak bisa di download eror saat di extrak master

    Reply
    • Avatar for Sigit Prasetya NugrohoSigit Prasetya Nugroho says

      February 5, 2017 at 9:30 am

      maksutnya yang mana ya???

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Welcome to my Home,

Avatar for Sigit Prasetya NugrohoThis site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.



Popular Articles

Checked checkbox AdminLTE Bootstrap in Jquery

November 4, 2014 By Sigit Prasetya Nugroho 7 Comments

Simple create date format validation with jqueryUI

December 21, 2014 By Sigit Prasetya Nugroho Leave a Comment

Create Simple Progress Bar for Fake Online Generator with Jquery

January 10, 2015 By Sigit Prasetya Nugroho Leave a Comment

22+ Coolest Free Jquery Plugin For Premium Theme

October 3, 2015 By Sigit Prasetya Nugroho Leave a Comment

Easy Build Your Anti Copy Paste Plugin

October 6, 2015 By Sigit Prasetya Nugroho Leave a Comment

Popular Tags

adminlte (15) adsense (13) adsense tips (4) affiliate amazon (13) amazon (12) Android (8) angular (16) angular 4 (12) angular 5 (4) asin grabber (3) Bootstrap (27) codeigniter (5) create wordpress theme (5) crud (8) css (6) free wordpress theme (7) google adsense (4) imacros (4) increase traffic (6) jquery (34) laravel (10) laravel 5 (5) learn android (5) lumen api (4) modal dialog (5) mysql (6) nodeJs (4) optimize seo (4) pdo (6) php (30) plugin (53) pos (8) Publisher Tips (5) react (4) Reactjs (7) SEO (37) theme (17) tutorial angular (5) tutorial angular 4 (6) tutorial javascript (10) tutorial javascript beginners (4) twitter (3) wordpress (18) wordpress plugin (13) XMLRPC (5)




  • About
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

©2021 Seegatesite.com