How to clear a PHP session using jQuery / javascript?

Just ask if it is possible to clear a PHP session using jquery or javascript? Because what I want to do is to clear the PHP session using the dialog box. If the user clicks the OK button, he will process the canceled function. There is a verification process on my page, and after the user clicks the OK button, he will offer a dialog box, and if the user clicks the OK button, he will cancel the PHP session and be redirected to another page.

Here is my simple code:

<i class="fa fa-check"></i> <span id="save_registry" class="cursor: pointer">OK</span> <div id="save_alert" style="display: none"> <?php unset($this->session->data['cart']); </div> .... $('#save_registry').click(function(){ $('#save_alert').dialog({ modal: true, buttons: { Ok: function() { $( this ).dialog( "close" ); location = 'index.php?route=common/home'; } } }); }); 
+7
jquery php session
source share
3 answers

Simply put: you cannot, directly.

PHP is the server language, and javascript is the client side , which means that there is no other connection between them except the received document and interacts with you. Sessions are stored and managed by the server .

However, you can, as War10ck suggests, access the server side of the script that clears the session. Something like that:

PHP:

 <?php /* * session_start(); * Starting a new session before clearing it * assures you all $_SESSION vars are cleared * correctly, but it not strictly necessary. */ session_destroy(); session_unset(); header('Location: continue.php'); /* Or whatever document you want to show afterwards */ ?> 

HTML / JavaScript:

 <script type="text/javascript"> function logout() { document.location = 'logout.php'; } LogoutButton.addEventListener('click', logout, false); </script> <button id="LogoutButton">Log out</button> 

Or even make an asynchronous call:

 function logout() { var xhr = new XMLHttpRequest(); xhr.onload = function() { document.location = 'continue.php'; } xhr.open('GET', 'logout.php', true); xhr.send(); } 
+10
source share

There is a way that you can do this directly from javascript ... first you need to declare a function that clears the cookie with its key

 function removeCookie(cookieName) { cookieValue = ""; cookieLifetime = -1; var date = new Date(); date.setTime(date.getTime()+(cookieLifetime*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); document.cookie = cookieName+"="+JSON.stringify(cookieValue)+expires+"; path=/"; } 

Now you need to delete the cookie with the key " PHPSESSID " like this

 removeCookie("PHPSESSID"); 

Now that you have var_dump($_SESSION) , you will find an empty array

+4
source share

You cannot clear a php session using Javascript directly.

PHP is the server language, and Javascript is the client language.

However, you can send an ajax request to the specified page that handles the destruction.

Like this:

 // Client-side $.post("logout.php", {"can_logout" : true}, function(data){ if(data.can_logout) // redirect the user somewhere }, "json"); <?php // Server-side $can_logout = $_POST["can_logout"]; if($can_logout) session_destroy(); echo json_encode(array("can_logout" => true)); ?> 

Or simply point the user to some page that handles the destruction of the session; those. logout.php

+3
source share

All Articles