Discussion:
pulling script in interval of second.
hadi
2014-06-30 23:56:46 UTC
Permalink
I have javascript to pull my php script every interval of second.

When javascript stop pulling my php script, block in if statement should
execute.

Can someone provide me with the php script please ?

Thank you.
Aziz Saleh
2014-07-01 00:04:11 UTC
Permalink
Post by hadi
I have javascript to pull my php script every interval of second.
When javascript stop pulling my php script, block in if statement should
execute.
Can someone provide me with the php script please ?
Thank you.
You will have to make the script 2 parts. One to reload and the other to
execute the else (exiting). You need to use ajax to call the php script
every second (for the first part) and use the page's onunload method to
execute the second part (user exited):

http://www.w3schools.com/jsref/event_onunload.asp
hadi
2014-07-01 00:21:31 UTC
Permalink
Post by Aziz Saleh
You will have to make the script 2 parts. One to reload and the other to
execute the else (exiting). You need to use ajax to call the php script
every
second (for the first part) and use the page's onunload method to execute
http://www.w3schools.com/jsref/event_onunload.asp
I already tried your method with onunload, the problem with onunload when I
change the page the script getting execute.

I already have javascript to pull php script, just I need php script doing the
flowing; when javascript stop pulling php script. Block in if statement
should run.

Can you give me php script ? please.
Post by Aziz Saleh
Post by hadi
I have javascript to pull my php script every interval of second.
When javascript stop pulling my php script, block in if statement
should execute.
Can someone provide me with the php script please ?
Thank you.
You will have to make the script 2 parts. One to reload and the other to
execute the else (exiting). You need to use ajax to call the php script
every
second (for the first part) and use the page's onunload method to execute
http://www.w3schools.com/jsref/event_onunload.asp
louis
2014-07-01 01:45:13 UTC
Permalink
Hi hadi:

As http server is stateless, php can not know if a client has stop
requesting.
you should do your trick on the client side.

But if you insist doing this on the server, you may need to write a
daemon process or crontab job , here's the step

1. Everytime the client make a request, your php code write a timestamp
in a temp file.
2. Your daemon process should read this file and compare to the
timestamp by NOW, if it is larger than you expect ,then do the if
statement thing in the daemon process.
Post by hadi
Post by Aziz Saleh
You will have to make the script 2 parts. One to reload and the other to
execute the else (exiting). You need to use ajax to call the php script
every
second (for the first part) and use the page's onunload method to execute
http://www.w3schools.com/jsref/event_onunload.asp
I already tried your method with onunload, the problem with onunload when I
change the page the script getting execute.
I already have javascript to pull php script, just I need php script doing the
flowing; when javascript stop pulling php script. Block in if statement
should run.
Can you give me php script ? please.
Post by Aziz Saleh
Post by hadi
I have javascript to pull my php script every interval of second.
When javascript stop pulling my php script, block in if statement
should execute.
Can someone provide me with the php script please ?
Thank you.
You will have to make the script 2 parts. One to reload and the other to
execute the else (exiting). You need to use ajax to call the php script
every
second (for the first part) and use the page's onunload method to execute
http://www.w3schools.com/jsref/event_onunload.asp
hadi
2014-07-01 03:07:51 UTC
Permalink
As http server is stateless, php can not know if a client has stop requesting.
you should do your trick on the client side.
But if you insist doing this on the server, you may need to write a daemon
process or crontab job , here's the step
1. Everytime the client make a request, your php code write a timestamp in a
temp file.
2. Your daemon process should read this file and compare to the timestamp
by NOW, if it is larger than you expect ,then do the if statement thing in the
daemon process.
Look what I have accomplished in my script, the problem in my script when " session_destroy();" and " unset($_SESSION['timeout']);" happen it supposed to kill "timeout" then "while loop if(time() > $st) " will stop. But theses not happing im wondering why. Can you look in my code and see?

<?php
session_start();
?>


<?php

for( $x=0; $x< 50; $x++ )

{

$st = $_SESSION['timeout'] = time() + 10;

session_destroy();

unset($_SESSION['timeout']);
}

$loop= true;

while($loop)

