[英]How can I check the browser connection with server still active?
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 . 所以你可以回答php,java我认为如果它可能,那么它可能在每个为客户端 - 服务器架构设计的编程语言上。
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. 您可以创建一个javascript函数,以间隔加载PHP页面。
Something like this: 像这样的东西:
create 3 files: file.js, onlinecheck.php and log.txt 创建3个文件:file.js,onlinecheck.php和log.txt
file.js: 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: 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). 您可以使用时间戳更新会话变量,并检查变量是否低于或等于/略大于更新间隔,而不是在Paul L的答案中使用日志文件。
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). 您可以让来自浏览器的JavaScript调用从服务器接收答案:已完成/未完成,并且在检索完成后,让用户通过按下现在可见的按钮来符合他的事务(在给定时间内,否则您应该使回滚)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.