• 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 Do I Check PHP Session Realtime Using Jquery And Mousedown Event

How Do I Check PHP Session Realtime Using Jquery And Mousedown Event

By Sigit Prasetya Nugroho ∙ August 10, 2016 ∙ Javascript, PHP ∙ Leave a Comment

Share : TwitterFacebookTelegramWhatsapp

Why do we need to check php session on a web application? $_SESSION is a temporary variable that’s placed on a server that can be accessed by PHP although we open a new page in the browser. Session variables store information into super global variable $_SESSION. The constraints are when a session timeout often causes application error because it can’t access the global variable. The use of session is closely related to authentication.

Many methods are used to check PHP session like when we refresh the page, use the timer in javascript , and when the mouse is pressed on a particular page. In this tutorial, I shared how to check PHP session in realtime using jquery and mousedown event. Mousedown event is executed when the mouse pointer over the element, and the mouse button is pressed. Each HTML element can receive this event. I use mousedown event that the script does not overload the browser. Okay we immediately start the following tutorial

Table of Contents

  • 1 My tutorial check PHP session realtime using Jquery and Mousedown event
    • 1.1 Thus article about how to check PHP session using jquery and mousedown event, hope useful

My tutorial check PHP session realtime using Jquery and Mousedown event

1. Create a javascript file named session_checker.js and copy the following code.

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
27
28
29
$(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.session == false)
{
console.log(data.session);
console.log(data.redirect_url);
}else{
console.log(data.session);
}
},
error: function(jqXHR, textStatus, errorThrown)
{
$.notify({
message: 'Error : '+jqXHR.status+' - '+textStatus+' '+errorThrown
},{
type: 'danger',
delay: 10000,
});
}
});
}
})

2. Create a PHP file named session_checker.php and copy the following code.

1
<?php session_start(); if (!isset($_SESSION['my_id'])) { $data['session'] = false; $data['redirect_url'] = 'your redirect url here'; }else{ $data['session'] = true; } echo json_encode($data); ?>

3. Create a PHP file named index.php and copy the following code.

1
2
3
4
5
6
7
8
9
10
11
<?php session_start(); ?>
<html>
<head>
<title>Session Checker</title>
</head>
<body>
<a href="set_session.php" target="_blank">Set Session</a> || <a href="release_session.php" target="_blank">Release Session</a>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script src="session_checker.js"></script>
</body>
</html>

4. Create a PHP file with the name set_session.php to do a set a new session variable and copy the following code

1
<?php session_start(); $_SESSION['my_id'] = 'myuniqid'; echo 'Session already set!!'; ?>

5. Create a PHP file with the name release_session.php to do an unset session and copy the following code

1
<?php session_start(); unset($_SESSION['my_id']); echo 'Session already release!!'; ?>

Save entire file above and put in a single folder. Please open your browser and press the F12 button to see the results. Press the “set session” link to create $_SESSION[‘id’] and press the “release session” link to remove session. through the browser console will look like the following image results

How Do I Check PHP Session Realtime Using Jquery And Mousedown Event

To perform the redirect URL if a session has timed out or doesn’t exist, then change the URL at session_checker.php with your login page and change the session_checker.js with the following code

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.session == false)
{
window.location.replace(data.redirect_url);
}
},
error: function(jqXHR, textStatus, errorThrown)
{
$.notify({
message: 'Error : '+jqXHR.status+' - '+textStatus+' '+errorThrown
},{
type: 'danger',
delay: 10000,
});
}
});
}
})

Please try on your application. To get  this example, please download the full source at the following link

[sociallocker id=”58″] http://wp.me/a65Dpx-yR [/sociallocker]

Do you have other ways that are more effective and efficient? I am so glad to hear your opinion, please tell me in the comments form below for all your questions and suggestions about how to check PHP session.

Thus article about how to check PHP session using jquery and mousedown event, hope useful

Another Javascript Related Post :

  • React-Table Not Updating or Refreshing Data, The Solution ?
  • How To Custom React Datepicker In Bootstrap
  • 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

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

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 (6) Reactjs (9) 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

©2022 Seegatesite.com