{

if(time() > $st)

{

file_put_contents('/tmp/phptest1234.txt', 'test');
$loop=false;
}
}

?>
As http server is stateless, php can not know if a client has stop requesting.
you should do your trick on the client side.
But if you insist doing this on the server, you may need to write a daemon
process or crontab job , here's the step
1. Everytime the client make a request, your php code write a timestamp in a
temp file.
2. Your daemon process should read this file and compare to the timestamp
by NOW, if it is larger than you expect ,then do the if statement thing in the
daemon process.
Post by hadi
Post by Aziz Saleh
You will have to make the script 2 parts. One to reload and the other
to execute the else (exiting). You need to use ajax to call the php
script every second (for the first part) and use the page's onunload
http://www.w3schools.com/jsref/event_onunload.asp
I already tried your method with onunload, the problem with onunload
when I change the page the script getting execute.
I already have javascript to pull php script, just I need php script
doing the flowing; when javascript stop pulling php script. Block in
if statement should run.
Can you give me php script ? please.
Post by Aziz Saleh
Post by hadi
I have javascript to pull my php script every interval of second.
When javascript stop pulling my php script, block in if statement
should execute.
Can someone provide me with the php script please ?
Thank you.
You will have to make the script 2 parts. One to reload and the other
to execute the else (exiting). You need to use ajax to call the php
script every second (for the first part) and use the page's onunload
http://www.w3schools.com/jsref/event_onunload.asp
louis
2014-07-01 03:50:32 UTC
Permalink
your code did not make any sense, PHP code executing from top to bottom
line by line. you can't expect it to enter the loop and do the
unset($_SESSION) staff at the same time.
let me tell you what your code do exactly:

