PHP echo printing language construct

780 views Asked by At

In an assignment I'm having trouble running a php script with page handling. It's outputting actual php code when submitted through another php page but works fine on its own.

I have a html login page which submits via submit buttons rather than form submit [a requirement]. This submits to login.php. Seperately I have testBalance.php which checks a file balance.txt on my server which simply has an amount (1000). testBalance.php calls a function in getBalance.php to return the amount here.

THE PROBLEM IS when I run testBalance.php by itself it works just fine. 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: "); 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?

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

There are 1 answers

0
Marc B On BEST ANSWER

readfile() doesn't EXECUTE anything it reads. 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. Or you could try eval(), but you really don't want to go down that route. eval() is evil and dangerous.