Jesse Elser
Jesse Elser

Reputation: 976

Dynamic Page Title Via PHP

I'm trying to set my page title dynamically through PHP. My page title is in a head.php file which is included on every page so doing this individually is not an option.

I wrote the following code and I cannot understand why it is not working:

<?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 | ";
?>

And my page title code is:

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

The code works for setting "Home" but not on any of the others. If I go to invoice.php it still says "Home" in the title.

Any help is appreciated.

Upvotes: 1

Views: 128

Answers (3)

Jesse Elser
Jesse Elser

Reputation: 976

I removed a line and got it working. Posting as an answer in case others come across this:

I removed:

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

and made my code:

<?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:

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

Upvotes: 1

Muhammad Abdul-Rahim
Muhammad Abdul-Rahim

Reputation: 2010

To get just the file by itself, you can use:

$_SERVER['ORIG_PATH_INFO'];

This is independent of URL parameters. It also is independent of includes and gets the information you see in the address bar. Here are some results:

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

As you can see, this is independent of URL parameters as well. From there, you can do something like:

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

Or you can use a dictionary if the switch/case gets daunting:

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

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

Upvotes: 0

Tuan Anh Hoang-Vu
Tuan Anh Hoang-Vu

Reputation: 1995

Two problems:

  1. $currentUrl.$currentpage will contains full hostname and query string, but you only check against query string in your if else
  2. Your solution will not likely to work if URL has parameters, for example /index.php?rel=xxx. Try to use $_SERVER["SCRIPT_NAME"] instead $_SERVER['REQUEST_URI']

Upvotes: 2

Related Questions