这段 PHP 代码可以改进吗?
我想要实现的基本上是根据通过表单输入的数据设置变量(然后在整个网站中使用这些变量)。我不关心安全问题,因为我是唯一有权访问该表格的人。
因此,目前我使用 fwrite
将数据保存在单独的文件中,然后对每个变量使用 file_get_contents
。表单中的数据很小,每个字段只有一两个字。
我无法使用数据库,因此下面是我目前正在工作的示例,它可以改进还是有其他方法可以实现这一目标?
<?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>
任何建议将不胜感激,谢谢:)
更新
非常感谢大家的帮助和建议。我现在已经实现了类似于下面的东西,我必须说它效果非常好!
<?
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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我猜你这样做是为了持久性(即网络服务器重新启动并且你希望数据在备份后仍然存在)?因此,如果您一直在编写、读取和解析大量文件,那么就很难从执行中节省大量时间。
您可以查看 fscan ,看看这是否会使读取速度更快一些你。
除此之外,我建议您在解析文件后缓存数据,这样您就不必一遍又一遍地执行此操作,并且只需在文件更改后重新扫描。您可以通过校验和或查看文件修改时间戳来跟踪这一点。
“简化”的一种方法是将您发布的数据保存在数组中,然后序列化并将其保存到文件中。然后,您可以一次性读取整个文件并反序列化它,您将获得包含数据的数组。
将数组保存到文件的一种快速而肮脏的方法类似于
和 取回数组
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
您可以使用 php 会话来存储和检索值。
http://www.w3schools.com/php/php_sessions.asp
其次你可以使用SplFileInfo 而不是 fopen 、 fwrite
you can use php sessions to store and retrieve values.
http://www.w3schools.com/php/php_sessions.asp
Secondly you can use SplFileInfo instead of fopen , fwrite
这应该可以解决问题。
但正如上面所说,使用数据库效率要高得多。
This should do the trick.
But as said above, it's much more efficient to use databases.
这只是@Austin Brunkhorst 代码的一点修改。抱歉,评论太多,无法发表。
This is just a little modification of @Austin Brunkhorst's code. Sorry, too much to post in comment.
如果您只想在项目中包含代码,可以使用 eval:
正如所指出的,这使您容易受到任何攻击,因此请确保您真正保护脚本,例如使用.htaccess-文件。
If you just want to include the code in your project, you can use eval:
As pointed out, this leaves you wide open for any attack, so make sure you really secure the script, e.g. with a .htaccess-file.