PHP动态网页


PHP Dynamic Webpage

我试图创建一个动态网站,其中某些网站内容加载到index.php的身体。我目前将网站分为3个部分:页眉,正文和页脚,其中每个部分都是相互动态分离的。

<?php
     if(!file_exists('content/header.php'))
     {
          die('Sorry we are experiencing technical difficulties. Please come back later.');
     }
     else
          include('content/header.php');
?>
<?php
    include('content/body.php'); 
?>
<?php    
    include('content/footer.html'); 
?>

现在我希望做的是有特定的页面内容加载到body。php如果让我们说,我点击一个超链接,上面写着"注册"所以用户注册他们自己的网站,注册。php将其内容加载到body。php。

我试着四处搜索,但我认为我在谷歌搜索中问错了问题,所以我想我应该在这里解释一下我的自我,希望有人能指引我正确的方向,谢谢你的时间。

如果您希望用户不会被重定向,请查看AJAX。

否则,您可以将寄存器重定向到index.php?page=register,代码为

<?php
    $page = isset($_GET['page']) ? 'body' : someSanitizingFunction($_GET['page']);
    include('content/'.$page.'.php'); 
?>

这显然是一个不安全的实现,但你会从中得到灵感。

你还应该看看像Smarty这样的模板引擎,这可能就是你想要的。

要在PHP中创建简单的布局,您可以编写像about.php, contact.php和register.php这样的页面你可以把这段代码放到body.php

  $whitelist = array("about", "register", "contact");
  if(in_array($_GET['page'], $whitelist)) {
    include($_GET['page'].".php");
  } else {
     include("error404.php");
  } 

你的url应该像index.php?page=register.

我还建议使用Smarty、Twig或TemplatePower看看MVC框架。