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

问题描述:

我是第一个承认我是绿色的PHP编码。然而很多年前,一位同事给了我一个模式,用于通过HTML连接PHP来创建网页。现在,我正在修改网站,但我想知道是否有更好的方法来编写它?目前,我有它有一个类似的布局的index.php页面:如何使php包含在网站中的html页面

<?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他们会得到整个关于内置在浏览器中所有必需元,头,边栏页,footer,javascript,css,analytics等。每个必需的部分都是html文件,有些可能有一些$标注的php脚本,例如Page Title,关键字等。

我的问题之一是当我客户希望将其中一个'($ content ==“”)'的名称更改为其他名称。首先,我可以更改变量,但是我必须将旧名称重定向到新名称,以便我们不会丢失页面排名。

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

最终,客户端会将所有或大部分页面重定向为更直接,http://yourwebsite.com/about-us。据信,当网站变成WordPress网站时,这将使重建更顺利。

那么,有没有更好的方法来写这个?有更好的方法来重定向网址吗?

谢谢...

+0

你应该有一个看起来MVC模式。这是比这更好,更强大的方法。 –

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

要回答你的问题,另一种常用的系统是这样的:

文件:include.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重定向页面。无论哪种方式,页面被重命名意味着他们失去索引。为什么页面需要经常更名?

+0

谢谢大家的帮助。是的,我一直在努力学习PHP,但我有点迷路。我想我也需要一本新书... 感谢您的支持。 – Heidi

http://php.net/manual/de/function.file-get-contents.php 所以您可以在PHP(VAR)的HTML网站

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

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