How to change the title of each PHP page dynamically?

17

I do not understand almost anything about PHP. Once a friend sent me a code that allowed me to separate my page into several parts and then get those parts back in case I change anything in the scripts and style sheets so I do not need to go page by page to change it again , this is include_once of PHP.

Finally, I remember that I've read an article that said it's a good practice to add a descriptive title for each page of our site, for example "My site - Photoshop Tutorial" p>

Based on these two pieces of information, I came across a problem. I separated my site in two parts the header and footer, and just change main with the information I want:

<?php   
include_once "./includes/header.php";   
<!-- AQUI VAI MEU CONTEÚDO -->  
include_once "./includes/footer.php"; 
?>

Only <title> of the page is in the header (which is the default of all pages), and ends up being unique for the whole site. Thinking back home I found a solution, which is to use JavaScript to modify the title. I have a practice that is always put an H1 with the main subject of the site before starting the content, for example <h1>Contato</h1> . The question is, does it influence something on the site? Be it in SEO or anything else? Is there any better way to do it?

This is the code I use to change the title:

var title = $('main').first('h1').text();
$('title').text('Meu site -' + title);
    
asked by anonymous 16.03.2014 / 20:25

3 answers

16

The ideal would be to have the title stored in a variable at the top of PHP, and use the value of this variable in both <title> and <h1> .

For example, the code of a page would look like this:

<?php
$titulo = "Meu site - Tutorial Photoshop";
include_once "./includes/header.php";   
?>

<h1><?php echo $titulo ?></h1>
etc.

<?php
include_once "./includes/footer.php"; 
?>

And header.php would contain something like this:

<!DOCTYPE HTML>
<html>
<head>
...
   <title><?php echo $titulo ?></title>
...

In terms of SEO, it is rather preferable to have <title> defined by PHP rather than by JS, since several search bots disregard JS.

    
16.03.2014 / 22:11
5

Another interesting way would be to centralize all the work in header.php , defining the titles of each page using an array, for example:

$titulos = [
    'index.php' => 'Meu site - Tutorial Photoshop',
    'tutoriais.php' => 'Tutoriais de Photoshop - Meu Site',
    'ajuda.php' => 'Página de ajuda - Meu Site',
    'contato.php' => 'Fale Conosco - Meu Site'
    #pode ter 'n' páginas
];

#limpa a url (ex.: /url.php?foo#bar => url.php)
$uri = str_replace("/","",explode(".php",$_SERVER[REQUEST_URI])[0]).".php";

And in the title you put:

<h1><?php echo $titulos[$uri] ?></h1>

Basically what the code does is create a "bank" with all the titles, and it prints the current title according to the URL that is detected.

In this way your code becomes more centralized and makes maintenance easier.

    
17.03.2014 / 15:17
0
$titulos = [
    'index.php' => 'Meu site - Tutorial Photoshop',
    'tutoriais.php' => 'Tutoriais de Photoshop - Meu Site',
    'ajuda.php' => 'Página de ajuda - Meu Site',
    'contato.php' => 'Fale Conosco - Meu Site'
    #pode ter 'n' páginas
];

clears the url (eg: /url.php?foo#bar => url.php)

$uri = str_replace("/","",explode(".php",$_SERVER[REQUEST_URI])[0]).".php";

You can use this method above, but this only works if it is in the root folder of the project, if you are testing on the server in hidden folder it will not work then switch str_replace("/","",explode(".php",$_SERVER[REQUEST_URI])[0]).".php"; by end(explode("/", $_SERVER['PHP_SELF'])); , because regardless of where the project is, it will always return the name of the file accessed.

It will look like this:

$titulos = [
    'index.php' => 'Palestra Clube',
    'contato.php' => 'Contato',
    'sobre.php' => 'Sobre'
    #pode ter 'n' páginas
];

$uri = end(explode("/", $_SERVER['PHP_SELF']));

clears the url (eg: /url.php?foo#bar => url.php)

echo $titulos[$uri];
    
18.12.2015 / 14:18