Logging a http post by a PHP script

Discussion in 'PHP' started by smartcard, Apr 19, 2013.

  1. #1
    I need somebody to help me to capture the http post coming to a PHP script.

    For example I will host this script at my website : http: //mydom.com/http_analyser.php

    When I post a data like this http: //mydom.com/http_analyser.php?u...this+is+a+test
    I want to log this data to a log / txt file.
     
    Solved! View solution.
    smartcard, Apr 19, 2013 IP
  2. merlin77

    merlin77 Active Member

    Messages:
    54
    Likes Received:
    3
    Best Answers:
    2
    Trophy Points:
    78
    #2
    Try this, should work

    <?php
     
    $query = $_SERVER['QUERY_STRING'];  // gets the query string info
     
    $fp = fopen("querystrings.txt", "a"); // opens the file and appends
     
    flock($fp, LOCK_EX); // lock the file
     
    fwrite($fp, $query."\r\n"); // write to the file
     
    flock($fp, LOCK_UN); // unlock the lock
     
    fclose($fp); // close the file
     
    ?>
    PHP:
     
    merlin77, Apr 19, 2013 IP
  3. smartcard

    smartcard Well-Known Member

    Messages:
    278
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3

    Great, can you please tell me how to include the URL to the log file too? When I tried your script it is logging the query eg:
    username=jone&password=pass&subj=Testing
    HTML:
    How can I log the URL of the incoming POST too like:
    http://mydom.com/script.php?username=jone&password=pass&subj=Testing
    HTML:
     
    smartcard, Apr 19, 2013 IP
  4. #4
    I think this is what you want, test it and let me know.
    <?php
     
    $query = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
     
    $fp = fopen("querystrings.txt", "a"); // opens the file and appends
     
    flock($fp, LOCK_EX); // lock the file
     
    fwrite($fp, $query."\r\n"); // write to the file
     
    flock($fp, LOCK_UN); // unlock the lock
     
    fclose($fp); // close the file
     
    ?>
    PHP:
     
    merlin77, Apr 20, 2013 IP
    GMF likes this.
  5. smartcard

    smartcard Well-Known Member

    Messages:
    278
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    Thanks a lot, it works the way I want.
     
    smartcard, Apr 21, 2013 IP