문제

When money transfer occurring then if the browser closes what happen then? I mean when I am processing on server then before commit all changes I want to check the client is still online. If the suddenly closes browser then undone the changes.

My code is like :

function some(){
 do_some_stmt;//also keep track what changes has been done
 do_some_stmt;//also keep track what changes has been done
 ...............

if(connection_is_still_alive()){

// final commit
}else{
  undo_the_previous_changes();
}
}

So you can answer with php,java I think if its possible then its possible on every programming languages that design for client-server architecture .

Basically I want to know that in a middle of script is it possible to check the browser still online?

Any suggestion is welcome and thanks in advance for make me understand.

도움이 되었습니까?

해결책

You could make a javascript function which loads a PHP-Page in intervals.

Something like this:

create 3 files: file.js, onlinecheck.php and log.txt

file.js:

function tellTheServerThatIAmStillHere() {
  $.post('onlinecheck.php', {onlineStatus: iAmStillHere}); /* Sends a post to the file onlinecheck.php with the parameter "onlineStatus: iAmStillHere" */
  timer = window.setTimeout(tellTheServerThatIAmStillHere,8000); /* sends a new post every 8 seconds */
}

onlinecheck.php:

<?php
  if(isset($_POST["onlineStatus"])) {
    if($_POST["onlineStatus"] == "iAmStillHere") {
      $fd = fopen("log.txt", "w");
      fwrite($fd, date("[d.m.Y - H:i:s] ").$_SERVER['REMOTE_ADDR'].": He is still here"); //Puts out something like "[26.03.2014 - 13:57:33] 127.0.0.1: He is still here"
      fclose($fd);
    }
  }

Now you could check in the logfile if the browser is still online, if that's what you meant.

다른 팁

Instead of using a log file in the answer from Paul L, you can update a session variable with a time stamp and check the variable to be lower or equal/slightly larger than your update interval).

But you can never be sure that the user hasn't closed the the browser a millisecond before.

You can let the JavaScript call from the browser receive an an answer from the server: finished / not finished, and when finished is retrieved let the user conform he transaction by pushing a now visible button (within a given time, otherwise you should make your roll-back).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top