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 > } > } > > 于 2014年07月01日 12:20, hadi 写道: > >> 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 > > 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 > > > > > >> 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 > >> > >> > >> > >> 于 2014年07月01日 11:07, 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; } } > >>> > >>> ?> > >>> > >>>> 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. > >>>> > >>>> 于 2014年07月01日 08:21, hadi 写道: > >>>>>> 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 > >>>>> 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. > >>>>> > >>>>>> On Mon, Jun 30, 2014 at 7:56 PM, hadi > <almarzuki2011@xxxxxxxxxxx> > >>>> wrote: > >>>>>>> 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 > >> > >> -- > >> PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: > >> http://www.php.net/unsub.php > > > -- > PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: > http://www.php.net/unsub.php
<<attachment: smime.p7s>>