Dynamic Content - Meta Tags

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have some simple dynamic content pages (included below). I need to
know how to add specific meta tags (ie. description, keywords, ect.),
as well as extra specific header coding to a page. I would like to
have these on the Links page. Is this possible? and how?
// Assign the requested page to a shorter variable for simplicity.
// The value of $_GET["page"] will be obtained from index.php?
$p = $_GET["page"];
// Check for bad requests. This is necessary to prevent the wrong
// from being included. This is a security measure.
if (strpos($p,"..")) {
    // A bad request has been made, exit immediately. Do not proceed
    // with the inclusion.
    //  strpos($p,"..") checks for presence of ".." in the page
    //   ".." can be used to access files in parent directories
    die("Bad page request");
// File to include.
// this will be something like '/www/page.php'. If $p is empty, fall
// main.php. THIS FILE MUST EXIST, otherwise bad things will happen.
if (!$p) $p = "main";
// See if $content_file exists. If it does not, redirect to the
if (!file_exists($content_file)) {
    header("Location: ");
// At this point everything is fine. Set the appropriate title and
// include the files.
// Syntax: $variable = (condition) ? value_if_true : value_if_false;
$title = ($p) ? "$p - My Site!" : "Main page - My Site!";

<title><?=$title ?></title>
<style type="text/css">
body {
    background-color: white;
    color: #333333;
    font-family: verdana;
    font-size: 11px;
[meta description]
[extra javascript special for this page]
This is my links page. Notice there is no header or footer inclusion
in this
file. There are no links here at the moment.

Re: Dynamic Content - Meta Tags

Quoted text here. Click to load it

Be careful with this line! strpos() returns the numeric position of
one string within another, which can be zero (if the contained string
is the beginning). That's where it would be a problem for it to be.
What this line assumes is that strpos() will return false when it
doesn't find it, or a numeric position when it does find it. Any
number other than zero, when casted to a boolean, is true, and zero is
false. So to make sure this line works right, change it to this:
if (strpos($p,"..") !== false) {

!== means not equal to, before type casting.

So anyway, you're in a position where you might find it easier to be
using a templating library. Smarty (smarty.php.net) is standard, but
there are alternatives with (almost) the same syntax which are more
efficient, such as TemplateLite.

Otherwise, you could either search/replace the new meta tags in there
(find the </head> tag, replace with "<meta ... /></head>"), or put
another variable in header.php, like <?=$description ?>, and add that
before or after that <style> element. Example:

<title><?=$title ?></title>
<meta http-equiv="description" content="<?=$description ?>" />
<style type="text/css">
body {
    background-color: white;
    color: #333333;
    font-family: verdana;
    font-size: 11px;

Then you could just assign $header in index.php like you do $title.

By the way, in <style> tags, you don't need the <!-- and -->. Trying
to account for those browsers that less than 10 people on Earth still
use was about as bad of an idea as it was to type all HTML tags in
capital letters (there are way more people that don't speak English,
but do we all account for them? Every language?). If you really want
to do it anyway, you might consider using /*<!--*/ and /*-->*/ instead
so that if you ever need to put your code on one line with an output
buffer (not really efficient unless you are using output caching or
have very little traffic), it won't screw up your CSS.

-Mike PII

Re: Dynamic Content - Meta Tags

Thanks. I got this code off of the internet, so it is not original to
me. Will check out the template deal. That might be more what I am
looking for. Not sure. Thanks again for the detailed reply.


Quoted text here. Click to load it

Site Timeline