PHP to place the title generated by the page in <head>

We include the header.php file on all pages of our site. Thus, we could put one heading in the header.php file, which will be applied to the whole site, or have our own heading on each page to be more visual.

The problem is that in this case the header will be outside the headers (which remain in the header.php file) and marked as invalid.

Is there any function that we can use to define a variable ($ pageTitle) on the page that will be displayed in the title bar?

Thank.

+3
source share
4

news.php:

<?
include "config.php"; //connect to database HERE.
$data = getdbdata("SELECT * FROM news where id = %d",$_GET['id']);
$page_title = $data['title'];
$body = nl2br($data['body']);

$tpl_file = "tpl.news.php";
include "template.php";
?>

template.php:

<html>
<head>
<title><?=$page_title?></title>
</head>
<body>
<? include $tpl_file ?>
</body>

tpl.news.php

<h1><?=$page_title?></h1>
<?=$body?>

+5

.....

<?php 
$pageTitle = "Test";
include('header.php');
?>

header.php

<head>
    <title><?php echo $pageTitle; ?> </title>
</head>
+1

, ?

<?php
$defaultPageTitle='Default Title'; //Default title
include('header.php');
?>

<?php
/* You would define $newPageTitle here if necessary
 (i.e. use $_SERVER['REQUEST_URI'] to get the URL
 and check a database for the $newPageTitle */
?>
<head>
<?php
if(isset($newPageTitle)){echo '<title>'.$newPageTitle.'</title>';}
else{echo '<title>'.$defaultPageTitle.'</title>';}
?>
</head>
+1

As I see it, you can still do all this in your header:

<?php
include(...your config/connect file...);
mysql_query(... get page variables ...);
$pageTitle = stripslashes($titlefromDB);
?>
<html><head><title><?php echo $pageTitle; ?></head>

So you end header.php. Now include this on every page you want to use, and follow yours <body></body></html>.

Only one idea, but in any case you will get around this, first you need to connect to your database, check if the page exists if it is set as a variable, and then start constructing html.

0
source

Source: https://habr.com/ru/post/1765164/


All Articles