如何使 php 包含用于网站中的 html 页面


How to make php includes for html pages in web site

我是第一个承认我在PHP编码方面是绿色的人。然而,很多年前,一位同事给了我一种模式,用于将PHP与HTML连接起来以创建网页。现在,我正在寻求改造网站,但我想知道是否有更好的方法来编写它?目前,我有一个索引.php页面,其布局类似于以下内容:

<?php 
if (! isset($HTTP_GET_VARS['content']) || ! $HTTP_GET_VARS['content']){
  $content="home";
}
else 
  $content=$HTTP_GET_VARS['content'];
//1
 if ($content == "home"){
    $page_title="Page Title";
    $keywords="Keywords found in Meta";
    $desc="Description found in Meta";
    $style="scripts/style.css";
    $popupjs="none";
    $storbutnjs="none";
    $retreatjs="none";
    $rolloverjs="scripts/rolloverjs.js";
    $readform_chkjs="none";
    $logo="req-files/logo.html";
    $sidebar="req-files/sidebar.html";
    $main="home.html";
}
//2
if ($content == "about"){
    $page_title="Page Title";
    $keywords="Keywords found in Meta";
    $desc="Description found in Meta";
    $style="scripts/style.css";
    $popupjs="none";
    $storbutnjs="none";
    $retreatjs="none";
    $rolloverjs="none";
    $readform_chkjs="none";
    $logo="req-files/logo.html";
    $sidebar="req-files/sidebar.html";
    $main="about.html";
}
include ("req-files/head.html");
include ($logo);
include ("req-files/navbar.html");
include ($sidebar);
include ($main);
/*include ("scripts/analytics.js");*/
include ("req-files/footer.html");
?>

因此,如果一个人输入http://yourwebsite.com/?content=about他们将在浏览器中构建整个"关于"页面,其中包含所有必需的元,标题,侧边栏,页脚,javascript,css,分析等。这些必需部分中的每一个都是 html 文件,有些可能有用于某些 $ 标注的 php 脚本,如页面标题、关键字等。

我的一个问题是当我的客户想要将"($content==")"之一的名称更改为其他名称时。首先,我可以更改变量,但随后我必须将旧名称重定向到新名称,这样我们就不会丢失页面排名。

例如,http://yourwebsite.com/?content=about需要重定向到 http://yourwebsite.com/?content=about-us

最终,客户端将重定向所有或大多数页面以使其更直接,http://yourwebsite.com/about-us。相信当网站变成WordPress网站时,这将使重建更加顺利。

那么,有没有更好的写法呢?有没有更好的方法来重定向 URL?

谢谢。。。

$HTTP_GET_VARS 已被弃用。请尝试从官方文档中学习 PHP。

为了回答您的问题,另一个常用的系统是这样的:

文件:包括.php

<?php
function topbanner($title) { ?>
  <!doctype html>
  <html>
  <head>
  <title><?php echo $title; ?></title>
  <script type="text/javascript" src="jquery.js"></script>
  </head>
  <body>
  <header>
  Site name, Logo, etc.
  <header>
<?php }
function footer() { ?>
  <footer>
  &copy;2012. Your company name. Best viewed in Mozilla Firefox.
  </footer>
  </body>
  </html>
<?php }

现在,像往常一样创建 html 页面,但要确保扩展名.php。在这些页面中,执行此操作。

<?php require_once('include.php'); topbanner("Title of this page"); ?>
<h3>Welcome to this site</h3>
<p>Content content content</p>
<img src="image.jpg" />
<?php footer(); ?>

这适用于简单页面。如果您需要更复杂的设置,请按照 fork-cms 的样式使用 .htacess 重定向页面。无论哪种方式,页面被重命名都意味着它们会失去索引。为什么页面需要经常重命名?

http://php.net/manual/de/function.file-get-contents.php所以你可以在PHP(var)中包含HTML网站

$page = file-get-contents('myHTMLSite.html');

str_replace('{header}', $header, $page);