How can I edit a PHP command using PHP?

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!

>Solution :

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;

Leave a Reply