-2

I have a file which is a small place_config.php file.

Take this as an example where i am setting my variables

<?php

//config file
$place_config = array(
    'credentials' => array(
        'sid' => 'some_value',
        'token' => 'some_token'
    )
?>

I want to change the sid and token from the admin panel of the user for the ease. How can i effectively achieve this. One solution which i understand is to make the content of the file in a string with the variables of $_REQUEST placed after the post request write that whole string to the file? Is it a effective approach?

tech_geek
  • 1,624
  • 3
  • 21
  • 44
  • FWIW: http://php.net/manual/en/function.parse-ini-file.php You are describing basic CRUD. Yes, POST is likely how you would accomplish this with HTTP. – ficuscr Jul 18 '18 at 16:55
  • @ficuscr think ini file will not be suitable in my case because the system is working on .php file. or i should go with the post method? – tech_geek Jul 18 '18 at 17:00
  • Maybe read the link first? It is a native PHP function I am suggesting. Goole for a tutorial where someone is using that method and maybe shows an example of implementing a user interface to edit the values. As is your question it too broad. – ficuscr Jul 18 '18 at 17:03
  • I have read but isnt that function only work with .ini file? – tech_geek Jul 18 '18 at 17:05
  • Don't get hung up on that. It's just a file extension. Most people use a database for this stuff. You want to use a flat file what I am suggesting is a good approach. [Read](https://stackoverflow.com/questions/5695145/how-to-read-and-write-to-an-ini-file-with-php). And [more here](https://stackoverflow.com/questions/14752470/creating-a-config-file-in-php) – ficuscr Jul 18 '18 at 17:06
  • `include` the file, modify `$place_config` then `var_export` and `file_put_contents`. – AbraCadaver Jul 18 '18 at 17:08
  • @ficuscr let me try this – tech_geek Jul 18 '18 at 17:14
  • @AbraCadaver this will put the variables in exact position as the previous file ? – tech_geek Jul 18 '18 at 17:15

2 Answers2

2

Submit a form with the proper inputs and when submitted call update_place_config():

function update_place_config() {
    include('place_config.php');
    $place_config['credentials']['sid'] = $_POST['sid'];
    $place_config['credentials']['token'] = $_POST['token'];
    $output = '<?php $place_config = ' . var_export($place_config, true) . '; ?>';
    file_put_contents('place_config.php', $output);
    return $place_config; //if you want to get the new config
}

Another option:

$content = file_get_contents('place_config.php');
$content = preg_replace("/('sid' =>) '[^']+'/", "$1 '{$_POST['sid']}'", $content);
file_put_contents('place_config.php', $content);

I personally would store in a database or use JSON if it needs to be a file.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

Instead of storing the configuration data in a php file, I'll recommend storing them in a json file which can be easily read/edited through php.

Create a json file, let's say config.json. Then you can load the configuration using $conf = json_decode(file_get_contents("config.json")). You can make changes to the $conf object and save back the configurations as file_put_contents("config.json", json_encode($conf)).

Yousuf Khan
  • 334
  • 3
  • 12