Example:
source:
$var=X
url call:
....?changeSourceTo:Y
php:
//do something? (what)
source:
$var=Y
This should be useful for a rapid change of the source code without opening it and without passing the next call the same parameter to the url.... (I know I could use a file with the var parameter and if $_GET['var'] change the file, or maybe better set up a database...)
Put the different versions of code into different files and use require($changeSourceTo); to specify which file to load.
I should add that this carries a few inherent security risks because the user is able to specify filesystem paths and alter the code via the request, both could be open to abuse by an attacker. So maybe use in dev, but ensure it doesn't go live.
You can do it, yes. Just open the .php file from inside the file using fopen(), modify what you need and then save (overwriting it).
So if you have test.php with this code:
<?
$x = 1;
$myfile = fopen('test.php', 'w');
$txt = '<?$x=2;echo $x?>';
fwrite($myfile, $txt);
fclose($myfile);
echo $x;
?>
The first time you run it you will see "1". If you reload the page, you will see "2". It can be tricky if you've got a lot of code, in which case I would suggest to split the file into "pieces" and use them with include, so you can modify the small piece you really need to.
As Steve mentioned, be very careful the way you modify it. If my example could be modified with a $_GET[], for example, it would be extremely unsafe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With