How to change the title of each PHP page dynamically?

Asked

Viewed 9,289 times

17

I know almost nothing about PHP. Once a friend sent me a code that allowed me to separate my page into several parts and then pick up those parts again in case I change something in the scripts and style sheets, so I don’t need to go page by page to change it again, it’s about the include_once of PHP.

Anyway, I remember I once read an article that said it’s good practice to add a title descriptive for each page of our site, for example "My site - Photoshop Tutorial".

Starting from this two information, I came across a problem. I separated my site into two parts header and footer, and I just change the main with the information I want:

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

Only that the <title> of the page is in the header (which is standard of all pages), and ends up being unique to the entire site. Thinking here at 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 anything on the site? Whether it’s SEO or anything else? Is there a 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);
  • You can put here the code you are using to call/open new pages?

  • I guess that’s what you wish? <?php&#xA; include_once "./includes/header.php";&#xA; <!-- AQUI VAI MEU CONTEÚDO -->&#xA; include_once "./includes/footer.php";&#xA;?>

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 that variable both in <title> how much in the <h1>.

For example, the one-page code 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 the header.php would contain something like this:

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

In terms of SEO, it is preferable to have the <title> defined by PHP and not by JS, as several search robots disregard JS.

5

Another interesting way would be to centralize all the work on 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 titles, and it prints the current title according to the URL that is detected.

This way your code gets more centralized and facilitates maintenance.

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
];

clear the url (e.g.: /url.php? foo#bar => url.php)

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

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

Will stay like this:

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

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

clear the url (e.g.: /url.php? foo#bar => url.php)

echo $titulos[$uri];
  • Hello @Alexandrefelix, a tip, use the format bars, this helps the user a lot when reading your answer. Take the tour for more questions http://answall.com/tour

Browser other questions tagged

You are not signed in. Login or sign up in order to post.