2011-04-28 68 views
0

如何动态地在我的简单PHP网站的每个页面的<head>中添加不同的标题,关键字和描述?在每个页面的不同标题,关键字和描述

我已经在我的所有页面中包含了文件header.php,我怎么知道用户在哪个页面?

例如,我有php文件register.php和login.php,我需要不同的标题,关键字和描述。我不想使用$_GET方法。

谢谢!

+0

如果你有有很多文件您应该创建一个数据库并从那里检索值,否则只需手动完成。 – 2011-08-09 20:34:44

回答

1

把输出放入一个函数(在你的header.php中),并将它的参数在适当的地方插入到标记中。

function html_header($title = "Default") { 
    ?><!DOCTYPE html> 
    <html> 
    <head> 
     <meta charset="utf-8"> 
     <title><?php echo $title ?></title> 
    </head> 
    … 
    <?php 
} 
6

在每个页面的顶部设置变量,将由header.php读取。然后在header.php的正确位置插入变量的值。这里有一个例子:

register.php:

<?php 
    $title = "Registration"; 
    $keywords = "Register, login"; 
    $description = "Page for user registration"; 

    include('header.php'); 
?> 

的header.php

<html> 
    <head> 
     <meta name="keywords" content="<?php echo $keywords; ?>" /> 
     <meta name="description" content="<?php echo $description; ?>" /> 
     <title><?php echo $title; ?></title> 
    </head> 
    <body> 
1

你可以试试这个:

例如$page变量是页面名称:

<?php 
switch($page) 
    { 
    case 'home': 
    $title = 'title'; 
    $keyword = 'some keywords..'; 
    $desc = 'description'; 
    break; 
    case 'download': 
    $title = 'title'; 
    $keyword = 'some keywords..'; 
    $desc = 'description'; 
    break; 
    case 'contact': 
    $title = 'title'; 
    $keyword = 'some keywords..'; 
    $desc = 'description'; 
    break; 
    } 

if(isset($title)) 
{ 
    ?> 
<title><?php echo $title; ?></title> 
<meta name="keywords" content="<?php echo $keyword; ?>" /> 
<meta name="description" content="<?php echo $desc; ?>" /> 
<?php 
} 
else 
{ 
    ?> 
<title>default</title> 
<meta name="keywords" content="default" /> 
<meta name="description" content="default" /> 
<?php 
} 
?> 
+2

为什么不立即设置所需的变量,而不是使用'$ page'和一个巨大的'switch ... case'? – 2011-04-28 15:35:59