2015-05-12 101 views
1

我试图通过PHP动态设置我的页面标题。我的页面标题位于每个页面上包含的head.php文件中,因此单独执行此操作不是一种选择。动态页面标题通过PHP

我写了下面的代码,我不明白为什么它不工作:

<?php 
$currentUrl = $_SERVER['SERVER_NAME']; 
$currentpage = $_SERVER["SCRIPT_NAME"]; 
if("hoosiermommapcs.com"==$currentUrl) { 
$currentTitle = "Home"; 
} 
else if("/index.php"==$currentpage) { 
$currentTitle = "Home"; 
} 
else if("/dispatch.php"==$currentpage) { 
$currentTitle = "Request Pilot Cars"; 
} 
else if("/invoice.php"==$currentpage) { 
$currentTitle = "Submit Invoice"; 
} 
else if("/gallery.php"==$currentpage) { 
$currentTitle = "Image Gallery"; 
} 
else if("/contact.php"==$currentpage) { 
$currentTitle = "Contact Us"; 
} 
$siteTitle = "Hoosier Momma Pilot Car Services | "; 
?> 

我的页面标题代码:

<title><?php echo($siteTitle . $currentTitle); ?></title> 

代码工作设置“家”,而不是在任何其他人。如果我去invoice.php它仍然会在标题中显示“Home”。

任何帮助表示赞赏。

+0

'开关($ _ SERVER [“SCRIPT_NAME”]){}'是理想的选择。 – Devon

+0

直到现在还没有想到使用开关盒功能。可以尝试。谢谢。 –

+0

我从标题中删除了已解决。接受答案会自动将其标记为正在解决。发布自己的答案,接受给定的答案或(标记)删除问题。 –

回答

1

我删除了一条线,得到它的工作。张贴在情况下别人的答案碰到过这样的:

我删除:

if("hoosiermommapcs.com"==$currentUrl) { 
$currentTitle = "Home"; 
} 

,使我的代码:

<?php 
$currentpage = $_SERVER["SCRIPT_NAME"]; 
if("/index.php"==$currentpage) { 
$currentTitle = "Home"; 
} 
else if("/dispatch.php"==$currentpage) { 
$currentTitle = "Request Pilot Cars"; 
} 
else if("/invoice.php"==$currentpage) { 
$currentTitle = "Submit Invoice"; 
} 
else if("/gallery.php"==$currentpage) { 
$currentTitle = "Image Gallery"; 
} 
else if("/contact.php"==$currentpage) { 
$currentTitle = "Contact Us"; 
} 
$siteTitle = "Hoosier Momma Pilot Car Services | "; 
?> 

标题:

<title><?php echo($siteTitle . $currentTitle); ?></title> 
+0

你明白为什么修好了吗? – Devon

+0

是和不是。我假设我的原始代码严格匹配URL,并且由于每个页面上的URL都是相同的,所以变量保持不变。 –

+0

你正在使用'else if'。所以一旦URL匹配,其他条件都不会被考虑,并且我认为URL可以匹配任何页面。 – Devon

2

两个问题:

  1. $currentUrl.$currentpage将包含完整的主机名和查询字符串,但你只核对查询字符串在你的if else
  2. 您的解决方案可能不会工作,如果URL有参数,例如/index.php?rel=xxx。尝试使用$_SERVER["SCRIPT_NAME"]代替$_SERVER['REQUEST_URI']
+0

我用这个更新了我的代码,它没有工作。我将用新代码更新我的问题,以便您可以查看一下,看看我是否正确。 –

+0

使用'var_dump($ currentpage)'打印'$ currentpage'的值,让我知道你为'invoice.php'得到了什么 –

+0

string(12)“/invoice.php” –

0

要单独得到公正的文件,你可以使用:

$_SERVER['ORIG_PATH_INFO']; 

这是独立的URL参数。它也独立于包含并获取您在地址栏中看到的信息。以下是一些结果:

URL          RESULT 
http://example.com/index.php   /index.php 
http://example.com/about.php?a=dsa  /about.php 
http://example.com/t/t.php?t=t   /t/t.php 

正如您所看到的,这也与URL参数无关。从那里,你可以这样做:

switch($_SERVER['ORIG_PATH_INFO']) 
{ 
    default: 
     $title = ""; 
     break; 
    case "/index.php": 
     $title = "Home"; 
     break; 
    case "/about.php": 
     $title = "About"; 
     break; 
} 

或者你可以使用一本字典如果交换机/箱变得令人生畏:

$file = $_SERVER['ORIG_PATH_INFO']; 
$titleDict = [ 
    "/index.php" => "Home", 
    "/about.php" => "About" 
]; 

if(array_key_exists($file,$titleDict)) 
    $title = $titleDict[$file]; 
else 
    $title = ""; 
+0

试过了。替换$ _SERVER [“SCRIPT_NAME”];与它并且拿出斜线,仍然没有改变。它可能是别的吗? –

+0

如果您所做的只是做出替换,您将看不到任何结果。查看我的更新开关/案例或字典选项。完全重做实施。 –

+0

'__FILE__'在这种情况下,我相信会返回head.php而不是index.php或其他。 – Devon