How to move <title> inside the <head>? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In my website I have the same header for all pages. So I use the include function for the header. This includes all the <head></head>.
Because the <title> is variable depending on the page, I have put it just after the <body>.
I know this is wrong. How can I fix this and move the variable <title> inside the standard <head> ?
Thank you

In the code that you include for the header, use a placeholder for the title:
<head>
<title><?php echo $pageTitle; ?></title>
...
</head>
Then, in your page-specific code, just before the include statement, populate the $pageTitle variable:
$pageTitle = "Your Title";
include...
To make your included code more robust, consider setting a default title if none is provided:
<title><?php echo isset($pageTitle)? $pageTitle: 'Default Title'; ?></title>

Set a title variable before including header:
$title = 'Whatever you want';
include('header.php');
//rest of your page
Inside header.php:
<head>
<title><?php echo $title ?></title>
<!-- the rest of your stuff -->
</head>

Put a variable inside of your header file for your title, then define that value in your page before you include the header:
Inside your include:
<html>
<head>
<title><?= $page_title; ?></title>
Inside your main file:
<?php
$page_title = 'My page title';
include('header.php');

Make your included file only contain the contents of the <head> tag, and not the tag itself.
This way you can include the file between <head></head> tags and also have the title differ for each page.

Possibly set the page title in a variable ahead of your include, and have this in the head:
<title><?php echo htmlspecialchars($pageTitle, ENT_QUOTES); ?></title>

You can add a PHP variable above the header include such as
$title = 'test page';
Then in the header you use
<title>Main site title | <?php isset($title) ? $title : 'No title set' ?>

