How to make a meta description and meta keywords unique, in a PHP document that only has 1 header for all the pages

-1

Recently I learned how to make a static site a bit dynamic by putting the footer and header into a single document for all pages. But for each page I need a different meta description and meta keywords. How do I do this?

The script I used:

<?php
function carrega_pagina(){
(isset($_GET['p'])) ? $pagina = $_GET['p'] : $pagina = 'home';
if(file_exists('page_'.$pagina.'.php')):
    require_once('page_'.$pagina.'.php');
else:
    require_once('page_home.php');
endif;
}

function gera_titulos(){
(isset($_GET['p'])) ? $pagina = $_GET['p'] : $pagina = 'home';
switch ($pagina):

    case 'contato':
        $titulo = 'Contato - BizarroNEWS';
        break;

    case 'privacidade':
        $titulo = 'Privacidade | BizarroNEWS';
        break;

    case 'ultimasnoticias':
        $titulo = 'Últimas Notícias | BizarroNEWS';
        break;

    default:
        $titulo = 'BizarroNEWS | Home';
        break;

endswitch;
return $titulo;
}

Document index.php:

<?php
require_once('funcoes.php');
require_once('header.php');
carrega_pagina();
require_once('footer.php');
?>

I have 3 documents called header.php , index.php and footer.php .

The header.php has only the header of the page, footer.php only has the footer, and the index.php only has the PHP command that is calling the file funcoes.php (first script that I pasted in the question) header.php and footer.php .

And I have more documents called page_home.php , page_ultimasnoticias.php , page_contato.php and page_privacidade . Within them only the content of the page, without footer and without header.

    
asked by anonymous 27.08.2018 / 18:16

2 answers

3

Just take advantage of what you already have, even though your gera_titulos is more to a get_titulos , I mean, it would be more interesting if the function itself generated everything, up to HTML, and honestly does not even need to separate into functions , after all it will only use once

example would look something like:

  • funcoes.php (for anything you use, you do not need the functions you've created)
  • init.php (will contain what checks the pages and creates the variables)
  • header.php (will generate the goals and title)
  • footer.php (will generate the footer)

init.php

<?php

$pagina = empty($_GET['p']) ? 'home' : $_GET['p'];

switch ($pagina):
    case 'contato':
        $titulo = 'Contato - BizarroNEWS';
        $keywords = 'Fale Conosco, Localização, Endereço';
        $descricao = 'Formulário de contato, mapa e telefones';
        break;

    case 'privacidade':
        $titulo = 'Privacidade | BizarroNEWS';
        $keywords = 'GDPR, Privacy, Privacidade, Proteção de dados';
        $descricao = 'Entenda seus direitos e como usamos seus dados';
        break;

    case 'ultimasnoticias':
        $titulo = 'Últimas Notícias | BizarroNEWS';
        $keywords = 'News, Newsfeed, Notícias';
        $descricao = 'Saiba tudo que acontece no Bizarro';
        break;

    default:
        $titulo = 'BizarroNEWS | Home';
        $keywords = '';
        $descricao = 'Home page';
        $pagina = 'home';
endswitch;

header.php

<html>
<head>
<title><?php echo $titulo; ?></title>
<meta name="keywords" content="<?php echo $keywords; ?>">
<meta name="description" content="<?php echo $descricao; ?>">
</head>
<body>

index.php :

<?php
require_once 'init.php';
require_once 'funcoes.php';
require_once 'header.php';
require_once 'page_' . $pagina . '.php';
require_once 'footer.php';

Of course as you will get everything in index.php you could reduce things well, after all are simple things, do not need these includes all, a lot could be solved there in the index.php same, for example:

index.php

<?php

$pagina = empty($_GET['p']) ? 'home' : $_GET['p'];

switch ($pagina):
    case 'contato':
        $titulo = 'Contato - BizarroNEWS';
        $keywords = 'Fale Conosco, Localização, Endereço';
        $descricao = 'Formulário de contato, mapa e telefones';
        break;

    case 'privacidade':
        $titulo = 'Privacidade | BizarroNEWS';
        $keywords = 'GDPR, Privacy, Privacidade, Proteção de dados';
        $descricao = 'Entenda seus direitos e como usamos seus dados';
        break;

    case 'ultimasnoticias':
        $titulo = 'Últimas Notícias | BizarroNEWS';
        $keywords = 'News, Newsfeed, Notícias';
        $descricao = 'Saiba tudo que acontece no Bizarro';
        break;

    default:
        $titulo = 'BizarroNEWS | Home';
        $keywords = '';
        $descricao = 'Home page';
        $pagina = 'home';
endswitch;
?><html>
<head>
<title><?php echo $titulo; ?></title>
<meta name="keywords" content="<?php echo $keywords; ?>">
<meta name="description" content="<?php echo $descricao; ?>">
</head>
<body>

<?php require_once 'page_' . $pagina . '.php'; ?>

<footer>Rodapé</footer>
</body>
</header>
    
27.08.2018 / 20:24
0

Attention: The example will only serve the case quoted by the author .

You can get the current page being used through the p parameter using $_GET['p'] and perform the verification on an as-needed basis, thereby writing the value to constante to be used on the pages it will contain the meta .

On your index.php page:

  <?php         
    $str = $_GET['p'];  
    switch($str) {
        case 'teste':
        define('TITULO',' o titulo é teste');
        break;
        case 'home':
        define('TITULO',' o titulo é home');
        break;
        default:
        define('TITULO','nenhum titulo');
        break;
    }

    include "teste2.php";
 ?>

In your file containing the metas tags , (in my case, it is the file teste2.php ) would look like this:

<?php 

    echo "Aqui está o titulo em outra pagina:: ".TITULO;
 ?>

 <meta name="description" content="<?php echo TITULO?>" />

COMMENTS:

1 - I used an example of a URL that was quoted by the author: link

2- Note that I imported the file ( include "teste2.php" ) at the bottom of the page index.php .

3- Use DevTools of the browser to see how the meta tag was, in my case it looks like this: link

    
27.08.2018 / 19:08