2014-10-28 64 views
0

我创建了一个PHP网站,有一个index.php文件,而不是有文件something.php,about.php,contact.php我已经设置它如下:index.php ?p =东西,的index.php p =联系等HTML元数据后<head>标签

目前我的index.php代码如下所示:

<head> 
    <title>Something</title> 
    <link href="theme.css" rel ="stylesheet" media="screen"> 
</head> 

<body> 
<?php 
if (@$_GET['p'] == 'something') include 'pages/something.php'; 
elseif (@$_GET['p'] == 'about') include 'pages/about.php'; 
</body> 

现在的问题是,我想在不同的index.php元,something.php和about.php。我知道我必须在<head></head>之间添加元数据,但稍后会包含something.php和about.php。如何为这些文件提供不同的元数据? <head></head>标签后是否可以(重新)设置元数据?

+1

PHP不能达到到过去修改已经发生的输出。你必须缓冲你的输出,所以它根本不会被发送出去,然后修改缓冲区,或者使用javascript修改'',一旦它到达客户端,或者修改你的脚本,以便有多个步骤来生成一个页面,并在适当的时间/地点输出元数据。 – 2014-10-28 20:16:59

回答

1

在你的index.php

<?php 
if (@$_GET['p'] == 'something') include 'pages/something.php'; 
elseif (@$_GET['p'] == 'about') include 'pages/about.php'; 
?> 

,并在您about.phpsomething.php包括包括完整的HTML页面,它的头标记

1

我不知道,我明白你的问题,当然,你也可以在标题中使用if条件!

<head> 
    <title>Something</title> 
    <link href="theme.css" rel ="stylesheet" media="screen"> 
    <?php 
     if ($_GET["p"] === 'something') { 
      ?> 
    <!-- Write your metadata here if the page is something... --> 

    <?php 
     } elseif ($_GET["p"] === 'about') { 
     ?> 
    <!-- Write your metadata here if the page is about... --> 
    <?php 
     } 
    ?> 
</head> 

<body> 
    <?php 
    if ($_GET['p'] == 'something') { 
     include 'pages/something.php'; 
    } elseif ($_GET['p'] == 'about') { 
     include 'pages/about.php'; 
    } 
    ?> 
</body> 
1

尝试加载元数据根据您的喜好,因为这样的:

<head> 
    <title>Something</title> 
    <link href="theme.css" rel ="stylesheet" media="screen"> 
    <?php if (@$_GET['p'] == 'something') echo "<meta charset='utf-8'>"; ?> 
</head> 

<body> 
<?php 
if (@$_GET['p'] == 'something') include 'pages/something.php'; 
elseif (@$_GET['p'] == 'about') include 'pages/about.php'; 
?> 
</body>