【问题标题】:How can I edit a PHP command using PHP?如何使用 PHP 编辑 PHP 命令?
【发布时间】:2022-09-23 21:42:44
【问题描述】:

所以,我是一个 PHP 新手,一直在尝试做一个 PHP 程序,通过添加更多信息来编辑其他一些 PHP 程序。

我希望它如何工作:

  1. 您向edit.php 发送PHP 请求并带有参数add=(text),(文本)是您可能要添加的一些随机文本。
  2. 通过将第二行从 echo \"(already existing text)\" 更改为 echo \"(already existing text) (text)\"(text) 被添加到 raw.php

    有人可以帮助我吗?我想不通。任何帮助,将不胜感激!

  • 为什么你需要这样做?为什么不使用变量?
  • 这不是一个好习惯
  • 我也想警告您,无论您尝试做什么都不是您所说的良好做法@stysan!
  • 请提供足够的代码,以便其他人更好地理解或重现问题。
  • @islemdev 喜欢 $text = \"(already existing text) (text)\" 而不是 echo \"(already existing text) (text)\"

标签: php edit


【解决方案1】:

我想建议你这种替代方式: 为文本内容添加一个新文件,一个 JSON 文件将是完美的,但我不想让它变得更复杂,我们现在将坚持使用 txt。 用另一个 php 脚本编辑 php 源代码是不好的,这通常是由恶意脚本完成的。

所以我们有:

  1. edit.php(这将触发 GET 请求)
  2. raw.php(它会做的事情)
  3. mycontent.txt(它将存储文本内容)
    //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;
    

【讨论】:

  • 非常感谢!
【解决方案2】:
  • 您需要使用“file_get_contents”功能访问“raw.php”内容检查此链接

file_get_contents

  • 您需要在“raw.php”中的目标位置添加特殊字符串,例如“TARGET_PLACE_TEXT" ... THEN 使用 "str_replace" 功能检查此链接

str_replace

  • 要保存新文件内容,请检查此链接

file_put_contents

如果您可以发送您的代码,我将创建一个函数来为您完成这项工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-17
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多