Jump to content

Associative Arrays Edit via PHP file Problem


mssakib

Recommended Posts

Hi

 

Guys i want to edit a file named config.php with another php file named edit.php

 

But when i am trying it via edit.php tho whole code gets changed. Here Is the files. Please help if u can.

 

 

Code of Config.php

 

<?php


$a = ;
$b = ;
$c = ;
$d = ;
$e = ;
$f = ;
$g = ;


$options = array (
  'dhost' => $a,
  'unox' => $b,
  'psd' => $c,
  'age' => $d,
  'vip' => $e,
  'ads' => $f,
  'popup' => $g,
  
  ),
  'forbidden_filetypes_block' => false,
  'rename_these_filetypes_to' => '.xxx',
  'check_these_before_unzipping' => true,
  'no_cache' => true,
  'images_via_php' => false,
  'redir' => true,
  'fgc' => 0,
); 


?>

 

 

 

 

And here is edit.php

 

<?php



if (isset($_POST["Submit"])) {



$string = '<?php  

$a = "'. $_POST["a"]. '" . "\n"; 

$b = "'. $_POST["b"]. '" . "\n"; 

$c = "'. $_POST["c"]. '" . "\n"; 

$d = "'. $_POST["d"]. '" . "\n"; 

$e = "'. $_POST["e"]. '" . "\n"; 

$f = "'. $_POST["f"]. '". "\n"; 

$g = "'. $_POST["g"]. '". "\n"; 

?>';



$fp = fopen("test.php", "w");

fwrite($fp, $string);

fclose($fp);



}



?>

<form action="" method="post" name="install" id="install">

  <p>

    <input name="a" type="text" id="a" value=""> 

    DB Host

</p>

  <p>

    <input name="b" type="text" id="b"> 

    DB Username

</p>

  <p>

    <input name="c" type="password" id="c">

  DB Pass </p>

  <p>

    <input name="d" type="text" id="d">

  DB Name </p>

  <p>

    <input name="e" type="text" id="e">

  DB Prefix</p>

  <p>

    <input name="f" type="text" id="f">

  Userprefix</p>

  <p>

    <input name="g" type="text" id="g">

  DB Type </p>

  <p>

    <input type="submit" name="Submit" value="Install">

  </p>

</form>

 

 

 

But if i run the code the config.php totally changes.

 

Need help guyz

Because your code is overwriting your files contents with

$string = '<?php  

$a = "'. $_POST["a"]. '" . "\n"; 

$b = "'. $_POST["b"]. '" . "\n"; 

$c = "'. $_POST["c"]. '" . "\n"; 

$d = "'. $_POST["d"]. '" . "\n"; 

$e = "'. $_POST["e"]. '" . "\n"; 

$f = "'. $_POST["f"]. '". "\n"; 

$g = "'. $_POST["g"]. '". "\n"; 

?>';

It will not just change these lines of code

$a = ;
$b = ;
$c = ;
$d = ;
$e = ;
$f = ;
$g = ;

 

You first need to get the contents of config.php

$contents = file_get_contents('config.php');

 

Then replace the variable in config.php using

$var_letters = range('a', 'g');
foreach($var_letters as $letter)
{   
    $oldline = '$' . $letter . ' = ;';
    $newline = '$' . $letter . ' = \'' . $_POST[ $letter ] . '\';';

    echo "Replacing <tt>$oldline</tt> with <tt>$newline</tt><br />";

    $contents = str_replace($oldline, $newline, $contents);
}

 

Now save the new contents to config.php

file_put_contents('config.php', $contents);

 

Your variables $a to $g should now be set in config.php with the values you typed within your form.

So that if i just change those codes and the form code should remain same ?

Yes replace your php code with the php code I posted. You do not need to change your form.

 

and another question is if already my $a=1; value given can it edit that ?

No, it will not replace the variables that already have a value. It will only change lines like $a = ; to $a = 'value from form field a';

Hi

 

Thx . But if i need to change a value thats been already added. i mean existing value. what should i do ?

 

Actually i am trying to make a admin panel . the setup page is made. so i need a system to edit existing value, . But i don't to use mysql

 

But if i need to change a value thats been already added. i mean existing value. what should i do ?

Rather than use a simple string replacement

$contents = str_replace($oldline, $newline, $contents);

 

You can do a more complex string replacement using regular expressions. This line should make changes to existing values

$contents = preg_replace('~\$'.$letter.' =.+?;~s', $newline, $contents);

Hi

 

Bro thanks. But now a new problem it doesn't writing the values in config.php . Just makes like this

 

$a=" ";

 

But it should be like this $a="sakib";

 

