What I’m wanting to achieve is basically setting variables from data entered through my form (these variables are then used throughout my website). I’m not to concerned with security as I’m the only one who would have access to the form.
So at the moment I am using fwrite to save the data in separate files and then using file_get_contents for each variable. The data from the form is small, one or two words for each field.
I’m unable to use a database so below is an example of what I have working at the moment, can it be improved or is there any other ways of achieving this?
<?php
if(isset($_REQUEST['sub']))
{
$myFile = "first.php";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $_REQUEST['first'];
$string = preg_replace("/[^a-zA-Z0-9.:,=?%\/\\s]/", "", $stringData);
fwrite($fh, $string);
fclose($fh);
$myFile_second = "second.php";
$fh2 = fopen($myFile_second, 'w') or die("can't open file2");
$stringData2 = $_REQUEST['second'];
$string2 = preg_replace("/[^a-zA-Z0-9.:,=?%\/\\s]/", "", $stringData2);
fwrite($fh2, $string2);
fclose($fh2);
$myFile_third = "third.php";
$fh3 = fopen($myFile_third, 'w') or die("can't open file3");
$stringData3 = $_REQUEST['third'];
$string3 = preg_replace("/[^a-zA-Z0-9.:,=?%\/\\s]/", "", $stringData3);
fwrite($fh3, $string3);
fclose($fh3);
}
$first = file_get_contents ("first.php");
$second = file_get_contents ("second.php");
$third = file_get_contents ("third.php");
?>
<form method="post" name="installer">
<div id="field">
<label>First</label>
<input type="text" name="first" value="<?php echo $first; ?>" />
</div>
<div id="field">
<label>Second</label>
<input type="text" name="second" value="<?php echo $second; ?>" />
</div>
<div id="field">
<label>Third</label>
<input type="text" name="third" value="<?php echo $third; ?>" />
</div>
<div id="submit">
<input type="submit" value="Save" name="sub" />
</div>
</form>
Any suggestions would be much appreciated thanks 🙂
Update
Well thank you to everyone for your help and suggestions. I’ve now implemented something similar to below and I must say it works wonderfully!
<?
if (isset($_REQUEST['sub'])) {
$files_data = array(
'first' => &$_REQUEST['first'],
'second' => &$_REQUEST['second'],
'third' => &$_REQUEST['third']
);
$files_data = preg_replace("/[^a-zA-Z0-9.:,=?%\/\\s]+/", "", $files_data);
file_put_contents('data.txt', serialize($files_data)) !== FALSE or die("Can't write to file!" . PHP_EOL);
}
$files_data = unserialize(file_get_contents('data.txt'));
$first = $files_data[first];
$second = $files_data[second];
$third = $files_data[third];
?>
<form method="post" name="installer">
<div id="field">
<label>First</label>
<input type="text" name="first" value="<?php echo $first; ?>" />
</div>
<div id="field">
<label>Second</label>
<input type="text" name="second" value="<?php echo $second; ?>" />
</div>
<div id="field">
<label>Third</label>
<input type="text" name="third" value="<?php echo $third; ?>" />
</div>
<div id="submit">
<input type="submit" value="Save" name="sub" />
</div>
</form>
I guess you are doing this for persistence (i.e. the web server reboots and you want your data to be there once it’s back up)? So if you are stuck with writing, reading and parsing a multitude of files it’s hard to shave massive amounts of time from your execution.
You could look at fscan and see if that will make the reading a bit faster for you.
Other than that I recommend that you cache the data once you have parsed the files so you don’t have to do it over and over and only rescan once the files change. You could keep track of this by a checksum or looking at the files modified time stamp.
One way to “simplify” it would be to save your posted data in an array and serializing that and saving it to a file. Then you could read the entire file in one go and unserialize it and you would have your array with the data back.
A quick-and-dirty way to save a array to a file would be something like
and to get the array back