2

My entire PHP page only displays as text and no PHP code is executed. It's weird because when I test it using <? phpinfo(); ?> in a test.php file, I get a successful test and it works on my Apache server. However when I attempt to do anything else. It only shows as text.

Edit: Here is the link to the code. I couldn't figure out how to post it here. Pastebin

<?php
  // create short variable names
  $tireqty = $_POST['tireqty'];
  $oilqty = $_POST['oilqty'];
  $sparkqty = $_POST['sparkqty'];
  $find = $_POST['find'];
?>
<html>
<head>
  <title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php

    echo "<p>Order processed at ".date('H:i, jS F Y')."</p>";

    echo "<p>Your order is as follows: </p>";

    $totalqty = 0;
    $totalqty = $tireqty + $oilqty + $sparkqty;
    echo "Items ordered: ".$totalqty."<br />";


    if ($totalqty == 0) {

      echo "You did not order anything on the previous page!<br />";

    } else {

      if ($tireqty > 0) {
        echo $tireqty." tires<br />";
      }

      if ($oilqty > 0) {
        echo $oilqty." bottles of oil<br />";
      }

      if ($sparkqty > 0) {
        echo $sparkqty." spark plugs<br />";
      }
    }


    $totalamount = 0.00;

    define('TIREPRICE', 100);
    define('OILPRICE', 10);
    define('SPARKPRICE', 4);

    $totalamount = $tireqty * TIREPRICE
                 + $oilqty * OILPRICE
                 + $sparkqty * SPARKPRICE;

    echo "Subtotal: $".number_format($totalamount,2)."<br />";

    $taxrate = 0.10;  // local sales tax is 10%
    $totalamount = $totalamount * (1 + $taxrate);
    echo "Total including tax: $".number_format($totalamount,2)."<br />";

    if($find == "a") {
      echo "<p>Regular customer.</p>";
    } elseif($find == "b") {
      echo "<p>Customer referred by TV advert.</p>";
    } elseif($find == "c") {
      echo "<p>Customer referred by phone directory.</p>";
    } elseif($find == "d") {
      echo "<p>Customer referred by word of mouth.</p>";
    } else {
      echo "<p>We do not know how this customer found us.</p>";
    }

?>
</body>
</html>
19
  • how are you trying to view the file? Commented May 21, 2012 at 22:07
  • 1
    server? local? remote? details please. Commented May 21, 2012 at 22:11
  • Well it's nothing to do with the code. The code I have is from a book that I'm using to learn PHP so it is 100% correct. I just don't know how to make it work without appearing as text. Commented May 21, 2012 at 22:11
  • From book so "100% correct" LOL. No, really many books are full of errors. So as asked befog, show us the code. Commented May 21, 2012 at 22:14
  • I have an Apache server running on my Windows computer. I'm using it to try and learn PHP through a book. To view files I have to type in localhost. So like I said, a test is working just fine but when I try to implement the files from the book they don't work and they are correct. Commented May 21, 2012 at 22:14

1 Answer 1

5

I am willing to bet 10$ that test.php uses <?php, and the changed code uses <?, while the server does not understand it as an opening tag since short_open_tags is off in php.ini.

A lot of books use <? for open tags, while most servers only support the long version (<?php). If that's the case, then changing all the simple <? to <?php will do the trick.

PHP code is only exposed in 1 case: When PHP interpreter does not identify it as PHP code. That can be caused by only 2 problems:

  • Wrong configuration of Apache (or other http server) which doesn't handle php files at all.
  • Wrong open tags in PHP files, so PHP doesn't know when code begins.

If the file is a *.php, if Apache is turned on serving *.php files through PHP interpreter, if standard open tags are used or PHP is configured to use other types of used tags, and if you're accessing this PHP file through the browser, in no circumstances would PHP expose this code.

Sign up to request clarification or add additional context in comments.

4 Comments

Saw this recently on a website which was exposing PHP code, played with it for a while. Had fun.
Thanks for the reply. Although I wish that were the case, all of it uses <code><?php</code>
So what would be the cause for a php file not to work today if it worked yesterday on the exact same server with no changes?
well, hell. All is as it should from the code perspective. Files are .PHP, and if you say that yesterday they worked, and now they don't, then something happened either to PHP or to Apache. I suggest you reconfigure them or something. There is nothing wrong with your code or its placement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.