Form field save output to php line?

Hi, I am new to php…

Is there a way I can achieve this?

I have a form field with Username and Password.

I want to save the output in 2 lines of config.php…

The line contains:

// if true, activate admin authentication
$config[‘admin_actived’] = true;
// admin username
$config[‘admin_username’] = “admin”;<< it will save here
// admin password
$config[‘admin_password’] = “password”; << it will save here

Is this possible?

Thanks in advance

Yes it is possible have a search I am sure i answered this in the last week or 2.

Hi thanks:

is this the one ? http://www.phphelp.com/forum/index.php?topic=17563.msg61129#msg61129

it was something about

how to make a install script like

Or this one does a simular thing

http://www.phphelp.com/forum/index.php?topic=17575.msg59251#msg59251

Thanks noodle I am currently looking on it.

Ok I’ll post a simple form:

[php]

Username:

Password:

Save

[/php]

am I doing this correct?

This is part of config.php
[php]<?php

$config = Array();

// if true, activate admin authentication
$config[‘admin_actived’] = true;
// admin username
$config[‘admin_username’] = “admin”;
// admin password
$config[‘admin_password’] = “password”;
// if true, allow upload only for admin
$config[‘protect_upload’] = true;
// if true, show upload feature only for admin
$config[‘hide_upload’] = true;
// if true, allow delete only for admin
$config[‘protect_delete’] = true;
// if true, show delete feature only for admin
$config[‘hide_delete’] = true;
// if true, allow make dir only for admin
$config[‘protect_makedir’] = true;
// if true, show make dir feature only for admin
$config[‘hide_makedir’] = true;[/php]

or do I need a processor for this?

That is not correct.

You need to name the form inputs with a static name and the value is what will be posted.
You are posting to the page you want to edit which will do nothing.

Read the orange lines this tells you what it is doing.
Also you are trying to change something that is used many times admin this will change everything that is called admin if you put that as the word to change.

You need to make it unique to the rest of the code . that why they use things like %username%
on install scripts so only that word with % around them is found.

what you can do though is make one your self and do a restring replace on the name after.
The code below will do what you want it to do put the 2 codes in the 2 paqges and test it out form.php and config.php.

config.php
[php]<?php
$postedname = “%admin%”;
$admin_name = str_replace(’%’, ‘’, $postedname);
$config[‘admin_username’] = $admin_name;
?>[/php]

form.php
[php]<?php
// if nothing has been posted print to screen
if(!isset($_POST[‘submit’])&&(!isset($_POST[‘findmail’]))&&(!isset($_POST[‘newmail’]))){
?>

FIND
Replace
<?php } ?> <?php //if form has been posted && and filled out if(isset($_POST['submit'])&&(isset($_POST['findmail']))&&(isset($_POST['newmail']))){ // post the data to be replaced $old = '%'.$_POST['findmail'].'%'; $new = '%'.$_POST['newmail'].'%'; // Get a file to change $data = 'config.php'; if(file_exists($data)){ // change the content with the posted content $content = file_get_contents($data); file_put_contents($data, str_replace($old, $new, $content)); echo 'file has been modified'; }else{ echo 'Error no file to wirte to';} }else{ echo 'Error POST not set';} ?>[/php]

Can I use that code in able to change 2 lines on config.php?

You mean I need to change the lines which will be replace?

I tried to change the config arrays but when I login using the form nothing happens…

That is just a working example do if you put them on your server in a folder called example then go to your url/example form.php

you will get the form that will change the code in the conpfig.php the config might have to be chmod to 755 to be able to change it as this would need to write to the file example/config.php.

This will not change your config you have now unless you add the config.php code to yours to be able to change the admin name.

hhmm… can you please check I am doing this is correct?

