1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

can anyone make me a simple script to write text to a log file on server...

Discussion in 'PHP & Perl' started by SleepieGirl, Aug 31, 2013.

  1. SleepieGirl

    SleepieGirl Regular Member

    Joined:
    Mar 7, 2009
    Messages:
    439
    Likes Received:
    290
    Occupation:
    Need Custom Programs? Message Me!!!
    Location:
    TenDollarBlog.com
    http://myservername.com/myscript.php?password="blah"&text="blah blah blah blah"

    so then on my server ill have log.txt with "blah blah blah blah" inside of it?
     
  2. lincher

    lincher Junior Member

    Joined:
    Jun 29, 2013
    Messages:
    151
    Likes Received:
    39
    Code:
    <?php
    
    $pass = $_GET['password'];
    $text = $_GET['text'];
    
    if($pass = "blah")
    {
    $file = fopen("log.txt","w"); 
    echo fwrite($file,$text) . " bytes written.";
    fclose($file);
    }
    else
    {
    echo "Incorrect password!";
    }
    
    ?>
    
    That should do it.
    Just create log.txt file before using it.
     
    • Thanks Thanks x 1
    Last edited: Aug 31, 2013
  3. amandatorey

    amandatorey Newbie

    Joined:
    Sep 30, 2013
    Messages:
    6
    Likes Received:
    1
    lincher's solution is excellent, but here's a shorter version with no need to create log.txt beforehand and more extensive error-checking.

    Code:
    <?php
    
    isset($_GET['pass'])    or die('you need a password');
    $_GET['pass'] == 'blah' or die('wrong password');
    isset($_GET['text'])    or die('you need to specify text');
    
    file_put_contents('log.txt', $_GET['text']);
    
    ?>
    
     
  4. tripper_john_md

    tripper_john_md Newbie

    Joined:
    Feb 21, 2011
    Messages:
    40
    Likes Received:
    35
    Location:
    Southern Germany
    And one more addition:
    file_put_contents() will overwrite your file without the proper flags set.
    So if you want to log continuously and keep the logged text, use:

    PHP:
    <?php
    file_put_contents
    ('log.txt'$_GET['text'], FILE_APPEND);
    ?>
     
  5. xbyte

    xbyte Regular Member

    Joined:
    Jan 15, 2013
    Messages:
    295
    Likes Received:
    31
    Occupation:
    AccuRanker CEO/Developer
    Home Page:
    If you need something more specific, you can contact me on skype : byte-x
    Wont cost you anything :)