Page 1 of 1
Running script when leaving page
Posted: Tue Jul 12, 2011 4:39 am
by simonmlewis
Code: Select all
echo '<a href="javascript:window.close();">Close Window</a>';
Hi
I want to run a PHP script when a person leaves a window - not closing, just leaving it.
It's for a chat service we are looking at building, and need to close sessions and cookies, if they decide to either close the window, or click on a link on the page and go elsewhere.
The link above closes the window, that's fine - but need to ask the browser "are you being closed" and "are you being taken away from this page".
Is there such a script? I've been on web sites where a popup appears if you go to another site. So it must be possible.
Thanks.
Re: Running script when leaving page
Posted: Tue Jul 12, 2011 12:01 pm
by AbraCadaver
Not tested:
Code: Select all
window.onunload = my_function;
function my_function() {
/* window.close(); or other stuff */
}
Might have to put the window.onunload() in the body tag, not sure. Or possibly:
Re: Running script when leaving page
Posted: Tue Jul 12, 2011 12:17 pm
by simonmlewis
Sorry to sound a bit dim here, but I need to run a script when that Function is performed, but I don't know how to put a PHP function into it?
Code: Select all
<script type="text/javascript">
function windowclose() {
// DOES PHP FUNCTION GO IN HERE?
window.onunload = window.close();
}
</script>
</head>
<?php
echo "<body //WHAT DO I PUT HERE?">";
Re: Running script when leaving page
Posted: Tue Jul 12, 2011 2:16 pm
by simonmlewis
And the answer seems to be (after a bit more digging)
Code: Select all
<script>
window.onbeforeunload = function () {
document.createElement('img').src = 'end.php';
}
</script>
When the window is closed by the 'x' in the browser (not a hand-made button), end.php is run and it performs all scripts needed.
So simple!
Re: Running script when leaving page
Posted: Tue Jul 12, 2011 2:33 pm
by McInfo
This is one of the most fundamental ideas in PHP programming. It is important for you to understand.
For PHP to react to something the user does, an HTTP request must be made to the server.
JavaScript can trigger an HTTP request in a number of ways: [1] using the XMLHttpRequest object (AJAX), [2] inserting a tag which requires an external resource (<img>, <link>, <script>, <iframe>, etc.), [3] changing the src or href of such a tag, [4] (I could have overlooked something). Anyway, the point is that there must be a separate request.
I see you have discovered this already, but I will post my example because I had it prepared already and I think the comments will be helpful.
This example logs the current server time into a file (on the server, of course) when you close the browser window.
page.html - The first request is for this HTML document. When the page loads, your browser makes a second request, for close.js.
Code: Select all
<!DOCTYPE html>
<html>
<head>
<title>Close</title>
<script type="text/javascript" src="close.js"></script>
</head>
<body>
<p>Close me.</p>
</body>
</html>
close.js - When you close the page, JavaScript inserts an image tag. When the image is inserted, your browser makes its third request, for ping.php.
Code: Select all
window.onunload = function () {
var img = document.createElement('img');
img.src = 'ping.php';
document.body.appendChild(img);
}
ping.php
Code: Select all
<?php
file_put_contents('ping.txt', date('Y-m-d H:i:s'));
Re: Running script when leaving page
Posted: Tue Jul 12, 2011 2:50 pm
by AbraCadaver
And here was my stab at the xmlhttp request (not tested):
Code: Select all
window.onunload = function ()
{
xhttp = new XMLHttpRequest();
xhttp.open("GET", "http://example.com/end.php");
xhttp.send();
}
Re: Running script when leaving page
Posted: Wed Jul 13, 2011 4:22 am
by simonmlewis
None of these, or mine are going to work. I thought mine would, and does. But it has one major flaw.
It does it even when the page changes. I want it to work only when the browser itself is closed.
When someone his submit to send more info, it posts it to itself, but in effect, it is closed and reopening that page, which causes the script to run and close the session.
Re: Running script when leaving page
Posted: Fri Sep 16, 2011 9:21 am
by simonmlewis
Can PHP identify if the page has been 'changed' or if the Browser itself has been closed?
We've had issues where a user of the site has gone from one page to another within the site, and it logs them out as thes script has executed.
I want to have it run ONLY when the browser is CLOSED.
Is this possible - while leaving it running if the page is changed?
Re: Running script when leaving page
Posted: Fri Sep 16, 2011 12:55 pm
by califdon
I don't think so. As was emphasized above, once a web server has sent a page to a browser, the server can only respond to a subsequent request. So PHP cannot "identify if the page has been 'changed' or if the browser itself has been closed". That would require the browser to send a new request, which might be initiated by Javascript, as discussed earlier. So the question to ask is whether Javascript (not PHP) can detect when the browser is closed, not redirected to another page. As far as I know, it cannot.
Re: Running script when leaving page
Posted: Fri Sep 16, 2011 1:34 pm
by simonmlewis
Well yes, but I am in a Javascript forum so thought someone expert in JS might be able to respond with a solution.
Re: Running script when leaving page
Posted: Fri Sep 16, 2011 2:14 pm
by califdon
simonmlewis wrote:Well yes, but I am in a Javascript forum so thought someone expert in JS might be able to respond with a solution.
That's fine, this is the appropriate forum, but your questions have been:
I don't know how to put a PHP function into it
Can PHP identify if the page has been 'changed' or if the Browser itself has been closed?
I am just trying to insure that you understand that PHP has no way of knowing WHAT your browser is doing, other than receiving a request, so your entire issue is with Javascript, contrary to the questions that you asked. If you have that concept firmly in mind, you can disregard my comments.
Re: Running script when leaving page
Posted: Fri Sep 16, 2011 2:35 pm
by simonmlewis
Yes. I did just wonder if PHP is a method of doing something clientside.
The script I used above did work, but now doesn't. But it still closed the cookie session (ran the script in a file it ran on "unload"), even when just leaving that particular page and stayed in the site.