Meta tags based on file name

Discussion in 'PHP' started by oo7ml, Jan 30, 2010.

  1. #1
    Hi, i have split the header and footer of my website into includes. My website has 5 pages and i want to use different <meta> and <title> tags on each of the 5 pages.

    Considering i only have one header.php which is included on each page - i was thinking that i could use an if statement to determine what code needs to be displayed in the header on each page.

    I am not sure how to do this but i am thinking that i need something like

    IF $filename = index.php
    {
    display this code
    }

    I do not have much experience with php so i was hoping that some of you experts could help, thanks in advance
     
    oo7ml, Jan 30, 2010 IP
  2. HivelocityDD

    HivelocityDD Peon

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The simplest method is on each page before including the header section set the name of page as follows

    index.php file
    
    
    <?php
    
    $filename = "index.php";
    
    include('header.php'); // then include your header section
    ?>
    
    
    PHP:
    header.php file

    
    <?php
    
    if ($filename == 'index.php') {
    ?>
    <meta keyword="index">
    <meta description="">
    <?php
    
    } else if ($filename == 'login.php') {
    
    ?>
    <meta keyword="login">
    <meta description="">
    <?php
    
    }
    
    ?>
    
    
    PHP:
     
    HivelocityDD, Jan 30, 2010 IP
  3. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #3
    Thanks for the reply...

    Here is what i have but i just get a blank page now... can anyone see what is wrong:

    --- index.php

    <?php
    
    $filename = "index.php";
    
    ?>
    PHP:

    --- head.php

    <?php
    } else if ($filename == 'signs-banners.php') {
    ?>
    <title>Eagle Signs Wales - From Design to Installation, Signs and Banners </title>
    <meta name="description" content="Professional Sign and Banner Design and Installation in Wales" />
    <meta name="keywords" content="Commercial business", "Vinyl", "Graphics", "Designs", "Banners", "Signs", "Sign company", "Sign makers", “Carmarthenshire", "Pembrokeshire", "Llanelli", "West Wales" />
    <?php
    }
    ?>
    
    <?php
    } else if ($filename == 'vehicle-livery.php') {
    ?>
    <title>Eagle Signs Wales - From Design to Installation, Vehicle Livery </title>
    <meta name="description" content=" Quality Vehicle Livery in Wales " />
    <meta name="keywords" content="Professional", "Free Quote", "van", "lorry", "vinyl printing", "Installation", "Carmarthenshire", "Pembrokeshire", "Llanelli", "West Wales" />
    <?php
    }
    ?>
    
    <?php
    } else if ($filename == 'colour-printing.php') {
    ?>
    <title>Eagle Signs Wales - From Design to Installation, Full Colour Printing </title>
    <meta name="description" content="Full Colour Printing, from Design to Installation" />
    <meta name="keywords" content="Full Colour Printing", "Vinyl", "Stationary", "Graphic Design", "Board Promotions", "Free Quote", "Carmarthenshire", "Pembrokeshire", "Llanelli", "West Wales" />
    <?php
    }
    ?>
    
    <?php
    } else if ($filename == 'design.php') {
    ?>
    <title>Eagle Signs Wales – Design and Installation of Full Colour Signs and Banners </title>
    <meta name="description" content="Professional Sign and Banner Design and Installation West Wales" />
    <meta name="keywords" content="Full Colour Printing", "Vinyl", "Stationary", "Graphic Design", "Board Promotions", "West Wales", "Designs", "Designer", "Vehicle Printing", "Print", "Sign Makers", "Fleet Livery" />
    <?php
    }
    ?>
    
    <?php
    } else if ($filename == 'contact.php') {
    ?>
    <title>Eagle Signs Wales - From Design to Installation, Carmarthen, Pembrokeshire </title>
    <meta name="description" content="Free Online Quote for Sign Printing in Wales" />
    <meta name="keywords" content="Print", "Sign Makers", "Sign Company", "Graphic Designs", "Free Online Quote", "Carmarthenshire", "Pembrokeshire", "Llanelli", "West Wales" />
    <?php
    }
    ?>
    PHP:
     
    oo7ml, Jan 30, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    --- index.php

    <?php
    //or: $filename = $_SERVER['PHP_SELF'];
    $filename = "index.php";
    
    include("head.php");
    ?>
    PHP:

    --- head.php

    <?php
    
    if ($filename == 'index.php') {
    ?>
    <meta keyword="index">
    <meta description="">
    <?php
    } else if ($filename == 'signs-banners.php') {
    ?>
    <title>Eagle Signs Wales - From Design to Installation, Signs and Banners </title>
    <meta name="description" content="Professional Sign and Banner Design and Installation in Wales" />
    <meta name="keywords" content="Commercial business", "Vinyl", "Graphics", "Designs", "Banners", "Signs", "Sign company", "Sign makers", “Carmarthenshire", "Pembrokeshire", "Llanelli", "West Wales" />
    <?php
    }
    ?>
    
    <?php
    } else if ($filename == 'vehicle-livery.php') {
    ?>
    <title>Eagle Signs Wales - From Design to Installation, Vehicle Livery </title>
    <meta name="description" content=" Quality Vehicle Livery in Wales " />
    <meta name="keywords" content="Professional", "Free Quote", "van", "lorry", "vinyl printing", "Installation", "Carmarthenshire", "Pembrokeshire", "Llanelli", "West Wales" />
    <?php
    }
    ?>
    
    <?php
    } else if ($filename == 'colour-printing.php') {
    ?>
    <title>Eagle Signs Wales - From Design to Installation, Full Colour Printing </title>
    <meta name="description" content="Full Colour Printing, from Design to Installation" />
    <meta name="keywords" content="Full Colour Printing", "Vinyl", "Stationary", "Graphic Design", "Board Promotions", "Free Quote", "Carmarthenshire", "Pembrokeshire", "Llanelli", "West Wales" />
    <?php
    }
    ?>
    
    <?php
    } else if ($filename == 'design.php') {
    ?>
    <title>Eagle Signs Wales – Design and Installation of Full Colour Signs and Banners </title>
    <meta name="description" content="Professional Sign and Banner Design and Installation West Wales" />
    <meta name="keywords" content="Full Colour Printing", "Vinyl", "Stationary", "Graphic Design", "Board Promotions", "West Wales", "Designs", "Designer", "Vehicle Printing", "Print", "Sign Makers", "Fleet Livery" />
    <?php
    }
    ?>
    
    <?php
    } else if ($filename == 'contact.php') {
    ?>
    <title>Eagle Signs Wales - From Design to Installation, Carmarthen, Pembrokeshire </title>
    <meta name="description" content="Free Online Quote for Sign Printing in Wales" />
    <meta name="keywords" content="Print", "Sign Makers", "Sign Company", "Graphic Designs", "Free Online Quote", "Carmarthenshire", "Pembrokeshire", "Llanelli", "West Wales" />
    <?php
    }
    ?>
    PHP:
    :p
     
    danx10, Jan 30, 2010 IP
  5. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #5
    Here's a way to do it with allot less code:

    
    <?
    switch (basename($_SERVER['SCRIPT_FILENAME']))
    {
    case "index.php":
    	$title = "";
    	$description = "";
    	$keywords = "index";
    	break;
    case "signs-banners.php":
    	$title = "Eagle Signs Wales - From Design to Installation, Signs and Banners";
    	$description = "Professional Sign and Banner Design and Installation in Wales";
    	$keywords = "Commercial business, Vinyl, Graphics, Designs, Banners, Signs, Sign company, Sign makers, “Carmarthenshire, Pembrokeshire, Llanelli, West Wales";
    	break;
    case "vehicle-livery.php'";
    	$title = "Eagle Signs Wales - From Design to Installation, Vehicle Livery";
    	$description = "Quality Vehicle Livery in Wales";
    	$keywords = "Professional, Free Quote, van, lorry, vinyl printing, Installation, Carmarthenshire, Pembrokeshire, Llanelli, West Wales";
    	break;
    case "colour-printing.php";
    	$title = "Eagle Signs Wales - From Design to Installation, Full Colour Printing";
    	$description = "Full Colour Printing, from Design to Installation";
    	$keywords = "Full Colour Printing, Vinyl, Stationary, Graphic Design, Board Promotions, Free Quote, Carmarthenshire, Pembrokeshire, Llanelli, West Wales";
    	break;
    case "design.php";
    	$title = "Eagle Signs Wales – Design and Installation of Full Colour Signs and Banners";
    	$description = "Professional Sign and Banner Design and Installation West Wales";
    	$keywords = "Full Colour Printing, Vinyl, Stationary, Graphic Design, Board Promotions, West Wales, Designs, Designer, Vehicle Printing, Print, Sign Makers, Fleet Livery";
    	break;
    case "contact.php";
    	$title = "Eagle Signs Wales - From Design to Installation, Carmarthen, Pembrokeshire";
    	$description = "Free Online Quote for Sign Printing in Wales";
    	$keywords = "Print, Sign Makers, Sign Company, Graphic Designs, Free Online Quote, Carmarthenshire, Pembrokeshire, Llanelli, West Wales";
    	break;
    }
    ?>
    <title><?=$title?></title>
    <meta name="description" content="<?=$description?>" />
    <meta name="keywords" content="<?=$keywords?>" />
    
    PHP:
     
    Silver89, Jan 30, 2010 IP
  6. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #6
    Cool, thanks... what do i need to add to your code to make a generic general case... let's say i haven't declared a set of meta tags for certain pages... would like your code to just use a standard default case if it a page doesn't find a case that matches it's name, thanks again to all who have helped
     
    oo7ml, Jan 31, 2010 IP
  7. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #7
    Here you go, the default is now setup at the bottom:

    
    <?php
    switch (basename($_SERVER['SCRIPT_FILENAME']))
    {
    case "index.php":    
    	$title = "";
        $description = "";
        $keywords = "index";
        break;
    case "signs-banners.php":    $title = "Eagle Signs Wales - From Design to Installation, Signs and Banners";
        $description = "Professional Sign and Banner Design and Installation in Wales";
        $keywords = "Commercial business, Vinyl, Graphics, Designs, Banners, Signs, Sign company, Sign makers, “Carmarthenshire, Pembrokeshire, Llanelli, West Wales";
        break;
    case "vehicle-livery.php'";
        $title = "Eagle Signs Wales - From Design to Installation, Vehicle Livery";
        $description = "Quality Vehicle Livery in Wales";
        $keywords = "Professional, Free Quote, van, lorry, vinyl printing, Installation, Carmarthenshire, Pembrokeshire, Llanelli, West Wales";
        break;
    case "colour-printing.php";
        $title = "Eagle Signs Wales - From Design to Installation, Full Colour Printing";
        $description = "Full Colour Printing, from Design to Installation";
        $keywords = "Full Colour Printing, Vinyl, Stationary, Graphic Design, Board Promotions, Free Quote, Carmarthenshire, Pembrokeshire, Llanelli, West Wales";
        break;
    case "design.php";
        $title = "Eagle Signs Wales – Design and Installation of Full Colour Signs and Banners";
        $description = "Professional Sign and Banner Design and Installation West Wales";
        $keywords = "Full Colour Printing, Vinyl, Stationary, Graphic Design, Board Promotions, West Wales, Designs, Designer, Vehicle Printing, Print, Sign Makers, Fleet Livery";
        break;
    case "contact.php";
        $title = "Eagle Signs Wales - From Design to Installation, Carmarthen, Pembrokeshire";
        $description = "Free Online Quote for Sign Printing in Wales";
        $keywords = "Print, Sign Makers, Sign Company, Graphic Designs, Free Online Quote, Carmarthenshire, Pembrokeshire, Llanelli, West Wales";
        break;
    default:
        $title = "Default Title";
        $description = "Default Description";
        $keywords = "Default Keywords";
        break;
    }
    ?>
    
    <title><?=$title?></title>
    <meta name="description" content="<?=$description?>" />
    <meta name="keywords" content="<?=$keywords?>" />
    
    PHP:
     
    Silver89, Jan 31, 2010 IP
  8. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #8
    Cool, thanks a million... much appreciated, thanks
     
    oo7ml, Jan 31, 2010 IP
    Silver89 likes this.
  9. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #9
    One thing i don't understand about your code is that you a mixture of : and ; after the case... which is the correct symbol

    <?php
    switch (basename($_SERVER['SCRIPT_FILENAME']))
    {
    case "index.php":
    $title = "";
    $description = "";
    $keywords = "index";
    break;
    case "signs-banners.php": $title = "Eagle Signs Wales - From Design to Installation, Signs and Banners";
    $description = "Professional Sign and Banner Design and Installation in Wales";
    $keywords = "Commercial business, Vinyl, Graphics, Designs, Banners, Signs, Sign company, Sign makers, “Carmarthenshire, Pembrokeshire, Llanelli, West Wales";
    break;
    case "vehicle-livery.php'";
    $title = "Eagle Signs Wales - From Design to Installation, Vehicle Livery";
    $description = "Quality Vehicle Livery in Wales";
    $keywords = "Professional, Free Quote, van, lorry, vinyl printing, Installation, Carmarthenshire, Pembrokeshire, Llanelli, West Wales";
    break;
    case "colour-printing.php";
    $title = "Eagle Signs Wales - From Design to Installation, Full Colour Printing";
    $description = "Full Colour Printing, from Design to Installation";
    $keywords = "Full Colour Printing, Vinyl, Stationary, Graphic Design, Board Promotions, Free Quote, Carmarthenshire, Pembrokeshire, Llanelli, West Wales";
    break;
    case "design.php";
    $title = "Eagle Signs Wales – Design and Installation of Full Colour Signs and Banners";
    $description = "Professional Sign and Banner Design and Installation West Wales";
    $keywords = "Full Colour Printing, Vinyl, Stationary, Graphic Design, Board Promotions, West Wales, Designs, Designer, Vehicle Printing, Print, Sign Makers, Fleet Livery";
    break;
    case "contact.php";
    $title = "Eagle Signs Wales - From Design to Installation, Carmarthen, Pembrokeshire";
    $description = "Free Online Quote for Sign Printing in Wales";
    $keywords = "Print, Sign Makers, Sign Company, Graphic Designs, Free Online Quote, Carmarthenshire, Pembrokeshire, Llanelli, West Wales";
    break;
    default:
    $title = "Default Title";
    $description = "Default Description";
    $keywords = "Default Keywords";
    break;
    }
    ?>
     
    oo7ml, Jan 31, 2010 IP
  10. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #10
    Fixed, the correct symbol to use after case is :

    
    <?php
    switch (basename($_SERVER['SCRIPT_FILENAME']))
    {
    case "index.php":    
    	$title = "";
        $description = "";
        $keywords = "index";
        break;
    case "signs-banners.php":
    	$title = "Eagle Signs Wales - From Design to Installation, Signs and Banners";
        $description = "Professional Sign and Banner Design and Installation in Wales";
        $keywords = "Commercial business, Vinyl, Graphics, Designs, Banners, Signs, Sign company, Sign makers, “Carmarthenshire, Pembrokeshire, Llanelli, West Wales";
        break;
    case "vehicle-livery.php":
        $title = "Eagle Signs Wales - From Design to Installation, Vehicle Livery";
        $description = "Quality Vehicle Livery in Wales";
        $keywords = "Professional, Free Quote, van, lorry, vinyl printing, Installation, Carmarthenshire, Pembrokeshire, Llanelli, West Wales";
        break;
    case "colour-printing.php":
        $title = "Eagle Signs Wales - From Design to Installation, Full Colour Printing";
        $description = "Full Colour Printing, from Design to Installation";
        $keywords = "Full Colour Printing, Vinyl, Stationary, Graphic Design, Board Promotions, Free Quote, Carmarthenshire, Pembrokeshire, Llanelli, West Wales";
        break;
    case "design.php":
        $title = "Eagle Signs Wales – Design and Installation of Full Colour Signs and Banners";
        $description = "Professional Sign and Banner Design and Installation West Wales";
        $keywords = "Full Colour Printing, Vinyl, Stationary, Graphic Design, Board Promotions, West Wales, Designs, Designer, Vehicle Printing, Print, Sign Makers, Fleet Livery";
        break;
    case "contact.php":
        $title = "Eagle Signs Wales - From Design to Installation, Carmarthen, Pembrokeshire";
        $description = "Free Online Quote for Sign Printing in Wales";
        $keywords = "Print, Sign Makers, Sign Company, Graphic Designs, Free Online Quote, Carmarthenshire, Pembrokeshire, Llanelli, West Wales";
        break;
    default:
        $title = "Default Title";
        $description = "Default Description";
        $keywords = "Default Keywords";
        break;
    }
    ?>
    
    <title><?=$title?></title>
    <meta name="description" content="<?=$description?>" />
    <meta name="keywords" content="<?=$keywords?>" />
    
    PHP:
     
    Silver89, Jan 31, 2010 IP
  11. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #11
    Ok, thanks a million, it is a really handy script, thanks
     
    oo7ml, Jan 31, 2010 IP