1. at first you set and unset a session key for 50 times, the $st 's
value is the time() of the 50th loop plus 10
2. you enter a loop and check if time() > $st, as $st is time() + 10 ,
it is always larger than time(), so it enter the if statement and write
the temp file , then the process exist
Post by hadi
As http server is stateless, php can not know if a client has stop requesting.
you should do your trick on the client side.
But if you insist doing this on the server, you may need to write a daemon
process or crontab job , here's the step
1. Everytime the client make a request, your php code write a timestamp in a
temp file.
2. Your daemon process should read this file and compare to the timestamp
by NOW, if it is larger than you expect ,then do the if statement thing in the
daemon process.
Look what I have accomplished in my script, the problem in my script when " session_destroy();" and " unset($_SESSION['timeout']);" happen it supposed to kill "timeout" then "while loop if(time() > $st) " will stop. But theses not happing im wondering why. Can you look in my code and see?
<?php
session_start();
?>
<?php
for( $x=0; $x< 50; $x++ )
{
$st = $_SESSION['timeout'] = time() + 10;
session_destroy();
unset($_SESSION['timeout']);
}
$loop= true;
while($loop)
{
if(time() > $st)
{
file_put_contents('/tmp/phptest1234.txt', 'test');
$loop=false;
}
}
?>
As http server is stateless, php can not know if a client has stop requesting.
you should do your trick on the client side.
But if you insist doing this on the server, you may need to write a daemon
process or crontab job , here's the step
1. Everytime the client make a request, your php code write a timestamp in a
temp file.
2. Your daemon process should read this file and compare to the timestamp
by NOW, if it is larger than you expect ,then do the if statement thing in the
daemon process.
Post by hadi
Post by Aziz Saleh
You will have to make the script 2 parts. One to reload and the other
to execute the else (exiting). You need to use ajax to call the php
script every second (for the first part) and use the page's onunload
http://www.w3schools.com/jsref/event_onunload.asp
I already tried your method with onunload, the problem with onunload
when I change the page the script getting execute.
I already have javascript to pull php script, just I need php script
doing the flowing; when javascript stop pulling php script. Block in
if statement should run.
Can you give me php script ? please.
Post by Aziz Saleh
Post by hadi
I have javascript to pull my php script every interval of second.
When javascript stop pulling my php script, block in if statement
should execute.
Can someone provide me with the php script please ?
Thank you.
You will have to make the script 2 parts. One to reload and the other
to execute the else (exiting). You need to use ajax to call the php
script every second (for the first part) and use the page's onunload
http://www.w3schools.com/jsref/event_onunload.asp
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
hadi
2014-07-01 04:20:07 UTC
Permalink
Post by louis
your code did not make any sense, PHP code executing from top to bottom
line by line. you can't expect it to enter the loop and do the
unset($_SESSION) staff at the same time.
1. at first you set and unset a session key for 50 times, the $st 's value is the
time() of the 50th loop plus 10 2. you enter a loop and check if time() > $st, as
$st is time() + 10 , it is always larger than time(), so it enter the if statement
and write the temp file , then the process exist
You sow my code and you got my point can you help to fix it.
So if $st is large then time why it should enter if statement. It supposed to stop. Because I already mention if time>$st, so if time is greater than $st not time < $st
Post by louis
your code did not make any sense, PHP code executing from top to bottom
line by line. you can't expect it to enter the loop and do the
unset($_SESSION) staff at the same time.
1. at first you set and unset a session key for 50 times, the $st 's value is the
time() of the 50th loop plus 10 2. you enter a loop and check if time() > $st, as
$st is time() + 10 , it is always larger than time(), so it enter the if statement
and write the temp file , then the process exist
Post by hadi
Post by louis
As http server is stateless, php can not know if a client has stop
requesting.
Post by hadi
Post by louis
you should do your trick on the client side.
But if you insist doing this on the server, you may need to write a
daemon process or crontab job , here's the step
1. Everytime the client make a request, your php code write a
timestamp in a temp file.
2. Your daemon process should read this file and compare to the
timestamp by NOW, if it is larger than you expect ,then do the if
statement thing in the daemon process.
Look what I have accomplished in my script, the problem in my script when
" session_destroy();" and " unset($_SESSION['timeout']);" happen it
supposed to kill "timeout" then "while loop if(time() > $st) " will stop. But
theses not happing im wondering why. Can you look in my code and see?
Post by hadi
<?php
session_start();
?>
<?php
for( $x=0; $x< 50; $x++ )
{
$st = $_SESSION['timeout'] = time() + 10;
session_destroy();
unset($_SESSION['timeout']);
}
$loop= true;
while($loop)
{
if(time() > $st)
{
file_put_contents('/tmp/phptest1234.txt', 'test'); $loop=false; } }
?>
Post by louis
As http server is stateless, php can not know if a client has stop
requesting.
Post by hadi
Post by louis
you should do your trick on the client side.
But if you insist doing this on the server, you may need to write a
daemon process or crontab job , here's the step
1. Everytime the client make a request, your php code write a
timestamp in a temp file.
2. Your daemon process should read this file and compare to the
timestamp by NOW, if it is larger than you expect ,then do the if
statement thing in the daemon process.
Post by hadi
Post by Aziz Saleh
You will have to make the script 2 parts. One to reload and the
other to execute the else (exiting). You need to use ajax to call
the php script every second (for the first part) and use the page's
http://www.w3schools.com/jsref/event_onunload.asp
I already tried your method with onunload, the problem with onunload
when I change the page the script getting execute.
I already have javascript to pull php script, just I need php script
doing the flowing; when javascript stop pulling php script. Block in
if statement should run.
Can you give me php script ? please.
Post by Aziz Saleh
Post by hadi
I have javascript to pull my php script every interval of second.
When javascript stop pulling my php script, block in if statement
should execute.
Can someone provide me with the php script please ?
Thank you.
You will have to make the script 2 parts. One to reload and the
other to execute the else (exiting). You need to use ajax to call
the php script every second (for the first part) and use the page's
http://www.w3schools.com/jsref/event_onunload.asp
--
http://www.php.net/unsub.php
louis
2014-07-01 04:39:49 UTC
Permalink
As I said what you need is a cron or daemon process to deal with the
"when the client stop requesting" issue.
If you don't know what is a daemon or cron , just google it a while. If
you do , then the code just like the followings

client_handler.php
<?php
file_put_contents("/tmp/time_cache", time());
//do the rest of you staff
?>

