str_replace 似乎正在禁用 PHP

str_replace seems to be disabling PHP

我正在使用 str_replace 根据所选域名更改模板中的 URL。但它似乎阻止加载模板中的 PHP 代码。

这是我所做的:

<?php

$domain = $website_data['domain'];
$path_to_file = 'assets/sites/'.$domain.'/index.php';
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace('domainhere',$domain,$file_contents);
file_put_contents($path_to_file,$file_contents);

echo $file_contents;

?>

所以它做了我想让它做的事,然后它回显 $file_contents 并且页面显示文件。除了页面上的任何 PHP 内容不会呈现。

例如,如果我这样做:

<?php echo $domain ?>

它会是空白的,如果我查看源代码,我可以看到上面写的。

我不是 PHP 专家,您可以猜到,所以任何想法都将不胜感激,因为我显然遗漏了一些东西!

我想你想做的是:

<?php

$domain = $website_data['domain'];
$path_to_file = 'assets/sites/'.$domain.'/index.php';
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace('domainhere',$domain,$file_contents);
file_put_contents($path_to_file,$file_contents);
include($path_to_file);

?>

如上面的评论所述,使用 echo() 显示的内容不会作为程序代码执行。

我很惊讶没有人提到这是一个多么糟糕的想法。如果您有两个不同的用户访问此脚本,他们将覆盖彼此的更改。此外,在第一次更改后,'domainhere' 将不再存在于原始文件中。

相反,在第一个文件中任何有 "domainhere" 的地方,将其更改为变量名,然后在简单地包含文件之前设置变量。