Logging specified parts of a form?

Discussion in 'PHP' started by retrospector, Jul 7, 2010.

  1. #1
    Hey there,

    I recently came accross a php code that logs all info submitted into all text boxes on a webpage.

    $handle = fopen("log.txt", "a");
    foreach($_POST as $variable => $value) {
    fwrite($handle, $variable);
    fwrite($handle, "=");
    fwrite($handle, $value);
    fwrite($handle, "\r\n");
    }
    fwrite($handle, "\r\n");
    fclose($handle);
    
    Code (markup):
    Its going to be a registration form that will be asking questions.

    Presuming that username is the variable for username

    and fav_game is the variable for favorite game.


    How would I log these parts of the webpage rather than all of it.
     
    retrospector, Jul 7, 2010 IP
  2. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #2
    
    $handle = fopen("log.txt", "a");
    fwrite($handle, "username=".$_POST['username']);
    fwrite($handle, "\r\n");
    fwrite($handle, "\r\n");
    fwrite($handle, "fav_game=".$_POST['fav_game']);
    fwrite($handle, "\r\n");
    fwrite($handle, "\r\n");
    fclose($handle);
    
    PHP:
    Or if you wish to add some more fields to log in future
    
    $fields = array('username','fav_game');
    $handle = fopen("log.txt", "a");
    foreach($fields as $variable) {
      if(!isset($_POST[$variable])) continue;
      fwrite($handle, $variable);
      fwrite($handle, "=");
      fwrite($handle, $_POST[$variable]);
      fwrite($handle, "\r\n");
    }
    fwrite($handle, "\r\n");
    fclose($handle);
    
    PHP:
     
    AsHinE, Jul 7, 2010 IP