繁体   English   中英

PHP回显打印语言构造

[英]PHP echo printing language construct

在一项作业中,我无法通过页面处理来运行php脚本。 当通过另一个php页面提交时,它会输出实际的php代码,但可以正常工作。

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

问题是当我自己运行testBalance.php时 ,它工作得很好。 显示“帐户余额:1000.00”,但是当我尝试将(在login.php中testBalance.php设置为重定向URL时,该页面从字面上显示了我的testBalance.php页面中的代码:“帐户余额:”); printf(“%01.2f”,$ returnValue); 回声(“”); ?>“我知道它是复杂的,这是Web prog。类的php部分的简介。我猜想它与传递给页面的值对有关。有人可以帮忙吗?

LOGIN.HTML代码段


  <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';" /> 

登录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身体片段


  <?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; } ?> 

readfile()不执行任何读取的内容。 从字面上看,它只是读取文件的字节,然后将其吐给客户端。 基本上在做

echo file_get_contents(...);

如果要执行其他文件,则需要include()require() 或者,您可以尝试eval() ,但您确实不想走那条路。 eval()是邪恶且危险的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM