0

I have a commom php header. I would like to add page specific META.

Currently my header looks like this.

<?php include ('includes/pageheader.php'); ?>

It currently uses a long if statemment

<?php $page =  $_SERVER['REQUEST_URI']; 
if($page == "/") {
  ?>
<!-- Metadata for home -->
?>

I was wondering if there was a better way or a class to be able to build up page specific meta to this header.

3
  • 1
    Try Switch case Commented Mar 14, 2017 at 13:13
  • 1
    If each of your routes/URI's is stored in a database, you can look up each pages metadata based on the request. Commented Mar 14, 2017 at 13:22
  • if you can create header files like homeheader.php , mypage1.php, my page2.php, & if you can identify the exact page, you can use : <?php include ('includes/'.$page.'.php'); ?> you can avoid lots of if else statements Commented Mar 14, 2017 at 13:52

1 Answer 1

2

In your header, you could do something like this.

<?php if(isset($header)): ?>
  <?php foreach($header as $tag): ?>
    <?= $tag ?>
  <?php endforeach ?>
<?php endif ?>

Then above your include, you could add.

<?php
$header[] = '<meta charset="UTF-8">';
$header[] = '<meta name="description" content="Free Web tutorials">';
$header[] = '<meta name="keywords" content="HTML,CSS,XML,JavaScript">';
$header[] = '<meta name="author" content="John Doe">';
$header[] = '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
include ('includes/pageheader.php');
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.