Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Noob in need of help - write to text file

    Hi,

    I'm new to PHP, im used to coding in asp, so please bare with me. What i would like is to output the result to a text file

    <?php
    $_ip = $_SERVER['REMOTE_ADDR'];
    echo "<b>Enter the IP or the domain name of the server that you are trying to route.</b><br>";
    echo "<form method='post' action='?do=route'><input type='text' name='domain' class='input_login' value='$_ip'>&nbsp;<input type='submit' value='Route' class='input_login'></form>";
    if($_GET['do'] == 'route')
    {
    $_domain = $_GET['domain'];
    echo "<pre>";
    system ("tracert $_domain") ;
    echo "</pre>";
    }
    echo "<br>";


    $your_data = "I WANT THE OUTPUT OF TRACEROUTE HERE.";

    // Open the file and erase the contents if any
    $fp = fopen("hostnamehere.txt", "w");

    // Write the data to the file
    fwrite($fp, $your_data);

    // Close the file
    fclose($fp);

    ?>

    I have combined 2 files, a tracert file and one that writes to a text file, how do i define the output of the traceroute as a variable so i can pass it to the part that writes to a text file, i have tried a few ways but end up getting errors.

    Any help appreciated.

    Chris.
    Last edited by csandbach; 08-10-2007 at 10:26 PM. Reason: Clarity

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    You need to use output buffering.
    *Note that you can't have any output sent before you use these functions

    PHP Code:
    ob_start();

    echo 
    "<pre>";
    system ("tracert $_domain") ;
    echo 
    "</pre>";

    $buffer ob_get_flush();
    file_put_contents('buffer.txt'$buffer); 
    You should definately filter the $_domain, and all of your GPC data for that matter.
    http://www.php.net/manual/en/ref.outcontrol.php
    http://us2.php.net/manual/en/function.urlencode.php
    http://us2.php.net/manual/en/function.htmlentities.php
    http://us2.php.net/manual/en/functio...ecialchars.php

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for that

    $_ip = $_SERVER['REMOTE_ADDR'];
    echo "<b>Enter the IP or the domain name of the server that you are trying to route.</b><br>";
    echo "<form method='post' action='?do=route'><input type='text' name='domain' class='input_login' value='$_ip'>&nbsp;<input type='submit' value='Route' class='input_login'></form>";
    if($_GET['do'] == 'route')
    {
    $_domain = $_GET['domain'];
    ob_start();

    system ("tracert $_domain") ;


    $buffer = ob_get_flush();
    file_put_contents('buffer.txt', $buffer);

    I still cant get it to work, im running on 2k3 with iis and the latest version of php 5 stable.

    Should i be looking in my php.ini for anything or am i just bing dumb?

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    You need to use $_POST, not $_GET for form variables when you use method="post".

    PHP Code:
    <?php
    if($_GET['do'] == 'route')
    {
        
    $_domain escapeshellarg($_POST['domain']);
        
    ob_start();
        
    system("tracert $_domain");
        
    $buffer ob_get_clean();
        
    file_put_contents('buffer.txt'$buffer); 
    }

    $_ip $_SERVER['REMOTE_ADDR'];
    echo 
    "<b>Enter the IP or the domain name of the server that you are trying to route.</b><br>";
    echo 
    "<form method='post' action='?do=route'><input type='text' name='domain' class='input_login' value='$_ip'>&nbsp;<input type='submit' value='Route' class='input_login'></form>";
    ?>
    I switched to ob_get_clean(), that's what I meant before.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •