duannian7116 2015-11-16 12:03
浏览 5
已采纳

有可能直接从PHP更改PHP源代码吗?

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...)

  • 写回答

2条回答 默认 最新

  • dongyan5239 2015-11-16 12:15
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用