Hello,

I'm new to PHP so please bear with me. Everything seems to work as I wanted it up until "$outputstring" and below (I posted the whole script below). It does not seem to write anything to orders.txt. I verified that document_root = /var/www/html. I am also not receiving any error message. I have verified that /var/www/html/orders.txt is rwx for all users. Please help. thank you.

<?php
// create short variable names
$shirt_qty = $POST['shirt_qty'];
$pants_qty = $
POST['pants_qty'];
$skirt_qty = $POST['skirt_qty'];
$shorts_qty = $
POST['shorts_qty'];
$acc_qty = $POST['acc_qty'];
$address = $
POST['address'];
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$date = date('e H:i, F jS Y'); // changed date format
?>
<html>
<head>
<title>Bob's Clothing Store - Order Results</title>
</head>
<body>
<h1>Bob's Clothing Store</h1>
<h2>Order Results</h2>
<?php

echo "<p>Order processed at ".$date."</p>"; 

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

$totalqty = 0;
$totalqty = $shirt_qty + $pants_qty + $skirt_qty + $shorts_qty + $acc_qty;
echo "Items ordered: ".$totalqty."<br />";


if ($totalqty == 0) {

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

} else {

  if ($shirt_qty > 0) {
	echo $shirt_qty." shirts<br />";
  }

  if ($pants_qty > 0) {
	echo $pants_qty." pants<br />";
  }

  if ($skirt_qty > 0) {
	echo $skirt_qty." skirts<br />";
  }
  if ($shorts_qty > 0) {
	echo $shorts_qty." shorts<br />";
  }

  if ($acc_qty > 0) {
	echo $acc_qty." accessories<br />";
  }
}


$totalamount = 0.00;

define('SHIRTPRICE', 50);
define('PANTSPRICE', 20);
define('SKIRTPRICE', 40);
define('SHORTSPRICE', 30);
define('ACCPRICE', 10); 
$totalamount = $shirt_qty * SHIRTPRICE 
		+ $pants_qty * PANTSPRICE 
		+ $skirt_qty * SKIRTPRICE 
		+ $shorts_qty * SHORTSPRICE 
		+ $acc_qty * ACCPRICE; 
	echo "Subtotal: $".number_format($totalamount,2)."<br />"; 
$taxrate = 0.10;  // local sales tax is 10%
$taxvalue=($totalamount * $taxrate); // calculate tax amount 
echo "Tax: $".number_format($taxvalue). "<br />"; // display tax amount
echo "Total including tax: $".number_format($totalamount + $taxvalue)."<br />";
echo "<p>Address to ship to is ".$address."</p>";

$outputstring = $date."\t".$shirt_qty." shirts\t".$pants_qty." pants\t".$skirt_qty." skirts\t".$shorts_qty." shorts\t".$acc_qty." accessories\t\$".$totalamount."\t". $address."\n";



// open file for appending
$fp = fopen("$DOCUMENT_ROOT/orders.txt", "ab");

flock($fp, LOCK_EX);

fwrite($fp, $outputstring, strlen($outputstring));
fwrite($fp, $outputstring);
flock($fp, LOCK_UN);
fclose($fp);

echo "<p>Order written.</p>";

?>
</body>
</html>

    First, please use the code formatting tags when you paste your code. It makes it much more readable.

    I don't recognize the "ab" second parameter in this line:

    $fp = fopen("$DOCUMENT_ROOT/orders.txt", "ab");

    Also, you should be checking the return result of your fopen call to see if it worked or not:

    if (!$fp) {
      die("Could not open the file");
    }
    

    You should also be checking the result of flock, fwrite, etc.

    Furthermore, that you are trying to open and write to a file in your document root presents a security problem for two reasons:
    1) your document root directory must be writable by your web server for your to create and write this file from this php script
    2) your order information might be accessed by evil visitors if they just go to http://yoursite.com/orders.txt

      Have you tried without using flock()? And can you verify if $outputstring is not empty?

      Also, consider using the forum's [noparse]

      ...

      [/noparse] and [noparse]

      ...

      [/noparse] tags when posting code so it's easier for everyone to read and help you out. 🙂

        Thank you so much for your response. I have followed your suggestions and have also added additional lines to show the specific error. I now get the following error: Warning: fopen(/var/www/html/orders/orders.txt): failed to open stream: Permission denied in /var/www/html/Chapter02/processorder.php on line 84.

        Also, thank you for pointing out security but i've been put in a secure environment (separate from production environment) to learn php and no pertinent information are in place. With that said, I pretty much gave 777 to /var/www/html/Chapter02/* permissions to get this fopen issue resolved. The user and group are both apache. However, I'm still receiving permission denied error. Any ideas?

        ini_set('display_errors', 1); 
        	error_reporting(E_ALL);
        	$fp = fopen("$DOCUMENT_ROOT/orders/orders.txt", "w");
        
        
        flock($fp, LOCK_EX);
        
        if (!$fp) {
        		die("Could not open the file");
            }
        

          Granted, this is yet another thing to learn, but have you considered using a relational database system instead? A SQLite database is essentially just a file, much like your text file, but it has the advantage of being a relational database, which is likely to be a good fit for what you are trying to record.

            sandygirl;11046987 wrote:

            Warning: fopen(/var/www/html/orders/orders.txt): failed to open stream:

            With that said, I pretty much gave 777 to /var/www/html/Chapter02/*

            However, I'm still receiving permission denied error. Any ideas?

            I see the reason. Do you ? 😉 (and I'm not trying to be mean or ugly here). 🙂

              Write a Reply...