check_daemon.php
//fork a daemon first
while (true) {
sleep(5); //check the time every 5s
$lastest_request_time = file_get_contents("/tmp/time_cache");
if ((time() - $lastest_request_time) > 5) {
// this is the if statement you should enter, and do the staff
in this if
}
}
Post by hadi
Post by louis
your code did not make any sense, PHP code executing from top to bottom
line by line. you can't expect it to enter the loop and do the
unset($_SESSION) staff at the same time.
1. at first you set and unset a session key for 50 times, the $st 's value is the
time() of the 50th loop plus 10 2. you enter a loop and check if time() > $st, as
$st is time() + 10 , it is always larger than time(), so it enter the if statement
and write the temp file , then the process exist
You sow my code and you got my point can you help to fix it.
So if $st is large then time why it should enter if statement. It supposed to stop. Because I already mention if time>$st, so if time is greater than $st not time < $st
Post by louis
your code did not make any sense, PHP code executing from top to bottom
line by line. you can't expect it to enter the loop and do the
unset($_SESSION) staff at the same time.
1. at first you set and unset a session key for 50 times, the $st 's value is the
time() of the 50th loop plus 10 2. you enter a loop and check if time() > $st, as
$st is time() + 10 , it is always larger than time(), so it enter the if statement
and write the temp file , then the process exist
Post by hadi
Post by louis
As http server is stateless, php can not know if a client has stop
requesting.
Post by hadi
Post by louis
you should do your trick on the client side.
But if you insist doing this on the server, you may need to write a
daemon process or crontab job , here's the step
1. Everytime the client make a request, your php code write a
timestamp in a temp file.
2. Your daemon process should read this file and compare to the
timestamp by NOW, if it is larger than you expect ,then do the if
statement thing in the daemon process.
Look what I have accomplished in my script, the problem in my script when
" session_destroy();" and " unset($_SESSION['timeout']);" happen it
supposed to kill "timeout" then "while loop if(time() > $st) " will stop. But
theses not happing im wondering why. Can you look in my code and see?
Post by hadi
<?php
session_start();
?>
<?php
for( $x=0; $x< 50; $x++ )
{
$st = $_SESSION['timeout'] = time() + 10;
session_destroy();
unset($_SESSION['timeout']);
}
$loop= true;
while($loop)
{
if(time() > $st)
{
file_put_contents('/tmp/phptest1234.txt', 'test'); $loop=false; } }
?>
Post by louis
As http server is stateless, php can not know if a client has stop
requesting.
Post by hadi
Post by louis
you should do your trick on the client side.
But if you insist doing this on the server, you may need to write a
daemon process or crontab job , here's the step
1. Everytime the client make a request, your php code write a
timestamp in a temp file.
2. Your daemon process should read this file and compare to the
timestamp by NOW, if it is larger than you expect ,then do the if
statement thing in the daemon process.
Post by hadi
Post by Aziz Saleh
You will have to make the script 2 parts. One to reload and the
other to execute the else (exiting). You need to use ajax to call
the php script every second (for the first part) and use the page's
http://www.w3schools.com/jsref/event_onunload.asp
I already tried your method with onunload, the problem with onunload
when I change the page the script getting execute.
I already have javascript to pull php script, just I need php script
doing the flowing; when javascript stop pulling php script. Block in
if statement should run.
Can you give me php script ? please.
Post by Aziz Saleh
Post by hadi
I have javascript to pull my php script every interval of second.
When javascript stop pulling my php script, block in if statement
should execute.
Can someone provide me with the php script please ?
Thank you.
You will have to make the script 2 parts. One to reload and the
other to execute the else (exiting). You need to use ajax to call
the php script every second (for the first part) and use the page's
http://www.w3schools.com/jsref/event_onunload.asp
--
http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
hadi
2014-07-01 19:04:32 UTC
Permalink
Thank you louis for your code. And your help.

I got another idea just listen to me. I got javascript passing "id" to my php script in interval time, once the "id" not being passed (its mean the user close javascript page). Block in if statement should run. But this scenario is not happening with me.

Check my code
Javascript code:

<script type="text/javascript" src="jquery-1.11.1.js"></script>
<script type="text/javascript" >

