简体   繁体   中英

update value in config.php with another php script

I include a config.php file in all of my pages. I also have a cron job which updates a total count of a table, into another table each hour. I want to also write that value into the config and save if possible.

my config is in the structure:

return array(
    'stats' => array(
        'total_count' => 130000,
        'another_stat' => 100000,
    ),
    'other_config_value' => 'value',
)

and I include it in my pages, at the top like:

$config = include('../php/config.php');
$total_count = $config['stats']['total_count']

just so I don't have to query the database on each page, every time it loads as I want to show that total on all pages.

You can keep your config in JSON format and use functions json_decode() to read it. If yout want update it you can use json_decode() to make an array from JSON string, update this value in array and use json_encode() to make JSON string from array. After that you can save it to file with file_put_contents().

$config = json_decode(file_get_contents('config.json'));
$config['value'] = $newValue;
file_put_contents('config.json', json_encode($config));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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