here is the files again so that u can see it. I tried but can't fix it. i have tried chmodding too.

 

edit.php

 

<?php

$contents = file_get_contents('config.php');
$var_letters = range('a', 'g');
foreach($var_letters as $letter)
{   
    $oldline = '$' . $letter . ' = ;';
    $newline = '$' . $letter . ' = \'' . $_POST[ $letter ] . '\';';

    echo "Replacing <tt>$oldline</tt> with <tt>$newline</tt><br />";

    $contents = str_replace($oldline, $newline, $contents);
}
file_put_contents('config.php', $contents);


?>

<form action="" method="post" name="install" id="install">

  <p>

    <input name="a" type="text" id="a" value=""> 

    DB Host

</p>

  <p>

    <input name="b" type="text" id="b"> 

    DB Username

</p>

  <p>

    <input name="c" type="password" id="c">

  DB Pass </p>

  <p>

    <input name="d" type="text" id="d">

  DB Name </p>

  <p>

    <input name="e" type="text" id="e">

  DB Prefix</p>

  <p>

    <input name="f" type="text" id="f">

  Userprefix</p>

  <p>

    <input name="g" type="text" id="g">

  DB Type </p>

  <p>

    <input type="submit" name="Submit" value="Install">

  </p>

</form> 

 

config.php

<?php


$a = '';
$b = '';
$c = '';
$d = '';
$e = '';
$f = '';
$g = '';


$options = array (
  'dhost' => $a,
  'unox' => $b,
  'psd' => $c,
  'age' => $d,
  'vip' => $e,
  'ads' => $f,
  'popup' => $g,
  
  ),
  'forbidden_filetypes_block' => false,
  'rename_these_filetypes_to' => '.xxx',
  'check_these_before_unzipping' => true,
  'no_cache' => true,
  'images_via_php' => false,
  'redir' => true,
  'fgc' => 0,
); 


?> 

 

 

Try this code I have tested it and it works as expected

<?php

if(isset($_POST['Submit']))
{
    $contents = file_get_contents('config.php');
    $var_letters = range('a', 'g');
    foreach($var_letters as $letter)
    {   
        $oldline = '$' . $letter . ' = ;';
        $newline = '$' . $letter . ' = \'' . $_POST[ $letter ] . '\';';

        echo "Replacing <tt>$oldline</tt> with <tt>$newline</tt><br />";

        $contents = preg_replace('~\$'.$letter.' =.+?;~s', $newline, $contents);
    }
    file_put_contents('config.php', $contents);
}

include 'config.php';

?>

<form action="" method="post" name="install" id="install">

  <p>

    <input name="a" type="text" id="a" value="<?php echo $a; ?>"> 

    DB Host

</p>

  <p>

    <input name="b" type="text" id="b" value="<?php echo $b; ?>"> 

    DB Username

</p>

  <p>

    <input name="c" type="password" id="c" value="<?php echo $c; ?>">

  DB Pass </p>

  <p>

    <input name="d" type="text" id="d" value="<?php echo $d; ?>">

  DB Name </p>

  <p>

    <input name="e" type="text" id="e" value="<?php echo $e; ?>">

  DB Prefix</p>

  <p>

    <input name="f" type="text" id="f" value="<?php echo $f; ?>">

  Userprefix</p>

  <p>

    <input name="g" type="text" id="g" value="<?php echo $g; ?>">

  DB Type </p>

  <p>

    <input type="submit" name="Submit" value="Install">

  </p>

</form>

if(isset($_POST['Submit']))  // If the form has been submitted then do this stuff
{
    $contents = file_get_contents('config.php');  // Store the contents of config.php in the $contents variable
    $var_letters = range('a', 'g');  // Create an array and store it to $var_letters variable. Same as array('a', 'b', 'c', 'd', 'e', 'f', 'g')
    foreach($var_letters as $letter)  // Go through each letter and perform this code
    {   
        $oldline = '$' . $letter . ' = ;';  // The string that you are searching for ($a = ; or $b = ; or $c = ; etc.)
        $newline = '$' . $letter . ' = \'' . $_POST[ $letter ] . '\';';  // The string you are going to replace it with ( $a = $_POST['a']; ) 

        echo "Replacing <tt>$oldline</tt> with <tt>$newline</tt><br />";  // Print out a status message

        $contents = preg_replace('~\$'.$letter.' =.+?;~s', $newline, $contents); // Replace contents (from config.php) with the new line based on the regular expression
    }
    file_put_contents('config.php', $contents);  // Write the new contents back to config.php
}

include 'config.php';  // Include the config file

 

 

Hope that helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.