$(function(){
// set interval for 5 second
setInterval(function(){
$.ajax({url: "test9.php?id=('1')"

});
}, 50000);
});

</script>

<?php

echo "welcome\n";

?>

Php code:

<?php

while(true)
{
$id = $_GET['id'];
if(isset($_GET['id']))
{
file_put_contents('/tmp/phptest1234.txt', 'test');
}
else
{
file_put_contents('/tmp/phptest.txt', 'test');

}
}
?>
As I said what you need is a cron or daemon process to deal with the "when
the client stop requesting" issue.
If you don't know what is a daemon or cron , just google it a while. If you do ,
then the code just like the followings
client_handler.php
<?php
file_put_contents("/tmp/time_cache", time()); //do the rest of you staff ?>
check_daemon.php
//fork a daemon first
while (true) {
sleep(5); //check the time every 5s
$lastest_request_time = file_get_contents("/tmp/time_cache");
if ((time() - $lastest_request_time) > 5) {
// this is the if statement you should enter, and do the staff in this if
}
}
Post by hadi
Post by louis
your code did not make any sense, PHP code executing from top to
bottom line by line. you can't expect it to enter the loop and do the
unset($_SESSION) staff at the same time.
1. at first you set and unset a session key for 50 times, the $st 's value is the
time() of the 50th loop plus 10 2. you enter a loop and check if
time() > $st, as $st is time() + 10 , it is always larger than
time(), so it enter the if statement and write the temp file , then
the process exist
You sow my code and you got my point can you help to fix it.
So if $st is large then time why it should enter if statement. It
supposed to stop. Because I already mention if time>$st, so if time is
greater than $st not time < $st
Post by louis
your code did not make any sense, PHP code executing from top to
bottom line by line. you can't expect it to enter the loop and do the
unset($_SESSION) staff at the same time.
1. at first you set and unset a session key for 50 times, the $st 's value is the
time() of the 50th loop plus 10 2. you enter a loop and check if
time() > $st, as $st is time() + 10 , it is always larger than
time(), so it enter the if statement and write the temp file , then
the process exist
Post by hadi
Post by louis
As http server is stateless, php can not know if a client has stop
requesting.
Post by hadi
Post by louis
you should do your trick on the client side.
But if you insist doing this on the server, you may need to write a
daemon process or crontab job , here's the step
1. Everytime the client make a request, your php code write a
timestamp in a temp file.
2. Your daemon process should read this file and compare to the
timestamp by NOW, if it is larger than you expect ,then do the if
statement thing in the daemon process.
Look what I have accomplished in my script, the problem in my script when
" session_destroy();" and " unset($_SESSION['timeout']);" happen it
supposed to kill "timeout" then "while loop if(time() > $st) " will
stop. But theses not happing im wondering why. Can you look in my code
and see?
Post by hadi
Post by louis
Post by hadi
<?php
session_start();
?>
<?php
for( $x=0; $x< 50; $x++ )
{
$st = $_SESSION['timeout'] = time() + 10;
session_destroy();
unset($_SESSION['timeout']);
}
$loop= true;
while($loop)
{
if(time() > $st)
{
file_put_contents('/tmp/phptest1234.txt', 'test'); $loop=false; } }
?>
Post by louis
As http server is stateless, php can not know if a client has stop
requesting.
Post by hadi
Post by louis
you should do your trick on the client side.
But if you insist doing this on the server, you may need to write a
daemon process or crontab job , here's the step
1. Everytime the client make a request, your php code write a
timestamp in a temp file.
2. Your daemon process should read this file and compare to the
timestamp by NOW, if it is larger than you expect ,then do the if
statement thing in the daemon process.
Post by hadi
Post by Aziz Saleh
You will have to make the script 2 parts. One to reload and the
other to execute the else (exiting). You need to use ajax to call
the php script every second (for the first part) and use the
http://www.w3schools.com/jsref/event_onunload.asp
I already tried your method with onunload, the problem with
onunload when I change the page the script getting execute.
I already have javascript to pull php script, just I need php
script doing the flowing; when javascript stop pulling php script.
Block in if statement should run.
Can you give me php script ? please.
Post by Aziz Saleh
On Mon, Jun 30, 2014 at 7:56 PM, hadi
Post by hadi
I have javascript to pull my php script every interval of second.
When javascript stop pulling my php script, block in if
statement should execute.
Can someone provide me with the php script please ?
Thank you.
You will have to make the script 2 parts. One to reload and the
other to execute the else (exiting). You need to use ajax to call
the php script every second (for the first part) and use the
http://www.w3schools.com/jsref/event_onunload.asp
--
http://www.php.net/unsub.php
--
http://www.php.net/unsub.php
Stuart Dallas
2014-07-01 19:14:00 UTC
Permalink
Post by hadi
Thank you louis for your code. And your help.
I got another idea just listen to me. I got javascript passing "id" to my
php script in interval time, once the "id" not being passed (its mean the
user close javascript page). Block in if statement should run. But this
scenario is not happening with me.
Hadi,

