Save value of a constant through a form

Asked

Viewed 68 times

0

I have a config.php file with several defines through a form.

If there is such a definition:

define("NAME","Rodrigo");

and another like:

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 saves this name in both defines, even though I have no other input with the ID and NAME equal to the define I want to save.

This is my job:

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 fix this?

  • ele grava esse nome nos dois defines - What do you mean Record in Constants??? Modify this question, friend.

  • 1

    It’s really not making it very clear, but it’s not changing the constant itself, but physically the file that generates the constant.

1 answer

1

First point:

  • In your form the field name cannot be MSSQL_Pass if you want to receive the 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>";
    }
    
  • i forgot to change the name, I just posted an example but the original function this right

  • Got it, ideal is you post the original code, either the answer helped or the problem still persists?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.