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:
- You send PHP request to
edit.php
with an argumentadd=(text)
, (text) is some random text you may want to add. (text)
gets added toraw.php
by changing 2nd line fromecho "(already existing text)"
toecho "(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:
- edit.php (this will fire the GET request)
- raw.php (it will do the thing)
- 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;