Write value of a constant through a form [closed]

0

I have a config.php file with several define via a form.

If there is one define it like this:

define("NAME","Rodrigo");

and another like this:

define("MEU_NOME","Rodrigo");

And in my HTML I have only ONE input:

<?php include(config.php) ?>
<input type="text" id="NAME" value="<?=NAME?>" name="MSSQL_Pass">

If the input sends the name "Danilo", it records that name in both sets, even though I do not have another input with the ID and NAME equal to the one I want to record.

This is my role:

function write(){
$file = fopen("config.php","rb");
    $MainContents = "";
    while(!feof($file)) $MainContents .= fgets($file);

    fseek($file, 0);

    while(!feof($file))
    {
        $Main = fscanf($file,'%[^;]');
        if(strpos($Main[0],"//") === false && strpos($Main[0],"?") === false && isset($Main[0]))
        {
            $data = explode("\",",$Main[0]);
            $const = str_replace("define(\"", "", $data[0]);
            $value = str_replace(")", "", $data[1]);

            $myVar = trim($const);

            if(!empty($_POST[$myVar]))
            {
                $new = ($_POST[$myVar] == "true" || $_POST[$myVar] == "false") ? $_POST[$myVar] : "\"" . $_POST[$myVar] . "\"";
                $MainContents = str_replace($value, " " . $new ,$MainContents);
            }
        }
    }

    fclose($file);

    $file = fopen("config.php","wb");
    fwrite($file, $MainContents);
    fclose($file);

    echo " <div class=\"success-box\">Configura&ccedil;&otilde;es salvas com sucesso!</div>";

}

How can I resolve this?

    
asked by anonymous 13.10.2016 / 23:46

1 answer

1

First point:

  • In your form the field name can not be MSSQL_Pass if you want to receive value name ;

Second point:

  • You need to use hardcode if you want to use this rule

    function write(){
    $file = fopen("config.php","rb");
    $MainContents = "";
    while(!feof($file)) $MainContents .= fgets($file);
        fseek($file, 0);
    
    while(!feof($file))
    {
    
        $Main = fscanf($file,'%[^;]');
        if(strpos($Main[0],"//") === false && strpos($Main[0],"?") === false && isset($Main[0]))
        {
            $data = explode("\",",$Main[0]);
            $const = str_replace("define(\"", "", $data[0]);
            $value = str_replace(")", "", $data[1]);
    
            $myVar = trim($const);
    
            $myVar = ($myVar == "MEU_NOME" && !isset($_POST[$myVar]) ? "NAME" : $myVar;
    
            if(!empty($_POST[$myVar]))
            {
                $new = ($_POST[$myVar] == "true" || $_POST[$myVar] == "false") ? $_POST[$myVar] : "\"" . $_POST[$myVar] . "\"";
                $MainContents = str_replace($value, " " . $new ,$MainContents);
            }
        }
    }
    fclose($file);
    $file = fopen("config.php","wb");
    fwrite($file, $MainContents);
    fclose($file);
    
    echo " <div class=\"success-box\">Configura&ccedil;&otilde;es salvas com sucesso!</div>";
    }
    
14.10.2016 / 14:21