PHP echo打印语言构造

时间:2014-11-12 17:09:04

标签: php html output echo submit-button

在一项作业中,我无法运行带有页面处理功能的php脚本。它通过另一个php页面提交时输出实际的PHP代码,但它自己工作正常。

我有一个 html登录页面,它通过提交按钮提交而不是表单提交[要求]。提交 login.php 。 我单独使用 testBalance.php 来检查我的服务器上的文件 balance.txt ,该文件只有一个数量(1000)。 testBalance.php调用 getBalance.php 中的函数来返回此处的金额。

问题 当我单独运行 testBalance.php 时,它运行正常。显示“帐户余额:1000.00”,但当我尝试设置(在 login.php 中) testBalance.php 作为重定向网址时,页面会显示我的testBalance中的代码。 php页面:“账户余额:”); printf(“%01.2f”,$ returnValue);回声(“ “);?>”我知道这是错综复杂的,这是对web prog的php部分的介绍。类。我猜它与传递给页面的值对有关。有人可以帮忙吗?

LOGIN.HTML snippit


 <input type="button" name="sub_but" id="bal"   value="check balance"
 onclick="location.href = 'login.php' + '?' + 'name='+ document.forms[0].username.value + 
 '&amp;redirectURL=' + 'bal';" />

的login.php


<?php
    $NAME=$_GET["name"];
    $PAGE=$_GET["redirectURL"];
    $DESTINATION="";

if ($NAME == ''){   /* HANDLES NAME ERRORS */
    echo "PLEASE RETURN AND ENTER A NAME.";
    }
elseif (ctype_alpha(str_replace(' ', '', $NAME)) === false) {
    echo "$NAME is not a valid name. Name must contain letters and spaces only";
    }
else{
    if($PAGE=='with'){
        $DESTINATION = "withdraw.html";
    }
    elseif($PAGE=='bal'){
        //$DESTINATION = "balance.html";
        $DESTINATION = "testBalance.php";
    }
    elseif($PAGE=='depos'){
        $DESTINATION = "deposit.html";
    }
    elseif($PAGE=='weath'){
        $DESTINATION = "weather.html";
    }
    elseif($PAGE=='xchang'){
        $DESTINATION = "currency.html";
    }
                                                       /*echo("$DESTINATION\r\n");*/
    
    header("Content-Length: " .
    strlen(file_get_contents($DESTINATION)));
    header("Cache-Control: no-cache");
    readfile($DESTINATION);
    }
?>

testBalance.php body snippit


        <?php
            include 'getBalance.php';
            $returnValue = readBalance();
            echo "<p>Account balance: ";
            printf( "%01.2f", $returnValue );
            echo "</p>";
        ?>
        

getBalance.php


 <?php
    function readBalance(){
        $file = "balance.txt";
        $fp = fopen($file, "r");
        if (!$fp){
            echo "<p>Could not open the data file.</p>";
            $balance = 0;
        }
        else{
            $balance = fgets($fp);
            fclose ($fp);      
        }
        return $balance;
    }
?>

1 个答案:

答案 0 :(得分:0)

readfile()不会执行任何内容。它实际上只是在文件的字节中啜饮并将它们吐出到客户端。它基本上是在做

echo file_get_contents(...);

如果您希望执行其他文件,则需要include()require()。或者你可以尝试eval(),但你真的不想沿着那条路走下去。 eval()是邪恶和危险的。