刷新问题?脚本在每次刷新时保持 运行

Refresh Issue? Script keeps running on every refresh

每次按下刷新都会从文件中删除我的 txt。每当有玩家在我的游戏中获胜时,我只需要此脚本为 运行。但它似乎 运行 每次刷新。

这是索引

<?php
    $win = 0;
    $cards = 5;
    $probability = 0.5;

    function generateScratchCard($n,$p) {
        $w = 0;
        for($i = 0; $i<$n; $i++) {
            $result = (mt_rand() / mt_getrandmax());
            if($result >= $p) {
                $w++;
            }
        }
        return $w;
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <script type="text/javascript" src="assets/js/scratch.min.js"></script>
        <script>
            <?php
                $win = generateScratchCard($cards, $probability);
                echo "var win = ".$win."; ";
                echo "var cards = ".$cards."; ";
            ?>
            var p = 0.5;
            var scratched = 0;

            function callback(d) {
                scratched++;
                if(scratched >= cards) {
                    if(win > (cards/2)) {
                        document.getElementById('result').innerHTML = '<?php include_once ("file.php"); ?>';
                    } else {
                        document.getElementById('result').innerHTML = 'You lose, but you can try again!';
                    }
                }
            }

            //+ Function written by Jonas Raoni Soares Silva
            //@ http://jsfromhell.com/array/shuffle [v1.0]
            function shuffle(o){ //v1.0
                for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
                return o;
            };

            window.onload = function() {

                var container = document.getElementById('container');
                var cardsArray = [];
                for(var i=0, l=cards; i<l; i++) {
                    if(i<win) {
                        cardsArray[i] = true;
                    } else {
                        cardsArray[i] = false;
                    }
                }
                shuffledArray = shuffle(cardsArray);
                for(var i=0; i<cards; i++) {
                    var scratchCard = document.createElement('div');
                    var backgroundImage;
                    if(shuffledArray[i]) {
                        backgroundImage = 'assets/images/win.png';
                    } else {
                        backgroundImage = 'assets/images/lose.png';
                    }

                    container.appendChild(scratchCard);
                    createScratchCard({
                        'container':scratchCard,
                        'background':backgroundImage,
                        'foreground':'assets/images/foreground.png',
                        'percent':40,
                        'coin':'assets/images/coin2.png',
                        'thickness':18,
                        'counter':'percent',
                        'callback':'callback'
                    });
                }


            };
        </script>
        <style>
            #container { margin: 50px auto; width: 900px; }
            #container div { display: inline-block; vertical-align: top; }
            #result { font-size: 15px; text-align: center; color: #514d4d; text-transform: uppercase; font-weight: bold; padding: 15px 0 15px; }
        </style>
    </head>
    <body>
        <div id="container"></div>
        <div id="result"></div>
    </body>
</html>

这是file.php

<?php
$coupons = file("coupons.txt", FILE_IGNORE_NEW_LINES);
echo "Your Winning Code Is: ".htmlspecialchars($coupons[0]).".";
array_shift($coupons); // Removes the first coupon
file_put_contents("coupons.txt", implode("\n", $coupons));
?>

感谢您的帮助:)

sample here

如果您将 PHP 文件直接包含在 HTML 的正文中,它会在每次页面加载期间打印出来并执行。野兽的本性。为了根据 Javascript 事件触发 PHP 文件,您需要 ajax。这是假设 jQuery library to handle ajax,因为自己滚动有点费力。这是包含 jQuery:

的快速方法
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

这里是回调函数:

   function callback(d) {
        scratched++;
        if(scratched >= cards) {
            if(win > (cards/2)) {
                $.ajax({
                    method: "GET",
                    url: "file.php"
                })
                .done(function( msg ) {
                    document.getElementById('result').innerHTML = msg;
                });
            } else {
                document.getElementById('result').innerHTML = 'You lose, but you can try again!';
            }
        }
    }