gpt4 book ai didi

javascript - PHP - 不安全的文件编辑,删除所有数据

转载 作者:行者123 更新时间:2023-11-30 21:11:20 26 4
gpt4 key购买 nike

我有现有的 ini 文件,它仅用于从 Javascript 操作进行编辑。

但是PHP/Javascript会把ini文件中的所有数据都清除掉,只留下一行一行。 (有时它不会删除但是当第 4 节或第 5 节编辑时它会删除整个文件并保留最后一个)

配置文件:

[slide1]
button0=6,298,273,425
[slide2]
button1=1005,313,1269,425
button0=6,298,273,425
[slide3]
button1=1005,313,1269,425
button0=6,298,273,425
[slide4]
button1=1005,313,1269,425
button0=6,298,273,425
[slide5]
button1=1005,313,1269,425
button0=6,298,273,425
button2=1005,313,1269,425
button3=6,298,273,425
[slide6]

[slide7]

PHP:

// EDIT only? (no erase, no delete, no new line add
function config_set($config_file, $section, $key, $value) {
$config_data = parse_ini_file($config_file, true);
$config_data[$section][$key] = $value;
$new_content = '';
foreach ($config_data as $section => $section_content) {
$section_content = array_map(function($value, $key) {
return "$key=$value";
}, array_values($section_content), array_keys($section_content));
$section_content = implode("\n", $section_content);
$new_content .= "[$section]\n$section_content\n";
}
file_put_contents($config_file, $new_content);
}



config_set('config.ini', 'slide5', "button0", "1,2,3,4,5,5,6,7,7");
echo 'updated';

jQuery:

$.get('draw_save.php', {
test: 'none'
}, function(msg) {
console.log(msg);
});

编辑:尝试安全写入

function safefilerewrite($fileName, $dataToSave) {    
if ($fp = fopen($fileName, 'w')) {
$startTime = microtime(TRUE);
do {
$canWrite = flock($fp, LOCK_EX);
// If lock not obtained sleep
// for 0 - 100 milliseconds, to avoid collision and CPU load
if(!$canWrite) usleep(round(rand(0, 100)*1000));
} while ((!$canWrite)and((microtime(TRUE)-$startTime) < 5));

//file was locked so now we can store information
if ($canWrite) {
fwrite($fp, $dataToSave);
flock($fp, LOCK_UN);
}
fclose($fp);
}
}

最佳答案

延迟写入尝试修复了它。

 function save_later() {
// Ajax
$.get('draw_save.php', {

}, function(msg) {
console.log(msg);
});
}



var delay_saving = null;
function delay_save() {
delay_saving = setTimeout(function() {
save_later();
}, 200);
}

关于javascript - PHP - 不安全的文件编辑,删除所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46098395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com