Please read this carefully. Just listen to me. Please.

The scenario you describe is not happening for you because that's not how
the web works. This is how the web (usually) works...

1) PHP script runs on the server.
2) PHP script outputs content which gets sent to the browser.
3) PHP script finishes running. Beyond this point PHP is NOT running
anymore.
4) Javascript that was in the content now runs in the browser.
5) The Javascript can request something from the server, but this RUNS THE
PHP SCRIPT AGAIN.
6) The PHP script knows nothing about the previous time the script ran
(i.e. steps 1-3 above).

What you are trying to do does not follow the normal way PHP scripts
interact with a browser. That's not to say it's not possible to do what you
want, but until you understand these VERY basic facts about how the web
works I have no intention of trying to explain how to do it.

-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
Tim Streater
2014-07-01 20:13:00 UTC
Permalink
Post by hadi
I got another idea just listen to me. I got javascript passing "id" to my php
script in interval time, once the "id" not being passed (its mean the user
close javascript page). Block in if statement should run. But this scenario is
not happening with me.
If the user closes the page then no PHP script will be run unless you do it with onbeforeunload event.
--
Cheers -- Tim
hadi
2014-07-01 20:31:16 UTC
Permalink
I already tried onbeforeunload and unload event. But the problem when user change the page or navigate to different page code will be executed. And I don’t this to happen.
Thank you for your help.

I just want a method to keep pulling my php script once its stopped block in if statement should run in my php script.
Post by Tim Streater
Post by hadi
I got another idea just listen to me. I got javascript passing "id" to
my php script in interval time, once the "id" not being passed (its
mean the user close javascript page). Block in if statement should
run. But this scenario is not happening with me.
If the user closes the page then no PHP script will be run unless you do it with
onbeforeunload event.
--
Cheers -- Tim
Aziz Saleh
2014-07-01 20:36:09 UTC
Permalink
Post by hadi
I already tried onbeforeunload and unload event. But the problem when user
change the page or navigate to different page code will be executed. And I
don’t this to happen.
Thank you for your help.
I just want a method to keep pulling my php script once its stopped block
in if statement should run in my php script.
Post by Tim Streater
Post by hadi
I got another idea just listen to me. I got javascript passing "id" to
my php script in interval time, once the "id" not being passed (its
mean the user close javascript page). Block in if statement should
run. But this scenario is not happening with me.
If the user closes the page then no PHP script will be run unless you do
it with
Post by Tim Streater
onbeforeunload event.
--
Cheers -- Tim
Do the onunload, and add a check in you PHP (store the info in the DB)
that if the user doesn't refresh within 2 seconds (enough time to navigate
to a different page on the domain), then mark as out.
Stuart Dallas
2014-07-01 20:38:55 UTC
Permalink
Post by hadi
Post by hadi
I already tried onbeforeunload and unload event. But the problem when
user
Post by hadi
change the page or navigate to different page code will be executed. And
I
Post by hadi
don’t this to happen.
Thank you for your help.
I just want a method to keep pulling my php script once its stopped block
in if statement should run in my php script.
Post by Tim Streater
Post by hadi
I got another idea just listen to me. I got javascript passing "id"
to
Post by hadi
Post by Tim Streater
Post by hadi
my php script in interval time, once the "id" not being passed (its
mean the user close javascript page). Block in if statement should
run. But this scenario is not happening with me.
If the user closes the page then no PHP script will be run unless you
do
Post by hadi
it with
Post by Tim Streater
onbeforeunload event.
--
Cheers -- Tim
Do the onunload, and add a check in you PHP (store the info in the DB)
that if the user doesn't refresh within 2 seconds (enough time to navigate
to a different page on the domain), then mark as out.
That's nasty. There should be Javascript attached to any event that might
change the page, that checks where that event is taking the user and is
enabling/disabling the onbeforeunload accordingly. PHP should not be
involved in that logic in any way.

