AM new to php, but i included my php header to all page of my website, but thay are all showing the same html meta tags and I want them to show display meta tags per page, like showing meta title For Home and Meta title for our services. I know that I can store them in an array but don't know how to go about it. Need help.

Dani AI

Generated

Good call, — storing per-page meta is the right direction. Two simple, reliable patterns that avoid duplicate meta across pages:

  • Per-page variables set before including the shared header (easy and explicit).
  • A central associative array (meta map) in the header keyed by filename or route, with per-page overrides.

Example: per-page variables (page top + shared header)

// page (home.php)
<?php
$meta_title = 'Home - Example Site';
$meta_description = 'Short homepage description.';
include 'header.php';
?>

// header.php
<?php
$meta_title = $meta_title ?? 'Example Site - Default Title';
$meta_description = $meta_description ?? 'Default description for the site.';
function h($s){ return htmlspecialchars($s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); }
?>
<title><?php echo h($meta_title); ?></title>
<meta name="description" content="<?php echo h($meta_description); ?>">

Example: central map keyed by filename

// header.php (or a meta helper)
<?php
$meta_map = [
  'index.php'    => ['title'=>'Home - Example','description'=>'Home desc'],
  'services.php' => ['title'=>'Services - Example','description'=>'Services desc'],
];
$current = basename($_SERVER['SCRIPT_NAME']);
$page = $meta_map[$current] ?? [];
$meta_title = $meta_title ?? ($page['title'] ?? 'Example Site');
$meta_description = $meta_description ?? ($page['description'] ?? 'Default desc');
?>

Notes and cautions: always escape output (shown above). Keep titles unique and concise (roughly <=60 chars) and descriptions useful (50–160 chars). For dynamic content (blog posts, products) pull meta from the DB by slug. If using a front controller, key metadata by route instead of script name. ’s suggestion to share code helps diagnose include-order or scope issues; ’s status note confirms the array approach worked here.

Recommended Answers

All 3 Replies

Member Avatar for Member #120589

Is this solved? It says so, but no explanation

I figured it out, by putting my meta tags in an array. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.