2011-04-28 53 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

相關問題