...

View Full Version : Noob in need of help - write to text file



csandbach
08-10-2007, 09:54 PM
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.

Inigoesdr
08-10-2007, 10:28 PM
You need to use output buffering.
*Note that you can't have any output sent before you use these functions


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/function.htmlspecialchars.php

csandbach
08-10-2007, 11:29 PM
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?

Inigoesdr
08-11-2007, 04:48 AM
You need to use $_POST, not $_GET for form variables when you use method="post".



<?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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum