繁体   English   中英

可以改进此PHP代码吗?

[英]Can this PHP code be improved?

我要实现的基本上是从通过表单输入的数据中设置变量(然后这些变量将在我的整个网站中使用)。 我不必担心安全性,因为我是唯一可以访问该表格的人。

因此,目前我正在使用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>

我想您正在这样做是为了持久性(即Web服务器重新启动,并且您希望数据一旦备份就可以保存在那里)? 因此,如果您坚持编写,读取和解析大量文件,则很难从执行中节省大量时间。

您可以查看fscan ,看看是否可以使读取速度更快。

除此之外,我建议您在解析文件后就缓存数据,这样就不必一遍又一遍地处理,而仅在文件更改后才重新扫描。 您可以通过校验和或查看文件修改后的时间戳来跟踪此情况。

一种“简化”的方法是将发布的数据保存在一个数组中 ,并对其进行序列化并将其保存到文件中。 然后,您可以一次性读取整个文件并将其反序列化 ,然后将阵列与数据放回去。

一种将数组保存到文件的快捷方法是

file_put_contents('mydatafile', serialize($myArray)));

并取回阵列

$myArray = unserialize(file_get_contents('mydatafile'));

您可以使用php会话来存储和检索值。 http://www.w3schools.com/php/php_sessions.asp

其次,您可以使用SplFileInfo代替fopen,fwrite

$myFile = 'foo.txt';
$file = new SplFileInfo($myFile);
$lines = $file->openFile('w');

这应该可以解决问题。

<?php

if( isset($_REQUEST['sub']) ){

$files = array(
    'first.php'  => $_REQUEST['first'],
    'second.php' => $_REQUEST['second'],
    'third.php'  => $_REQUEST['third']
);  

foreach($files as $file => $request){
    $fh = fopen($file,'w') or die("can't open file ". $file);
    $string = preg_replace("/[^a-zA-Z0-9.:,=?%\/\\s]/", "", $request);
    fwrite($fh, $string);
    fclose($fh);
}

}

$first = file_get_contents ("first.php");
$second = file_get_contents ("second.php");
$third = file_get_contents ("third.php");

?>

但是如上所述,使用数据库效率更高。

<?php

function do_file_operation($filename,$field_name)
{
    $myFile = $filename;
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = $_POST[$field_name];
    $string = preg_replace("/[^a-zA-Z0-9.:,=?%\/\\s]/", "", $stringData);
    fwrite($fh, $string);
    fclose($fh);
}


if(isset($_POST['sub']))
{

    do_file_operation("first.php","first");
    do_file_operation("second.php","second");
    do_file_operation("third.php","third");

}

$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'])) {
    # MAKE SURE YOUR FORM IS WELL PROTECTED !
    file_put_contents("first.php",  preg_replace("/[^a-zA-Z0-9.:,=?%\/\\s]/", "", $_REQUEST['first']));
    file_put_contents("second.php", preg_replace("/[^a-zA-Z0-9.:,=?%\/\\s]/", "", $_REQUEST['second']));
    file_put_contents("third.php", preg_replace("/[^a-zA-Z0-9.:,=?%\/\\s]/", "", $_REQUEST['third']));
}    
?>
<form method="post" name="installer">
<div id="field">
<label>First</label>
<input type="text" name="first" value="<?php echo file_get_contents("first.php"); ?>" />
</div>
<div id="field">
<label>Second</label>
<input type="text" name="second" value="<?php echo file_get_contents("second.php"); ?>" />
</div>
<div id="field">
<label>Third</label>
<input type="text" name="third" value="<?php echo file_get_contents("third.php"); ?>" />
</div>
<div id="submit">
<input type="submit" value="Save" name="sub" />
</div>
</form>

这只是@Austin Brunkhorst的代码的一点修改。 抱歉,太多评论了。

if (isset($_REQUEST['sub'])) {
    //No need to copy variables
    $files_data = array(
        'first' => &$_REQUEST['first'],
        'second' => &$_REQUEST['second'],
        'third' => &$_REQUEST['third']
    );
    //Note that you should use '+' at the end of match
    //so that preg can match more the one character at once,
    //it is much faster. And we can apply it once to whole array.
    $files_data = preg_replace("/[^a-zA-Z0-9.:,=?%\/\\s]+/", "", $files_data);
    //Note '&' in foreach to disable variable copying
    foreach ($files_data as $filename => &$request_str) {
        //Why use complex fwrite when we have a ready function to write to file?
        file_put_contents($filename . '.php', $request_str) !== FALSE or die("Can't write to file '$filename'!" . PHP_EOL);
        //We don't need to read files again when we already have our string
        //It is ugly, but fun! Maby this should be avoided :)
        $$filename = $request_str;
    }
} else {
    $first = file_get_contents("first.php");
    $second = file_get_contents("second.php");
    $third = file_get_contents("third.php");
}

如果只想在项目中包含代码,则可以使用eval

$includes = array('first', 'second', 'third');
foreach ($includes as $value) {
    $code = preg_replace("/[^a-zA-Z0-9.:,=?%\/\\s]/", "", $_REQUEST[$value]);
    if (!eval($code))
        die('Parse Error in $_REQUEST["'.$value.'"]');
}

如前所述,这使您不受任何攻击的可能,因此请确保您确实保护了脚本,例如使用.htaccess-file

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM