简体   繁体   English

PHP回显打印语言构造

[英]PHP echo printing language construct

In an assignment I'm having trouble running a php script with page handling. 在一项作业中,我无法通过页面处理来运行php脚本。 It's outputting actual php code when submitted through another php page but works fine on its own. 当通过另一个php页面提交时,它会输出实际的php代码,但可以正常工作。

I have a html login page which submits via submit buttons rather than form submit [a requirement]. 我有一个html登录页面 ,它通过提交按钮而不是表单提交提交[要求]。 This submits to login.php . 这将提交给login.php Seperately I have testBalance.php which checks a file balance.txt on my server which simply has an amount (1000). 另外,我有testBalance.php ,它检查服务器上的balance.txt文件,该文件的数量仅为(1000)。 testBalance.php calls a function in getBalance.php to return the amount here. testBalance.php在getBalance.php中调用一个函数以在此处返回金额。

THE PROBLEM IS when I run testBalance.php by itself it works just fine. 问题是当我自己运行testBalance.php时 ,它工作得很好。 Displaying "Account Balance: 1000.00" but when I attempt to set (in login.php ) testBalance.php as the redirect url, the page literally displays code from my testBalance.php page: "Account balance: "); 显示“帐户余额:1000.00”,但是当我尝试将(在login.php中testBalance.php设置为重定向URL时,该页面从字面上显示了我的testBalance.php页面中的代码:“帐户余额:”); printf ( "%01.2f", $returnValue ); printf(“%01.2f”,$ returnValue); echo (" "); 回声(“”); ?> " I know it's convoluted, this is an intro to php portion of an web prog. class. I'm guessing it has to do with the value pairs that are being passed through to the pages. Can anyone help? ?>“我知道它是复杂的,这是Web prog。类的php部分的简介。我猜想它与传递给页面的值对有关。有人可以帮忙吗?

LOGIN.HTML snippit 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';" /> 

LOGIN.PHP 登录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 testBalance.php身体片段


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

getBalance.php 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() doesn't EXECUTE anything it reads. readfile()不执行任何读取的内容。 It's literally just slurping in the file's bytes and spitting them out to the client. 从字面上看,它只是读取文件的字节,然后将其吐给客户端。 It's basically doing 基本上在做

echo file_get_contents(...);

If you want your other files to be executed, you need to include() or require() them instead. 如果要执行其他文件,则需要include()require() Or you could try eval() , but you really don't want to go down that route. 或者,您可以尝试eval() ,但您确实不想走那条路。 eval() is evil and dangerous. eval()是邪恶且危险的。

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

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