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

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

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.

$(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.

<?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.

<?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

<?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

<?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

$(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

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

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

%d bloggers like this: