Jump to content

edit config.php


Ben Phelps

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
Link to comment
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]
Link to comment
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]
Link to comment
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]
Link to comment
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]
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.