将php文件加载到变量时出现问题(加载php代码的结果,而不是作为字符串加载代码)
我有一个网站架构,我将内容分配给变量,然后在母版页中打印它们。我的问题是子页面中的php代码作为字符串导入到变量中。是否有方法确保代码实际执行,并将结果导入变量中 在下面的示例中,signup_header.php中的php代码作为字符串导入$page_header。结果是“getVerifiedEmail();?>”显示在表单元素中,而不是电子邮件地址 master.php将php文件加载到变量时出现问题(加载php代码的结果,而不是作为字符串加载代码),php,master-pages,readfile,Php,Master Pages,Readfile,我有一个网站架构,我将内容分配给变量,然后在母版页中打印它们。我的问题是子页面中的php代码作为字符串导入到变量中。是否有方法确保代码实际执行,并将结果导入变量中 在下面的示例中,signup_header.php中的php代码作为字符串导入$page_header。结果是“getVerifiedEmail();?>”显示在表单元素中,而不是电子邮件地址 master.php <!DOCTYPE HTML> <html> <head> <?php
<!DOCTYPE HTML>
<html>
<head>
<?php echo $page_header; ?>
</head>
<body id="home">
<div class = "container">
<?php echo $page_content; ?>
</div>
</body>
</html>
signup.php:
<?php
$page_content = file_get_contents("./include/signup_content.php");
$page_header = file_get_contents("./include/signup_header.php");
include('master.php');
?>
signup_header.php
<script type="text/javascript">
$(document).ready(function(){
$('input[name="name"]').attr('value', "<?php echo $idpAssertion->getVerifiedEmail(); ?>");
});
</script>
$(文档).ready(函数(){
$('input[name=“name”]”)。attr('value',“”);
});
signup_content.php
<section>
<form class="task" method="POST">
Name: <input type="text" name="name" maxlength="30" value=""/><br/>
Email: <input type="text" name="email" value=""/><br/>
UserId: <input id="userId" type="text" name="userId" value="" /><br/>
</form>
</section>
名称:
电子邮件:
用户标识:
file\u get\u contents
返回文件的实际内容,您需要的是实际解析PHP文件。file\u get\u contents
返回文件的实际内容,您需要的是实际解析PHP文件。在signup.PHP中使用
<?php
$page_content = include("./include/signup_content.php");
$page_header = include("./include/signup_header.php");
include('master.php');
?>
这就是您需要的。在signup.php中使用
<?php
$page_content = include("./include/signup_content.php");
$page_header = include("./include/signup_header.php");
include('master.php');
?>
这就是您需要的。使用将返回实际文件的内容。但是您希望执行该文件。您可以使用来执行php文件,但大多数情况下,该文件本身已经创建了输出。这可能不是你想要的
相反,您仍然可以使用include
,但将输出捕获到缓冲区中。这就是所谓的
为了使您的程序更易于访问,您可以创建一个处理细节的小助手函数。然后,您可以调用将包含所讨论的文件的函数,并返回实际输出。然后可以将返回值分配给变量
示例:
<?php
/**
* include_get_contents
*
* include a file and return it's output
*
* @param string $path filename of include
* @return string
*/
function include_get_contents($path)
{
ob_start();
include($path);
return ob_get_clean();
}
$page_content = include_get_contents("./include/signup_content.php");
$page_header = include_get_contents("./include/signup_header.php");
include('master.php');
?>
使用将返回实际文件的内容。但是您希望执行该文件。您可以使用来执行php文件,但大多数情况下,该文件本身已经创建了输出。这可能不是你想要的
相反,您仍然可以使用include
,但将输出捕获到缓冲区中。这就是所谓的
为了使您的程序更易于访问,您可以创建一个处理细节的小助手函数。然后,您可以调用将包含所讨论的文件的函数,并返回实际输出。然后可以将返回值分配给变量
示例:
<?php
/**
* include_get_contents
*
* include a file and return it's output
*
* @param string $path filename of include
* @return string
*/
function include_get_contents($path)
{
ob_start();
include($path);
return ob_get_clean();
}
$page_content = include_get_contents("./include/signup_content.php");
$page_header = include_get_contents("./include/signup_header.php");
include('master.php');
?>
及
就这些
我希望signup_content.php只包含类似的模板
及
就这些
我希望signup_content.php只包含类似的模板使用可以使用eval函数
$string=eval('?'.>'.file_get_contents('signup_content.php',1)。use可以使用eval函数
$string=eval('?'.>'.file_get_contents('signup_content.php',1).“看一看我下面的建议,很简单,我想这就是你要找的。看一看我下面的建议,很简单,我想这就是你要找的。问题是include的内容是即时输出的,而不是在我需要的变量中。@Mattias为什么你需要将它保存在变量中?问题是include的内容是即时输出的,而不是在我需要它的变量中。@Mattias为什么需要将其保存在变量中?问题是include的内容是即时输出的,而不是在我需要它的变量中。问题是include的内容是即时输出的,而不是在我需要它的变量中T
$string = eval('?'.'>'.file_get_contents('signup_content.php',1).'<'.'?');
echo $string;