How can I edit a PHP command using PHP?

Viewed 47

So, I'm a PHP newbie who has been trying to do a PHP program that will edit some other PHP program by adding some more info.

How I want this to work:

  1. You send PHP request to edit.php with an argument add=(text), (text) is some random text you may want to add.
  2. (text) gets added to raw.php by changing 2nd line from echo "(already existing text)" to echo "(already existing text) (text)"

Can somebody help me? I can't figure this out. Any help would be appreciated!

3 Answers
  • You need to access "raw.php" content using "file_get_contents" function check this link

file_get_contents

  • You need to add special string in the target place in "raw.php" like "TARGET_PLACE_TEXT" ... THEN use "str_replace" function check this link

str_replace

  • To save the new file content check this link

file_put_contents

IF you can send Your code, I'll make a function to do this job for you.

I'd like to suggest you this aternative way: Add a new file for the text content, a JSON file would be perfect, but i don't want to make it more complicated, we will stick with a txt for now. It's not good to edit the php source code with another php script, it is often done by malicious script.

So we have:

  1. edit.php (this will fire the GET request)
  2. raw.php (it will do the thing)
  3. mycontent.txt (it will store the text content)
//This is raw.php

/*This is the new content retrived with GET*/
$my_new_content = $_GET["add"]; 

/*Update txt file with the new content, 
the new text will be append, I add an extra space in the front.*/
file_put_contents("mycontent.txt", " " . $my_new_content, FILE_APPEND); 

/* Read the actual text from the txt file */
$my_brand_new_content = file_get_contents("mycontent.txt"); 

echo $my_brand_new_content;

if you want to add text, than use mysql. From edit.php store the input text to mysql table. Then in raw.php just query the table in asc order to see text appear in the same way

(already existing text) 
(text)

and so on

Related