form.php
[php]<?php
// if nothing has been posted print to screen
if(!isset($_POST[‘submit’])&&(!isset($_POST[‘username’]))&&(!isset($_POST[‘password’]))){
?>

USERNAME
PASSWORD
<?php } ?> <?php //if form has been posted && and filled out if(isset($_POST['submit'])&&(isset($_POST['username']))&&(isset($_POST['password']))){ // post the data to be replaced $config['admin_username'] = $_POST['username']; $config['admin_password'] = $_POST['password']; // Get a file to change $data = 'config2.php'; if(file_exists($data)){ // change the content with the posted content $content = file_get_contents($data); file_put_contents($data, str_replace($config['admin_username'], $config['admin_password'], $content)); echo 'file has been modified'; }else{ echo 'Error no file to wirte to';} }else{ echo 'Error POST not set';} ?>[/php]

I renamed config.php as config2.php
[php]<?php
$postedname = “%admin%”;
$admin_name = str_replace(’%’, ‘’, $postedname);
$config[‘admin_username’] = $admin_name;
?>[/php]

Why there is no need to rename anything if you put them in there own empty directory.

You will need something like this for it to work on your config.

[php]form.php
[php]<?php
// if nothing has been posted print to screen
if(!isset($_POST[‘submit’])&&(!isset($_POST[‘oldusername’]))&&(!isset($_POST[‘newusername’])) && (!isset($_POST[‘oldpassword’])) && (!isset($_POST[‘newpassword’]) ) ){
?>

FIND
Replace

FIND

Replace

<?php } ?> <?php //if form has been posted && and filled out if(isset($_POST['submit']) && (isset($_POST['oldusername'])) && (isset($_POST['newusername']) ) && (isset($_POST['oldpassword'])) && (isset($_POST['newpassword']) ) ){ // post the data to be replaced $old = "$config['admin_username'] = '$_POST['oldusername']';"; $new = "$config['admin_username'] = '$_POST['newusername']';"; $oldpass = "$config['admin_password'] = '$_POST['oldpassword']';"; $newpass = "$config['admin_password'] = '$_POST['newpassword']';"; // Get a file to change $data = 'config.php'; if(file_exists($data)){ // change the content with the posted content $content = file_get_contents($data); file_put_contents( $data, str_replace($old, $new, $content) ); file_put_contents( $data, str_replace($oldpass, $newpass, $content) ); echo 'file has been modified'; }else{ echo 'Error no file to wirte to';} }else{ echo 'Error POST not set';} ?>[/php]

I get white page loading the code :frowning:

[php]<?php
/* if nothing has been posted print to screen */
if(!isset($_POST[‘submit’])&&(!isset($_POST[‘oldusername’]))&&(!isset($_POST[‘newusername’])) && (!isset($_POST[‘oldpassword’])) && (!isset($_POST[‘newpassword’]) ) ){
?>

FIND
Replace
FIND
Replace
<?php } ?> <?php /* if form has been posted && and filled out */ if(isset($_POST['submit']) && (isset($_POST['oldusername'])) && (isset($_POST['newusername']) ) && (isset($_POST['oldpassword'])) && (isset($_POST['newpassword']) ) ){ // post the data to be replaced $old = '$config[\'admin_username\'] = "$_POST[\'oldusername\']"; '; $new = '$config[\'admin_username\'] = "$_POST[\'newusername\']"; ';

$oldpass = ‘$config[‘admin_password’] = “$_POST[‘oldpassword’]”;’;
$newpass = ‘$config[‘admin_password’] = “$_POST[‘newpassword’]”;’;
/* // Get a file to change$data = ‘config.php’; /
if(file_exists($data)){
/
// change the content with the posted content */
$content = file_get_contents($data);
file_put_contents( $data, str_replace($old, $new, $content) );
file_put_contents( $data, str_replace($oldpass, $newpass, $content) );
echo ‘file has been modified’;
}else{
echo ‘Error no file to wirte to’;
}
}else{
echo ‘Error POST not set’;
}
?>[/php]

[php]<?php
$config[‘admin_username’] = “admin”;
$config[‘admin_password’] = “password”;
?>[/php]

[php]<?php
/* if nothing has been posted print to screen */
if(!isset($_POST[‘submit’])&&(!isset($_POST[‘oldusername’]))&&(!isset($_POST[‘newusername’])) && (!isset($_POST[‘oldpassword’])) && (!isset($_POST[‘newpassword’]) ) ){
?>

FIND
Replace
FIND
Replace
<?php } ?> <?php /* if form has been posted && and filled out */ if(isset($_POST['submit']) && (isset($_POST['oldusername'])) && (isset($_POST['newusername']) ) && (isset($_POST['oldpassword'])) && (isset($_POST['newpassword']) ) ){ // post the data to be replaced $old = '$config[\'admin_username\'] = "$_POST[\'oldusername\']"; '; $new = '$config[\'admin_username\'] = "$_POST[\'newusername\']"; ';

$oldpass = ‘$config[‘admin_password’] = “$_POST[‘oldpassword’]”;’;
$newpass = ‘$config[‘admin_password’] = “$_POST[‘newpassword’]”;’;
/* // Get a file to change$data = ‘config.php’; /
$data = ‘config.php’;
if(file_exists($data)){
/
// change the content with the posted content */
$content = file_get_contents($data);
file_put_contents( $data, str_replace($old, $new, $content) );
file_put_contents( $data, str_replace($oldpass, $newpass, $content) );
echo ‘file has been modified’;
}else{
echo ‘Error no file to wirte to’;
}
}else{
echo ‘Error POST not set’;
}
?>[/php]
I added $data = ‘config.php’; and it says “file has been modified”

but w/o $data = ‘config.php’; I get “Error no file to wirte to”

sorry code post was bare and not on bbcode.

I added $data = ‘config.php’; and it says “file has been modified” = but no files were change

and w/o $data = ‘config.php’; I get “Error no file to wirte to”

Try this there was white space in the code.
These are examples they are not 100% perfect its to give you an idea how to make it work with your form i am not here to make code for you.
There are place for that on the net that you pay for.

[php]<?php
$data = ‘config.php’;
/* if nothing has been posted print to screen */
if(!isset($_POST[‘submit’])&&(!isset($_POST[‘oldusername’]))&&(!isset($_POST[‘newusername’])) && (!isset($_POST[‘oldpassword’])) && (!isset($_POST[‘newpassword’]) ) ){
?>

FIND
Replace
FIND
Replace
<?php } ?> <?php /* if form has been posted && and filled out */ if(isset($_POST['submit']) && (isset($_POST['oldusername'])) && (isset($_POST['newusername']) ) && (isset($_POST['oldpassword'])) && (isset($_POST['newpassword']) ) ){ // post the data to be replaced $old = '$config[\'admin_username\'] = "'.$_POST['oldusername'].'";'; $new = '$config[\'admin_username\'] = "'.$_POST['newusername'].'";';

$oldpass = ‘$config[‘admin_password’] = "’.$_POST[‘oldpassword’].’";’;
$newpass = ‘$config[‘admin_password’] = "’.$_POST[‘newpassword’].’";’;

/* // Get a file to change$data = ‘config.php’; /
if(file_exists($data)){
/
// change the content with the posted content */

if( $content = file_get_contents($data)){
}
file_put_contents( $data, str_replace(trim($oldpass), trim($newpass), $content));
echo ‘file has been modified’;

if( $content = file_get_contents($data)){

  file_put_contents( $data, str_replace(trim($old), trim($new), $content));

}

 }else{
    echo 'Error no file to wirte to';
    }
    }else{
      echo 'Error POST not set';
      }

?>[/php]

config.php
[php]<?php
$config[‘admin_username’] = “admin”;
$config[‘admin_password’] = “password”;
?>[/php]

alright noodles… trying to figure out your code and search on for more. Thanks for those info, this helps and gives me ideas on form.

Thanks again.

Sponsor our Newsletter | Privacy Policy | Terms of Service