0

訪問期間のログ記録に問題があります。

次のようなテストhtmlファイルを作成しました。

<!DOCTYPE html>
<html>
    <body>
        <script language="JavaScript" type="text/javascript">

            function enter() {
                this.chrono = new Date().getMilliseconds();
                alert("test");
            }

            function leave() {
                this.chrono = new Date().getMilliseconds() - this.chrono;

                var myAjax = new Ajax.Request('visitor_log/ajax_store_visit_duration.php?visit_duration=' + this.chrono.toString(),{
                        method: 'get',
                        onComplete:handlerFunction
                });

                return null;
            }

            window.onload = enter;
            window.onbeforeunload = leave;
        </script>
    </body>
</html>

PHP ファイル (visitor_log/ajax_store_visit_duration.php):

<?php

if(isset($_GET["visit_duration"]))
{
    $text = $_GET["visit_duration"];

    log($text);
}
else die("error");

function log($text)
{
    $myFile = "test.txt";
    $fh = fopen($myFile, 'wb');
    fwrite($fh, $text);
    fclose($fh);
}

?>

ブラウザに入力すると:

http://localhost/visitor_log/ajax_store_visit_duration.php?visit_duration=123

onbeforeunload必要に応じてテキストファイルを作成しますが、イベントでの AJAX 呼び出しが機能していないようです。

私のコードの何が問題なのですか?


編集:

AJAX 呼び出しの問題を見つけるためのテスト関数を作成しました。

        function testajax(){
            this.chrono = new Date().getMilliseconds() - this.chrono;

           var blockingRequest = new XMLHttpRequest();
            blockingRequest.open("GET", "visitor_log/ajax_store_visit_duration.php?visit_duration=" + 123, false); // async = false
            blockingRequest.send();

            return null;
        }

        window.onload = testajax;
    </script>
</body>

これも機能していません。

4

1 に答える 1