PHP网站无法正常工作

时间:2015-07-27 18:32:19

标签: php web

我正在尝试建立一个在线"银行"我和我的朋友们在游戏中使用,比如Minecraft。我得到了大部分工作,但交易的东西。设置是我使用文本文件作为我们所拥有的金额的存储,然后从我的PHP代码中调用那些文本文件,但数学运算正常。我将链接到压缩文件整个网站的版本。我确实开始了一个会议,以及所有这些事情。

压缩文件:[GOOGLE DOCS HOSTED ZIP]



<!DOCTYPE html>
<html>
<body>
<?php
$riley = fopen("riley.txt", "r") or die("Unable to read data!");
$ethan = fopen("ethan.txt", "r") or die("Unable to read data!");
$ethanw = fopen("ethan.txt", "w") or die("Unable to write data!");
$rileyw = fopen("riley.txt", "w") or die("Unable to write data!");
$user = $_SESSION["user"];
$amm = $_POST["amm"];
if ($user == "ethan") {
	$txt = $ethan - $amm;
	fwrite($ethanw, $txt);
	$txt = $riley + $amm;
	fwrite($rileyw, $txt);
} 
if ($user == "riley") {
	$txt = $riley - $amm;
	fwrite($rileyw, $txt);
	$txt = $ethan + $amm;
	fwrite($ethanw, $txt);
} 
fclose($riley);
fclose($ethan);
?>
<p> transaction made. Redirecting to home in 3 seconds </p>
<?php
sleep(3);
header("Location: bank.php");
die();
?>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

这里的问题是您正在引用文件包装器,而不是文件本身的内容。

实施例: $txt = $ethan - $amm;

如果通过,比方说,10美元,这行代码实际上正在执行..

$txt = [file wrapper] - 10;

由于文件包装器不是值,因此您的数学错误。您需要使用以下内容读取包装器:http://php.net/manual/en/function.fread.php

OR

完全使用不同的功能。这就是我要做的。我会传递这些变量:amountuser。我会将user设置为受影响用户的银行帐户(当然,除了我自己的帐户,也可以设置。)

<!DOCTYPE html>
<html>
    <body>
    <?php

    $users = array('riley', 'ethan');
    $banks = array();

    foreach($users as $account_holder) {

        // Set the path of your file holding this persons bank
        $file_path = $account_holder . ".txt";

        // Lets set the $values[name] variable to the contents of the file
        // we also cast this as an integer, so that if empty, it will still be zero
        // I also prepended the function with @, to suppress any errors (if this bank doesnt exist yet)
        $banks[ $account_holder ] = (int) @file_get_contents($file_path);

    }

    function saveBanks() {

        // Access from within the function scope
        global $banks;

        // Loop through the banks
        foreach($banks as $account_holder => $balance) {

            // Set the path of your file holding this persons bank
            $file_path = $account_holder . ".txt";

            // Open the file, create if necessary, empty either way
            $fp = fopen($file_path, 'w');

            // Write balance to file
            fwrite($fp, $balance);

            // Close file wrapper
            fclose($fp);

        }

    }

    // Let's grab our active user, and transaction amount
    // If youre not familiar with this 'if' style, it's: condition ? value if true : value if false;
    // http://davidwalsh.name/php-ternary-examples
    $active_user = isset($_SESSION["user"]) ? $_SESSION["user"] : die("There is no active user.");
    $txn_amount  = isset($_POST["amount"])  ? $_POST["amount"]  : die("There was no transaction amount received.");
    $txn_user    = isset($_POST["user"])    ? $_POST["user"]    : die("There's no receiving bank account.");

    // Process the transaction, check for validity
    if(!isset($banks[ $txn_user ])) die("That receiving bank account doesn't exist.");

    // Adjust balances
    $banks[ $active_user ] -= $txn_amount;
    $banks[ $txn_user    ] += $txn_amount;

    // Write to files
    saveBanks();

    ?>

    <p> Transaction made. Redirecting to home in 3 seconds. </p>
    <p> New Balances: <?php foreach($banks as $user=>$bal): ?> <li><b><?php echo $user; ?></b>: <?php echo $bal; ?></li><?php endforeach; ?></p>
    <meta http-equiv="refresh" content="3;url=bank.php" />


    </body>
</html>