cdromes Posted September 10, 2007 Share Posted September 10, 2007 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 10, 2007 Share Posted September 10, 2007 Seems like you're making the problem hard than it is. Quote Link to comment Share on other sites More sharing options...
btherl Posted September 11, 2007 Share Posted September 11, 2007 What is the underlying problem you are trying to solve? As for the tap-dancing, it sounds like you want to make an ajax style call back to php. Quote Link to comment Share on other sites More sharing options...
cdromes Posted September 11, 2007 Author Share Posted September 11, 2007 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 Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 11, 2007 Share Posted September 11, 2007 edit the javascript so that the alter is replaced by the ajax call to the php script that needs to do the work for you. you will need to prevent the browser actually going back until teh php script has reported a successful completion of the tasks it runs. Quote Link to comment Share on other sites More sharing options...
magnetica Posted September 11, 2007 Share Posted September 11, 2007 Look into AJAX and use javascript to check the back button then AJAX can send a request to the server and run a PHP script that you have. Quote Link to comment Share on other sites More sharing options...
btherl Posted September 12, 2007 Share Posted September 12, 2007 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. Quote Link to comment Share on other sites More sharing options...
sneamia Posted September 12, 2007 Share Posted September 12, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.