-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
Christoph Becker
2014-07-01 21:26:22 UTC
Permalink
Post by Stuart Dallas
Post by hadi
Do the onunload, and add a check in you PHP (store the info in the DB)
that if the user doesn't refresh within 2 seconds (enough time to navigate
to a different page on the domain), then mark as out.
That's nasty. There should be Javascript attached to any event that might
change the page, that checks where that event is taking the user and is
enabling/disabling the onbeforeunload accordingly. PHP should not be
involved in that logic in any way.
And what if no beforeunload event is triggered (for instance, because
the browser is crashing or it simply doesn't implement the event)?
--
Christoph M. Becker
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Stuart Dallas
2014-07-01 21:49:27 UTC
Permalink
Post by hadi
Post by Stuart Dallas
Post by hadi
Do the onunload, and add a check in you PHP (store the info in the DB)
that if the user doesn't refresh within 2 seconds (enough time to
navigate
Post by Stuart Dallas
Post by hadi
to a different page on the domain), then mark as out.
That's nasty. There should be Javascript attached to any event that might
change the page, that checks where that event is taking the user and is
enabling/disabling the onbeforeunload accordingly. PHP should not be
involved in that logic in any way.
And what if no beforeunload event is triggered (for instance, because
the browser is crashing or it simply doesn't implement the event)?
The unreliability of using this method has already been pointed out to the
OP in a previous thread.

-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
Tim Streater
2014-07-01 21:02:00 UTC
Permalink
Post by hadi
I already tried onbeforeunload and unload event. But the problem when user
change the page or navigate to different page code will be executed.
In your onbeforeunload event handler, you should make a synchronous ajax request to your PHP script (or another script). If you do an asynchronous ajax request, the page unloads and apache will kill the PHP script. With a synchronous request, the page stays until the PHP script completes.
--
Cheers -- Tim
Stuart Dallas
2014-07-01 21:10:18 UTC
Permalink
Post by Tim Streater
Post by hadi
I already tried onbeforeunload and unload event. But the problem when
user
Post by hadi
change the page or navigate to different page code will be executed.
In your onbeforeunload event handler, you should make a synchronous ajax
request to your PHP script (or another script). If you do an asynchronous
ajax request, the page unloads and apache will kill the PHP script. With a
synchronous request, the page stays until the PHP script completes.
That's not his issue; he's already got that covered - see the code he's
previously posted to the list. He's talking about internal links within the
site also triggering the onbeforeunload as well as external links.

-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
Tim Streater
2014-07-01 21:44:00 UTC
Permalink
Post by Stuart Dallas
Post by Tim Streater
Post by hadi
I already tried onbeforeunload and unload event. But the problem when
user
Post by hadi
change the page or navigate to different page code will be executed.
In your onbeforeunload event handler, you should make a synchronous ajax
request to your PHP script (or another script). If you do an asynchronous
ajax request, the page unloads and apache will kill the PHP script. With a
synchronous request, the page stays until the PHP script completes.
That's not his issue; he's already got that covered - see the code he's
previously posted to the list. He's talking about internal links within the
site also triggering the onbeforeunload as well as external links.
Well, you have to consider how to organise/design your site or application, then. That is fundamental to programming. We shouldn't be doing that for people, we should suggest tools/methods that they may have overlooked. After that it's up to them.
--
Cheers -- Tim
Continue reading on narkive:
Loading...