#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    58
    Rep Power
    9

    Document_Root & flock


    In the code below what is the "Document_Root" all about. It was listed once at the top of the page on once as an argument to the fpen function. I had to remove it in order to get this script to work? Also, what is flock? (I'M assuming it's file lock, but I'M not sure. The PHP book I'M reading has failed to explain this. "PHP and MySQL Web Development"

    PHP Code:
    <?php
      
    // create short variable names
      
    $tireqty $_POST['tireqty'];
      
    $oilqty $_POST['oilqty'];
      
    $sparkqty $_POST['sparkqty'];
      
    $address $_POST['address'];
      
    $DOCUMENT_ROOT $_SERVER['DOCUMENT_ROOT'];
      
    $date date('H:i, jS F Y');
    ?>
    <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;

        
    $totalamount=number_format($totalamount2'.'' ');

        echo 
    "<p>Total of order is $".$totalamount."</p>";
        echo 
    "<p>Address to ship to is ".$address."</p>";

        
    $outputstring $date."\t".$tireqty." tires \t".$oilqty." oil\t"
                        
    .$sparkqty." spark plugs\t\$".$totalamount
                        
    ."\t"$address."\n";



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

        
    flock($fpLOCK_EX);

        if (!
    $fp) {
          echo 
    "<p><strong> Your order could not be processed at this time.
                Please try again later.</strong></p></body></html>"
    ;
          exit;
        }

        
    fwrite($fp$outputstringstrlen($outputstring));
        
    flock($fpLOCK_UN);
        
    fclose($fp);

        echo 
    "<p>Order written.</p>";
    ?>
    </body>
    </html>
    Last edited by Garrett85; December 2nd, 2012 at 10:14 PM. Reason: Did not turn out right the first time. I clicked the php button before pasting and selecting the text.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,126
    Rep Power
    9398
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    58
    Rep Power
    9
    The book "PHP and MySQL Web Development, Fourth Edition" by Luke Welling & Laura Thomson.


    Originally Posted by requinix
    Where did you get this script from?
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,126
    Rep Power
    9398
    Hmm, now why did I ask that...

    $_SERVER["DOCUMENT_ROOT"] is the root of your website on the filesystem. Like /home/garrett85/public_html or /var/www or C:\inetpub. Remember that it's a file location, not a URL location, so you can't use it in URLs.

    flock() allows you to lock a file against other processes - like other PHP scripts - reading and/or writing to the file. This script uses LOCK_EX which is an exclusive lock, and only one process can have it at a time. That means that if two of these scripts run at the exact same time then one will write to the file completely before the other gets to and there's no chance of any kind of "overlapping" writes.

    As for what you had to do to make it work, what exactly did you change?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    58
    Rep Power
    9

    just my reply to your reply


    I changed the "@ fp = fopen(...)" line to @ $fp = fopen("orders.txt", 'ab'); and commented out the document_root line at the top of the page. The book I'M reading didn't at all explain flock and I didn't understand it's explanation of document_root.

    Originally Posted by requinix
    Hmm, now why did I ask that...

    $_SERVER["DOCUMENT_ROOT"] is the root of your website on the filesystem. Like /home/garrett85/public_html or /var/www or C:\inetpub. Remember that it's a file location, not a URL location, so you can't use it in URLs.

    flock() allows you to lock a file against other processes - like other PHP scripts - reading and/or writing to the file. This script uses LOCK_EX which is an exclusive lock, and only one process can have it at a time. That means that if two of these scripts run at the exact same time then one will write to the file completely before the other gets to and there's no chance of any kind of "overlapping" writes.

    As for what you had to do to make it work, what exactly did you change?

IMN logo majestic logo threadwatch logo seochat tools logo