I have the following script which is working well at taking the data from my HTML form and writing it to a .conf file.
<?php
$path = '/usr/local/flowsim/data/phptest.conf';
if (isset($_POST['CollectorIP']) && isset($_POST['CollectorPort']) && isset($_POST['NetflowVersion'])) {
$fh = fopen($path,"a+");
$string = 'collector-ip='.$_POST['CollectorIP']. "n". 'collector-port='.$_POST['CollectorPort']. "n". 'engine='.$_POST['NetflowVersion'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
However I am needing this script to "auto-name" the .conf files differently using the variables from the HTML form. For example, at the moment the script is creating the file phptest.conf
and writing the below info (which will be different each time) which was inputted via the HTML form:
collector-ip=10.0.0.0
collector-port=9000
engine=Netflow Version 10 (IPFIX)
As these three inputs will be unique every time the script is run I would like to use them to name the new file each time the form is submitted.
For example if the collector-ip was 5.5.5.5, collector-port 9996 and engine Netflow Version 10 (IPFIX) the filename would be 5.5.5.5:9996:Netflow Version 10 (IPFIX).conf
.
I am quite new to PHP but I believe this could be achieved by using the (isset($_POST['CollectorIP'])
, ($_POST['CollectorPort'])
and isset($_POST['NetflowVersion'])
variables in the file path which would complete from the inputted data and name the files as expected each time the form is submitted.
Is this correct or do I have it wrong? Would the following script work or is there a better way to do this?
<?php
$path = '/usr/local/flowsim/data/(isset($_POST['CollectorIP']):isset($_POST['CollectorPort']):isset($_POST['NetflowVersion']).conf';
if (isset($_POST['CollectorIP']) && isset($_POST['CollectorPort']) && isset($_POST['NetflowVersion'])) {
$fh = fopen($path,"a+");
$string = 'collector-ip='.$_POST['CollectorIP']. "n". 'collector-port='.$_POST['CollectorPort']. "n". 'engine='.$_POST['NetflowVersion'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
Update
<?php
if ( isset( $_POST['CollectorIP'] ) && isset($_POST['CollectorPort']) && isset($_POST['NetflowVersion']) && isset($_POST['Flowrate']) && isset($_POST['TemplateFrequency']) && isset($_POST['SourceIPAddress']) && isset($_POST['DestinationIPAddress']) ) {
// ok let's try to create the file
$path = '/usr/local/flowsim/data/' . trim($_POST['CollectorIP']) . ':' . trim($_POST['CollectorPort']) . ':' . trim($_POST['NetflowVersion']) . '.conf';
$contents = "";
if ( $fh = fopen($path,"a+") ) {
if ( trim( $_POST['CollectorIP'] ) != "" ) {
$contents .= 'collector-ip=' . $_POST['CollectorIP'];
}
if ( trim( $_POST['CollectorPort'] ) != "" ) {
$contents .= "n" . 'collector-port=' . $_POST['CollectorPort'];
}
if ( trim( $_POST['NetflowVersion'] ) != "" ) {
$contents .= "n" . 'engine=' . $_POST['NetflowVersion'];
}
if ( trim( $_POST['Flowrate'] ) != "" ) {
$contents .= "n" . 'flow-rate=' . $_POST['Flowrate'];
}
if ( trim( $_POST['TemplateFrequency'] ) != "" ) {
$contents .= "n" . 'template-freq=' . $_POST['TemplateFrequency'];
}
if ( trim( $_POST['SourceIPAddress'] ) != "" ) {
$contents .= "n" . 'src-ip=' . $_POST['SourceIPAddress'];
}
if ( trim( $_POST['DestinationIPAddress'] ) != "" ) {
$contents .= "n" . 'dst-ip=' . $_POST['DestinationIPAddress'];
}
if ( fwrite( $fh, $contents ) ) {
}
fclose($fh); // Close the file
}
else {
if (fclose($fh)) {
echo "Netflow traffic is now being sent to the collector at ". ( $_POST['CollectorIP'] ). " on port ". ( $_POST['CollectorPort'] ). ".";
} else {
echo "The simulator was unable to start the traffic flow, please try again.";
}
}
}
?>
2
Answers
Expressions aren’t evaluated inside string literals. You need to use concatenation.
You should be very careful when using POST data in filenames, since the user could put
../../..
in the value to access outside the directory you want to write to. Add some data validation, or usebasename()
to discard the directory part.Before showing the code I think there are a couple of things worth pointing out:
it looks like you’re receiving this data via a post on a web form. Therefore, your intention is to allow users to send data that will be written to a file on your server. This is a big security risk, so you’ll want to be 100% certain that whatever they’re entering is trustworthy.
Assuming the above is correct and this script will live on a web server, most of the time the script will not have write access to create a file / write to a file. So you’ll have to modify permissions etc, which again has security concerns that you’ll have to be aware of
Anyway, as far as the script itself, the line where you’re using
isset
won’t work as it’s written. I would separate the test out and do it like so: