Jump to content

Archived

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

Ben Phelps

edit config.php

Recommended Posts

I am wondering how i can open a files (config.php) and change some values via a text box then save.

Example

config.php
[code]
<?php

$site_url= "http://127.0.0.1/";

?>
[/code]

I want to open config.php and change http://127.0.0.1/ to what ever is imputed in the text box.

It will be an install script when its finished

Share this post


Link to post
Share on other sites
well you could make two php pages like so:

[b]index.php[/b]
[code]
.....
<body>
<form action="config.php" method="post">
Change Ip to: <input type="text" name="ip">
<input type="submit" name="submit" value="Submit New Ip">
</form>
....
</body>
[/code]



[b]
config.php[/b]
[code]
<?
if(isset($_POST['submit'])) {
$ipfield = $_POST['ip'];
echo "Current Ip is:" . $ipfield . ".";
}
?>


[/code]

Share this post


Link to post
Share on other sites
that is close but i want it to physically edit the file.

so before edit
[code]
<?php

$site_url= "http://127.0.0.1/";

?>[/code]

after edit

[code]
<?php

$site_url= "http://192.168.0.100/";

?>[/code]

it is a config file for a script I'm working on

Share this post


Link to post
Share on other sites
That script would physically edit the file, it takes the ip from a input box and changes the variable on the config page.  I may not be be completely understanding of what you need but I think from what you just posted it should work for your needs.

Share this post


Link to post
Share on other sites
i need it to open the file change http://127.0.0.1/ to what ever the user inputs and save the file for later use, not just change the var. when the script is being used

Share this post


Link to post
Share on other sites
i have a script that writes the ip to the config.php but it writes to the end of the document.  I need it to write in a certain place.

[code]<?php

$script_url = "it needs to write here" ;

?>not here[/code]

Here are the scripts i use now.

get.php

[code]<html>
<head>
<title></title>
</head
<body>
<form action="write.php" method="post">
  <p align="center"><strong>Script Installer </strong><br>
    Where is the script at :<br>
    <input type="text" name="ipfield">
    <br>
    <input type="submit" name="submit" value="Install">
    <br>
    Ex. <strong>http://www.thepchs.com/script/</strong></p>
</form>
</body>
<html>[/code]

write.php

[code]<?php
$filename = 'config.php';
$somecontent = "$ipfield";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

  // In our example we're opening $filename in append mode.
  // The file pointer is at the bottom of the file hence
  // that's where $somecontent will go when we fwrite() it.
  if (!$handle = fopen($filename, 'a')) {
        echo "Cannot open file ($filename)";
        exit;
  }

  // Write $somecontent to our opened file.
  if (fwrite($handle, $somecontent === TRUE) {
      echo "Cannot write to file ($filename)";
      exit;
  }
 
  echo "Success, wrote $somecontent to file $filename";
 
  fclose($handle);

} else {
  echo "The file $filename is not writable";
}
?> [/code]

config.php

[code]<?php

script_url = "";

?>[/code]

Share this post


Link to post
Share on other sites
You could use this script (untested).
[code]
<?php
if(isset($_POST['newip'])) {
$newip = $_POST['newip'];
$oldtext = file_get_contents('config.php');
$newtext = preg_replace('/http:\/\/(.*)\//', "http://".$newip."/", $oldtext);
$fp = fopen("config.php", "w");
fwrite($fp, $newtext);
fclose($fp);
} else {
?>
<form method="post" action="<?php echo $_SERVER['SCRIPT_URI'] ?>">
IP: <input type="text" name="newip" /><br>
<input type="submit" value="submit" />
</form>
<?php } ?>[/code]

Share this post


Link to post
Share on other sites
THANK YOU VERY MUCH  ;D ;D ;D ;D ;D ;D ;D ;D ;D ;D
It works  ;D ;D ;D ;D ;D ;D ;D ;D ;D ;D

Now is there a way i could do that like 3 more times editing something different every time?  if not thats ok

Share this post


Link to post
Share on other sites
update.php
[code]
<?php

if(isset($_POST['newip'])) {
$newip = $_POST['newip'];
$oldtext = file_get_contents('config.php');
$newtext = preg_replace('/http:\/\/(.*)\//', "http://".$newip."/", $oldtext);
$fp = fopen("config.php", "w");
fwrite($fp, $newtext);
fclose($fp);
header("location: thankyou.php");
}
?>
[/code]

form.php

[code]
<html>
<body>
<form method="post" action="update.php">
IP: <input type="text" name="newip" /><br>
<input type="submit" value="submit" />
</form>
</body>
</html>
[/code]

thankyou.php

[code]
<html>
<body>
thank you
</body>
</html>
[/code]



or easer
[code]
<?php
if(isset($_POST['newip'])) {
$newip = $_POST['newip'];
$oldtext = file_get_contents('config.php');
$newtext = preg_replace('/http:\/\/(.*)\//', "http://".$newip."/", $oldtext);
$fp = fopen("config.php", "w");
fwrite($fp, $newtext);
fclose($fp);
echo "thank you";
exit;
} else {
?>
<form method="post" action="<?php echo $_SERVER['SCRIPT_URI'] ?>">
IP: <input type="text" name="newip" /><br>
<input type="submit" value="submit" />
</form>
<?php } ?>
[/code]

Share this post


Link to post
Share on other sites
Or if you want them to press a 'confirm' button:
[code]<?php
if(isset($_POST['newip'])) {
  if(isset($_POST['confirm'])) {
    $newip = $_POST['newip'];
    $oldtext = file_get_contents('config.php');
    $newtext = preg_replace('/http:\/\/(.*)\//', "http://".$newip."/", $oldtext);
    $fp = fopen("config.php", "w");
    fwrite($fp, $newtext);
    fclose($fp);
  } else {
    echo "Are you sure about that? <form method='post' action='$_SERVER[SCRIPT_URI]'><input type='hidden' name='newip' value='$_POST[newip]' /><input type='submit' name='confirm' value='confirm' /></form>";
  }
} else {
?>
<form method="post" action="<?php echo $_SERVER['SCRIPT_URI'] ?>">
IP: <input type="text" name="newip" /><br>
<input type="submit" value="submit" />
</form>
<?php } ?>[/code]

Share this post


Link to post
Share on other sites

×

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.