Jump to content

Edit PHP with PHP?


doomdude

Recommended Posts

Hey guys, not sure how this is possible. But I'm looking to edit contents of a .php file with a form from another .php file.

 

Just wondering how this is possible? I believe its possible since sites like wordpress edit the config.php file while installing.

 

I've looked for some examples but cannot seem to find any, does anyone have any links to an example or an example of there work editing a .php file from a .php form.

 

Thanks

Link to comment
Share on other sites

you may want to try this.

 

echo "<form action='edit.php' method='POST'><textarea name='editpage'>";
$filename = "index.php"; // replace to actual file name
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
echo $contents;
fclose($handle);
echo "</textarea><input type='submit' value='Submit'/></form>";

 

that will place everything in the file into a textarea which is in a form the will pass the data to edit.php

Link to comment
Share on other sites

you may want to try this.

 

echo "<form action='edit.php' method='POST'><textarea name='editpage'>";
$filename = "index.php"; // replace to actual file name
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
echo $contents;
fclose($handle);
echo "</textarea><input type='submit' value='Submit'/></form>";

 

that will place everything in the file into a textarea which is in a form the will pass the data to edit.php

 

I tried the code you said but it just prints the code of that page in the text box?

 

wjo1313336257i.PNG

 

 

Link to comment
Share on other sites

Yeh, (well editer.php).

 

Editer.php:

<?php
echo "<form action='editer.php' method='POST'><textarea name='editpage' rows='20' cols='50'>";
$filename = "includes/config.php"; // replace to actual file name
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
echo $contents;
fclose($handle);
echo "</textarea><input type='submit' value='Submit'/></form>";
?>

 

Displays:

epl1313336923h.PNG

 

but submit just refreshes the page.

Link to comment
Share on other sites

 

this is how you are gonna have it done. use this exact code.



// check if user edited the page.

if(isset($_REQUEST['editfile']))
{


$editfile = $_REQUEST['editfile'];
$filename = "r.php";
$handle = fopen($filename, "w+");
fwrite($handle, $editfile);
fclose($handle);
echo "Done Editing!";
}


// if not show the form
else {
$self = $_SERVER['PHP_SELF'];
echo "<form action='" .$self. "' method='post'/><textarea name='editfile' rows='20' cols='50'>";
$filename = "includes/config.php";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
echo $contents;
fclose($handle);
echo "</textarea><input type='submit' value='Submit'/></form>";
}

Link to comment
Share on other sites

opps. sorry. i forget to change the save point to includes/config.php

 



// check if user edited the page.

if(isset($_REQUEST['editfile']))
{


$editfile = $_REQUEST['editfile'];
$filename = "includes/config.php.php";
$handle = fopen($filename, "w+");
fwrite($handle, $editfile);
fclose($handle);
echo "Done Editing!";
}


// if not show the form
else {
$self = $_SERVER['PHP_SELF'];
echo "<form action='" .$self. "' method='post'/><textarea name='editfile' rows='20' cols='50'>";
$filename = "includes/config.php";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
echo $contents;
fclose($handle);
echo "</textarea><input type='submit' value='Submit'/></form>";
}

 

it should work now :)

Link to comment
Share on other sites

Yeh if possible how would I hide:

 

 $connection = mysql_connect($dbhost, $dbuser, $dbpass) 
or die ("Could not connect to server ... \n" . mysql_error ());
mysql_select_db($dbdata) 
or die ("Could not connect to database ... \n" . mysql_error ());

 

But show:

 

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbdata = 'accounts';

 

But still save the whole lot in the config.php file lol (I know I'm a pain)

Link to comment
Share on other sites

you may want to have the

 

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbdata = 'accounts';

 

in another file named connDB.php

 

then make it require from your config.php

 

thus the config, connDB & editer page would look like this.

 

config.php

<?php
require("connDB.php");

$connection = mysql_connect($dbhost, $dbuser, $dbpass) 
or die ("Could not connect to server ... \n" . mysql_error ());
mysql_select_db($dbdata) 
or die ("Could not connect to database ... \n" . mysql_error ());
?>

 

connDB.php

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbdata = 'accounts';
?>

 

then on editer.php

<?php


// check if user edited the page.

if(isset($_REQUEST['editfile']))
{


$editfile = $_REQUEST['editfile'];
$filename = "includes/connDB.php";
$handle = fopen($filename, "w+");
fwrite($handle, $editfile);
fclose($handle);
echo "Done Editing!";
}


// if not show the form
else {
$self = $_SERVER['PHP_SELF'];
echo "<form action='" .$self. "' method='post'/><textarea name='editfile' rows='20' cols='50'>";
$filename = "includes/connDB.php";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
echo $contents;
fclose($handle);
echo "</textarea><input type='submit' value='Submit'/></form>";
}

?>

 

its much simpler :)

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.