Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用include加载页面_Php - Fatal编程技术网

Php 如何使用include加载页面

Php 如何使用include加载页面,php,Php,我试图用php编写一个页面,为php文件保存代码,在该文件中,我必须通过链接加载不同的页面 这是我的页面选择功能: $page_to_see=$_GET['PageToSee']; if (!isset($page_to_see)) { $page_to_see="include 'pages/home.html';"; } else { $page_to_see="include 'pages/".$page_to_see.".html';"; } $u从链接(www.

我试图用php编写一个页面,为php文件保存代码,在该文件中,我必须通过链接加载不同的页面

这是我的页面选择功能:

$page_to_see=$_GET['PageToSee'];
if (!isset($page_to_see))  
{
    $page_to_see="include 'pages/home.html';";
}
else
{
    $page_to_see="include 'pages/".$page_to_see.".html';";
} 
$u从链接(www.mysite.com/myfile.php?PageToSee=home)获取值

在写入文件时,代码为:

$content="<table align='center'>";
$content.="<tr align='center'>";
$content.="<td align='center'>";
$content.="<?PHP include 'pages/header.php'; ?>";
$content.="</td></tr>";
$content.="<tr><td align='center'>";
$content.="<?PHP echo $page_to_see; ?>";
$content.="</td>";
$content.="</tr>";
$content.="</table>";
$content=”“;
$content.=”;
$content.=”;
$content.=”;
$content.=”;
$content.=”;
$content.=”;
$content.=”;
$content.=”;
$content.=”;
我在预览中得到的只是标题,其他什么都没有


有人能帮我吗我不知道该怎么办了,我尝试了一些没用的方法:-(

首先,在PHP文件中,必须定义一个字符串数组,指定实际可以包含的页面,以避免包含注入

然后,您必须测试
$\u GET
变量,以了解它是否对应于数组中的任何条目

myFile.php:

$pages = array('home', 'page2', 'page3');
if(in_array($_GET['PageToSee'], $pages)) {
    include($_GET['PageToSee'].'php'); // Add correct extension of your file
}
此外,对于html,必须输出正确的锚定:

echo '<a href="./myFile.php?PageToSee=home">Some Label</a>';
echo';

这一行很奇怪:

$content.="<?PHP echo $page_to_see; ?>";

您不需要在以下内容中引用:

$page_to_see="include 'pages/".$page_to_see.".html';";
尝试将其更改为:

$page_to_see= include 'pages/".$page_to_see.".html';

您可以尝试此操作,但请确保您的$page\u to\u see


$content=“”..等此代码在哪个文件中?在编写html文件的其他文件中,$page_____see函数包含在编写文件中,但当用户加载已写入的页面时,它只读取变量$page___see,我不能使用简单的$content。=$page____see;因为这样我会得到错误未定义的变量:page_______see,只有在我离开时才读取变量对于它构建的新文件,我不能使用简单的$content。=$page_to_see;因为随后我得到了错误未定义变量:page_to_see,当我转到它构建的新文件时,变量是只读的–Gil Josef刚刚编辑了它,感谢它起初不起作用,但我接受了你的想法,对它做了一些修改,效果很好;-)
$page_to_see= include 'pages/".$page_to_see.".html';
<?php
      $content="<table align='center'>";
      $content.="<tr align='center'>";
      $content.="<td align='center'>";
      $content.= file_get_contents('http://www.mysite.com/pages/header.php');
      $content.="</td></tr>";
      $content.="<tr><td align='center'>";
      $content.= file_get_contents($page_to_see);
      $content.="</td>";
      $content.="</tr>";
      $content.="</table>";
     echo $content;
 ?>