php 如何在另一页中回显一个页面的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5755241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:23:20  来源:igfitidea点击:

How to echo the contents of one page in another page?

php

提问by KPO

I am doing client customizations and i have multiple files. If for example i want to show the code of page 1.php on page home.php, how can i do it by echoing it or something. Please help me. Thanks!

我正在做客户定制,我有多个文件。例如,如果我想在页面 home.php 上显示页面 1.php 的代码,我该如何通过回显或其他方式来实现。请帮我。谢谢!

采纳答案by Shakti Singh

You can includefiles in a PHPscript by adding this your home.phpfile:

您可以通过将此文件添加到脚本中来包含文件:PHPhome.php

if ($example){
include("page 1");
}
else {
include("Error.php");
}

Or another way to do is file_get_contents():

或者另一种方法是file_get_contents()

$content = file_get_contents($file_path);
echo $content;

回答by mario

If including the scripts won't work, then you will need the workaround over the local webserver to merge just the contents (still need to cut out the content/body part):

如果包含脚本不起作用,那么您将需要通过本地网络服务器的解决方法来合并内容(仍然需要删除内容/正文部分):

print(file_get_contents("http://$_SERVER[SERVER_NAME]/page1.php"));

But a saner solution is most always just using an iframe and have the page inclusion handled by the browser:

但更明智的解决方案通常是使用 iframe 并由浏览器处理页面包含:

<iframe src="page1.php" width="500" height="400" ></iframe>

Or more newfangled with some jQuery:

或者更新颖的一些 jQuery:

<div id="page1"></div>
<script>$("#page1").load("page1.php body");</script>

回答by tim_a

Use include()or require(). Make sure you add one of these statements at the top of your code.

使用include()require()。确保在代码顶部添加这些语句之一。

Hereis some info from the php manual.

是php手册中的一些信息。

回答by Headshota

if you need to echo plain text use

如果您需要回显纯文本使用

echo file_get_contents('1.php');