Depending on how you have things set up, you may have to set the title before you call your include.
PHP:
<?php
$title = "My Page Title";
include('path-to-your-header.php);
Your header would look something like this then:
PHP:
<html>
<head>
<title="<?php echo($title); ?>">
...
</head>
Or, if you are using a framework (codeigniter for example) you can use a tempalte - then in your controller method(s) you can set the page title.
PHP:
<?php
// Controller
public function index()
{
$data['page_title'] = 'Your Page Title';
$data['main_content'] = 'home'; // page content
$this->load->view( 'templates/public', $data ); // page template
}
// Tempalte
<html>
<head>
<title><?php echo ($page_title !== '' ) ? $page_title . ' | ' . SITE_NAME : '' . SITE_NAME; ?></title>
...
</head>
<body>
<?php echo $this->load->view( 'header' ); ?>
<?php echo $this->load->view( $main_content ); ?>
<?php echo $this->load->view( 'footer' ); ?>
</body>

in page.php
<?php
$title = 'This title';
include('header.php');
?>
in header.php
<html>
<head>
<title><?php echo isset($title) ? $title : '' ?></title>
</head>
<!-- rest of code goes below here -->

Related

Print name of current page through included <head> file

So I would like to print name of the current page in the title tag of the head.
I include my head in every page like this:
include 'includes/head.php';
This is my head:
<head>
<?php $page = basename(__FILE__, '.php'); ?>
<title><?php echo ucfirst($page); ?><title>
</head>
I thought this would work, but now it just shows "Head" on every page.
I know I can make it work by just putting the $page variable on every page but I would like to prevent this.
So is there any way to print the name of the current page through the included head.php file without adding anything to every page?
Thanks
EDIT
This is how I fixed it:
$page = pathinfo($_SERVER['SCRIPT_NAME'],PATHINFO_FILENAME);
If you were to create a file head.php with the following content
<?php
$xpage = pathinfo( $_SERVER['SCRIPT_NAME'],PATHINFO_BASENAME );
$ypage = pathinfo( $_SERVER['SCRIPT_NAME'],PATHINFO_FILENAME );
echo "
<!--
with extension
{$xpage}
without extension
{$ypage}
-->";
?>
and include in your regular php pages using include '/path/to/head.php' you should get the desired result ~ you will see two options - with or without file extension.
To add this to the document title simply echo whichever option is preferrable
<title><?php echo $ypage;?></title>
Try this enclosing the $page variable in php tags:
<head>
<?php $page = basename(__FILE__, '.php'); ?>
<title><?php echo ucfirst($page); ?><title>
</head>
You have several problems, first one is curly bracket in front of the echo, the second one is that end title tag is missing forward slash and probably last one is that page variable is not inside php tags...
So your code should look like:
<?php $page = basename(__FILE__, '.php'); ?>
<title><?php echo ucfirst($page); ?></title>
You could use a function like this:
head.php
<?php
function head($page) {
echo "<head>";
echo "<title>".ucfirst($page)."<title>";
echo "</head>"
}
index.php
<?php
include 'includes/head.php';
$page = basename(__FILE__, '.php');
head(page);

Returning Values php

All i know from the return statement (based on JS) that is stops execution of the script and return the value and whats after it ignored, then how is that possible
(in fact required) in this php code :
<?php
return "<!DOCTYPE html>
<html>
<head>
<title>$title</title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'/>
</head>
<body>
$content
</body>
</html>";
which is completing this :
<?php
error_reporting( E_ALL );
ini_set( "display_errors", 1 );
$title = "Test title";
$content = "<h1>Hello World</h1>";
$page = include_once "templates/page.php";
echo $page;
and also is it possible to have return outside a function ????
You were almost there...
With the template/page.php you don't need to run a return all you need to do is as follows:
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'/>
</head>
<body>
<?php echo $content; ?>
</body>
</html>
Then within the PHP page side of things lets say for example, phpCode.php:
<?php
error_reporting( E_ALL );
ini_set( "display_errors", 1 );
$title = "Test title";
$content = "<h1>Hello World</h1>";
include_once "templates.php";
Include will pull all of the information from the file you're including generally.
You can pull information from anywhere providing you have required the php file. PDO / OOP is a great example of how to do this and I'd highly suggest learning it somewhat more to achieve what you're trying to do (from what I could make out in the question).

Changing title based on included file in PHP

I have to make some changes on my old website where I'm not using any templating system. I'm loading the content for some pages from a database based on ?page parameter. So I have something like this:
<title>Page title</title>
...
...
...
$page_id = $_GET['page'];
include 'page.php'; //escaping is done in this file
Inside the page.php file I'm actually loading the information about the page. Based on this information I have to change the title of the main page.
I know that this design is not good at all and I wouldn't do this way these days, but to change everything on this website would be too complicated.
Thank you for your ideas.
Try to add php code before title
<html>
<?php
$page_id=$_GET["page"];
include('page.php');
echo "<title>".$page_title."</title>";
?>
<body></body></html>
Inside page.php:
echo '<script>
document.title = "This is the new page title.";
</script>';
How to dynamically change a web page's title?
Enjoy !
<?php
$page_id = $_GET['page'];
if ($page_id == 'first value') {
$title = 'first title';
} else {
$title = 'second title';
}
?>
<title><?php echo $title?></title>
...<?php
include 'page.php'; //escaping is done in this file

Php - Title in Variable (not working)

I'm using include header in all of my php pages and want to extract each page title and description from a variable , this idea is working on php 5.4 at my local pc (wamp) , but on my host where php 5.2.17 is installed , its not showing title of any page ??
page.php:
<?php
include("header.php");
$title = "Page title";
?>
header.php:
<title><?php echo $title; ?></title>
any help please ??
You're displaying the value of $title before the assignment.
page.php:
<?php
$title = "Page title";
include("header.php");
?>
header.php:
<title><?php echo $title; ?></title>
You're trying to echo an undefined variable. The value of $title isn't declared but you're trying to echo it in header.php. You need to do it like this instead:
header.php:
<?php
$title ="Page title";
?>
page.php:
<?php
include("header.php");
<title><?php echo $title; ?></title>
?>
Hope this helps!
Your variable has not been assigned before you try and echo it in your include.
Change page.php to:
<?php
$title = "Page title";
include("header.php");
?>
(or probably move the variable $title assignment to header.php, making it more organised)
<?php
$title = 'title';
echo '<title>'.$title.'</title>';
?>

Dynamic Title Tag in PHP

I am trying to dynamically populate the title tag on a website. I have the following code in my index.php page
<?php $title = 'myTitle'; include("header.php"); ?>
And the following on my header page
<title><?php if (isset($title)) {echo $title;}
else {echo "My Website";} ?></title>
But no matter what I do, I cannot get this code to work. Does anyone have any suggestions?
thanks
This works (tested it - create a new folder, put your first line of code in a file called index.php and the second one in header.php, run it, check the title bar).
You should double check if those two files are in the same folder, and that you're including the right header.php from the right index.php. And ensure that $title is not being set back to null somewhere in your code.
Learn more about Variable Scope here.
Edit: Examples of visible changes would be:
TEST1<?php $title = 'myTitle'; include("header.php"); ?>
<title>TEST2<?php if ...
Are you including the header file before or after you set the title variable? If you're including it before, then of course it won't be set.
if you're doing something like this in your index.php:
<?php
include('header.php');
$title = "blah blah blah";
?>
then it won't work - you include the header file and output the title text before the $title variable is ever set.
try to declare the variable before using it
$title = '123';
require 'includes/header.php';
Hi Try this old school method ..
In your Header file (for e.g. header.php)
<?php
error_reporting(E_ALL);
echo '<!DOCTYPE html>
<!--[if IE 7 ]><html class="ie7" lang="en"><![endif]-->
<!--[if IE 8 ]><html class="ie8" lang="en"><![endif]-->
<!--[if IE 9 ]><html class="ie9" lang="en"><![endif]-->
<!--[if (gte IE 10)|!(IE)]><!-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<!--<![endif]-->
<head>';
?>
<?php
if($GLOBALS['title']) {
$title = $GLOBALS['title'];
} else {
$GLOBALS['title'] = "Welcome to My Website";
}
if($GLOBALS['desc']) {
$desc = $GLOBALS['desc'];
} else {
$desc = "This is a default description of my website";
}
if($GLOBALS['keywords']) {
$keywords = $GLOBALS['keywords'];
} else {
$keywords = "my, site, key, words";
}
echo "\r\n";
echo "<title> ". $title ." | MyWebsite.com </title>";
echo "\r\n";
echo "<meta name=\"description\" content='". $GLOBALS['title']."'>";
echo "\r\n";
echo "<meta name=\"keywords\" content='".$GLOBALS['title']."'>";
echo "\r\n";
?>
In you PHP Page file do like this (for example about.php)
<?php
$GLOBALS['title'] = 'About MyWebsite -This is a Full SEO Title';
$GLOBALS['desc'] = 'This is a description';
$GLOBALS['keywords'] ='keyword, keywords, keys';
include("header.php");
?>
I assume your header is stored in a different file (could be outside the root directory) then all the above solutions will not work for you because $title is set before it is defined.
Here is my solution:
in your header.php file you need to set the $title to be global by: global $title; then echo it in your title so:
<?php global $title; ?>
<title><?php echo isset($title) ? $title : "{YOUR SITE NAME}"; ?></title>
Then in every page now you can define your title after you have included your header file so for example in your index.php file:
include_once("header.php");
$title = "Your title for better SEO"
This is tested and it is working.
We can also use functions and its a good way to work on real time web sites.
Do simple:
create an index.php file and paste these lines:
<?php include("title.php");?>
<!doctype html>
<html>
<head>
<title><?php index_Title(); ?></title>
<head>
</html>
-- Then
Create a title.php file and paste these lines:
<?php
function index_Title(){
$title = '.:: itsmeShubham ::.';
if (isset($title)){
echo $title;
}else{
echo "My Website";
};
}
?>
It will work perfectly as you want and we can also update any title by touching only one title.php file.
<?php
echo basename(pathinfo($_SERVER['PHP_SELF'])['basename'],".php");
?>
This works. Since I'm using PHP I don't check for other extensions; use pathinfo['extension'] in case that's required.
You can achieve that by using define(); function.
In your header.php file add following line :
<title><?php echo TITLE; ?></title>
And on that page where you want to set dynamic title, Add following lines:
EX : my page name is user-profile.php where I want to set dynamic title
so I will add those lines that page.
<?php
define('TITLE','User Profile'); //variable which is used in header.php
include('header.php');
include('dbConnection.php');
?>
So my user-profile/.php file will be having title: User Profile
As like this you can add title on any page on your site
Example Template.php
<?php
if (!isset($rel)) {$rel = './';}
if (!isset($header)) {
$header = true;
?><html>
<head>
<title><?php echo $pageTitle; ?></title>
</head>
<body>
<?php } else { ?>
</body>
</html><?php } ?>
Pages Your Content
<?php
$rel = './'; // location of page relative to template.php
$pageTitle = 'This is my page title!';
include $rel . 'template.php';
?>
Page content here
<?php include $rel . 'template.php'; ?>
I'm using your code in my project and it works properly
My code in header:
<title>
<?php
if (isset($title)) {echo $title;}
else {echo "عنوانی پیدا نشد!";}
?>
</title>
and my code in index.php:
<?php
$title = "سرنا صفحه اصلی";
include("./include/header-menu.php");
?>

Categories