0

I am trying to modify the value of a variable $denumire produs=' '; from a php file _inc.config.php through a script by this code with form from file index.php and i have some errors. The new value of the variable will become value entered from the keyboard via the form. Anyone can help me, please?

<?php 

if (isset($_POST['modify'])) {
    $str_continut_post = $_POST['modify'];

    if (strlen($_POST['search']) > 0 || 1==1) {
        $fisier = "ask003/inc/_inc.config.php";
        $fisier = fopen($fisier,"w") or die("Unable to open file!");

        while(! feof($fisier)) {
            $contents = file_get_contents($fisier);
            $contents = str_replace("$denumire_produs =' ';", "$denumire_produs ='$str_continut_post';", $contents);
            file_put_contents($fisier, $contents);

            echo $contents;
        }
        fclose($fisier);
        die("tests");
    }
}
?>

 <form  method="POST" action="index.php"  >  
    <label>Modifica denumire baza de date: </label>  
    <input  type="text" name="den"> 
    <button type="submit" name="modify"> <center>Modifica</center></button>

     </div></div>
  </form> 
delboy1978uk
  • 12,118
  • 2
  • 21
  • 39

1 Answers1

1

This is an XY problem (http://xyproblem.info/).

Instead of having some sort of system that starts rewriting its own files, why not have the file with the variable you want to change load a json config file?

{
    "name": "Bob",
    "job": "Tea Boy"
}

Then in the script:

$json = file_get_contents('/path/to/config.json');
$config = json_decode($json, true);
$name = $config['name'];

Changing the values in the config is as simple as encoding an array and putting the json into the file.

$config['denumireProdu'] = 'something';
$json = json_encode($config);
file_put_contents('/path/to/config.json', $json);

This is far saner than getting PHP to rewrite itself!

Docs for those commands:

http://php.net/manual/en/function.json-decode.php

http://php.net/manual/en/function.json-encode.php

http://php.net/manual/en/function.file-get-contents.php

http://php.net/manual/en/function.file-put-contents.php

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
  • 1
    `var_export` also works well in situations like this, to create a file containing actually parse-able PHP code … can save the hassle of having to decode JSON manually, you can still simply include such a config file anywhere without additional data parsing steps required. – misorude Aug 31 '18 at 10:23
  • Interesting, why not write an example and stick it in an answer? – delboy1978uk Aug 31 '18 at 10:29
  • 1
    Basically that answer exists already, https://stackoverflow.com/a/2015811/10283047 ;-) – misorude Aug 31 '18 at 10:32