By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,426 Members | 3,407 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,426 IT Pros & Developers. It's quick & easy.

Dynamic Content - Meta Tags

P: n/a
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?
------------------------------------------------------------------------------------------------------------------------------
index.php
---------------
<?php
// Assign the requested page to a shorter variable for simplicity.
// The value of $_GET["page"] will be obtained from index.php?
page=RIGHT_HERE
$p = $_GET["page"];
// Check for bad requests. This is necessary to prevent the wrong
files
// 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
request.
// ".." 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
back
// main.php. THIS FILE MUST EXIST, otherwise bad things will happen.
if (!$p) $p = "main";
$content_file=$_SERVER["DOCUMENT_ROOT"]."/".$p.".php";
// See if $content_file exists. If it does not, redirect to the
homepage.
if (!file_exists($content_file)) {
header("Location: {$_SERVER["PHP_SELF"]}");
exit();
}
// At this point everything is fine. Set the appropriate title and
then
// include the files.
// Syntax: $variable = (condition) ? value_if_true : value_if_false;
$title = ($p) ? "$p - My Site!" : "Main page - My Site!";
include("header.php");
include($content_file);
include("footer.php");
?>

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

May 25 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On May 25, 6:34 am, "thetechturf.com" <BryanLBurkhol...@gmail.com>
wrote:
// Check for bad requests. This is necessary to prevent the wrong
files
// from being included. This is a security measure.
if (strpos($p,"..")) {
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 </headtag, replace with "<meta ... /></head>"), or put
another variable in header.php, like <?=$description ?>, and add that
before or after that <styleelement. Example:

header.php
----------------
<html>
<head>
<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;
}
</style>
--------------------------------------------------------------------------------------------

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

By the way, in <styletags, 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

May 25 '07 #2

P: n/a
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.

Bryan

On May 25, 4:20 pm, Mike P2 <sumguyovrt...@gmail.comwrote:
On May 25, 6:34 am, "thetechturf.com" <BryanLBurkhol...@gmail.com>
wrote:
// Check for bad requests. This is necessary to prevent the wrong
files
// from being included. This is a security measure.
if (strpos($p,"..")) {

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 </headtag, replace with "<meta ... /></head>"), or put
another variable in header.php, like <?=$description ?>, and add that
before or after that <styleelement. Example:

header.php
----------------
<html>
<head>
<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;}

</style>
--------------------------------------------------------------------------------------------

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

By the way, in <styletags, 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

May 25 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.