Jump to content

Catching the 'Back' Button


cdromes

Recommended Posts

Howdy

 

I'm wondering if there's a way to catch when the user clicks the 'Back' button from a specific page.  When this happens, I need to run a system call that deletes everything in a session-specific directory.  The systems call would look something like this:

 

system('rm -r /home/websites/genework/html/DATA/'.$Sess_ID.'/*');

 

So, the only thing left is the snag involving the catch of the 'Back' button.  Javascript has a way to catch this action (window.onbeforeunload), but the tap-dancing that would be required to get PHP to activate the system call is extremely ugly and I'm not even sure it'd work.

 

Any help is greatly appreciated.

 

Jason

Link to comment
Share on other sites

Specifically, when the user hits the back from a specific page, I need to execute a system command that deletes the contents of a directory that I create for each user via a session variable.  The tap-dancing involves the fact that I've been trying to do this in Javascript because PHP doesn't seem to have any way of catching the 'Back' button.

 

The problem with the Javascript method is that it simply displays a dialog box informing that you're about to leave the page.  Click OK and you move back; click cancel and you don't.  The method doesn't allow for the use of conditionals to check which choice was made, so there's no way to execute anything in the event 'OK' is clicked.

 

I've never worked with AJAX....might that be the answer?

 

Jason

 

 

Link to comment
Share on other sites

Specifically, when the user hits the back from a specific page, I need to execute a system command that deletes the contents of a directory that I create for each user via a session variable.

 

What is the underlying problem that gives rise to this solution?  There may be a better solution, such as passing along a token unique to each page view, and not allowing re-use of old tokens.

Link to comment
Share on other sites

Basically, for ajax, I'd do something like this:

function callAjax() {
scriptitem = document.createElement('script');
scriptitem.type = 'text/javascript';
scriptitem.src = 'includes/ajax.php';
scriptitem.id = 'ajax';
document.body.appendChild(scriptitem);
}
window.onbeforeonload = callAjax();

 

When you hit the back button, JS creates a script item including the includes/ajax.php file, which will delete the session, then output some relevant JS if necessary.

 

Ajax.php could be:

<?php
session_destroy();
echo 'alert("Destroyed the session");';
?>

 

I hope this makes sense, and moreso, it works correctly.  I'm doing this off the top of my head, so correct me where I'm wrong.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.