I have a header.php and a footer.php file. My HTML header is in header.php and I have an index.php file.
I’m using so (index.php):
require 'header.php';
$example_code = 'example';
︙
require 'footer.php';
And my header.php:
<html>
<head>
<title>
???
<title>
<meta name="description" content="???" />
<meta name="keywords" content="???" />
</head>
<body>
︙
I want to send some data from index.php to header.php to print it there (see the ???). I’m thinking of the header() function but I can’t see any example in the PHP manual.
The best thing you could do is separating logic from presentation. Using an MVC approach, where you take care of all logic in one file, and then display the outcome of what you've done in a presentation only layer.
Besides that, if you want to keep your approach, what you simply have to do is to make assignments before header.php is included. So, suppose you want to change your page title, this is what you need to do:
index.php
<?php
$title = 'My Page Title';
$description = 'My meta description';
$keywords = 'keyword list';
include('header.php');
?>
header.php
<html>
<head>
<title>
<?php echo $title; ?>
<title>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
</head>
<body>
It's as simple as that. Just keep in mind you can't make assignments to a page/script, AFTER such has been included
Again, though, I'm trying to answer you, not necessarily suggesting this approach. If your application has just a couple of pages, that's ok. If it's bigger (or going to be), something like the MVC pattern (two-step view pattern) is a better alternative IMHO.
php header function got nothing to do with html tag "head" .
<?php
$tpTitle="Helping you to improve your web site";
$pgHeading="Site-Report.com - Helping you to improve your web site";
$pgDesc="Helping you to improve your web site";
$pgKeywords="site-report";
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
<title><?php echo $tpTitle ?></title>
<meta name="description" content="<?php echo $pgDesc ?>"></meta>
<meta name="keywords" content="<?php echo $pgKeywords ?>"></meta>
</head>
http://www.cre8asiteforums.com/forums/index.php?showtopic=4558
The header() function is not suitable for what you would like to do. You're merely looking for a variable:
index.php:
$title = 'My Page Title!';
$description = 'This is how I describe it.';
$keywords = 'page, title, describe';
header.php:
<title>
<?php echo htmlspecialchars($title); ?>
<title>
<meta name="description" content="<?php echo htmlspecialchars($description); ?>" />
<meta name="keywords" content="<?php echo htmlspecialchars($keywords); ?>" />
Related
I am trying to get more control over the header of my Joomla site; for some pages I don't need many things in the header. I decided to make a template where I don't use the <jdoc:include type="head" />, because it loads lot of things that I don't need.
Searching, I found this old post about the subject, and in the web some people looking for the same thing. Manually control <head> markup in Joomla
I was wondering if it is possible to add to my index.php template file to PHP code that could get just the "metadescription" and the "title" of the Joomla publication. Something like this:
<?php defined( '_JEXEC' ) or die; ?>
<!doctype html>
<html lang="<?php echo $this->language; ?>">
<head>
<meta name="viewport" content="width=device-width />
<meta name="description" content="<?php echo **code metadescription** ?>" />
<title><?php echo **code to get title** ?></title>
</head>
<body>
<jdoc:include type="component" />
</body>
</html>
Nice, after while i could find the code that i was looking for, and maybe it could help others, it worked for me... in the index.php file of the template i added:
<?php defined( '_JEXEC' ) or die;
$doc =JFactory::getDocument();
$meta_description = $doc->getMetaData("description");
$title = $doc->getTitle();
?>
<!doctype html>
<html lang="<?php echo $this->language; ?>">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<meta name="description" content="<?php echo "$meta_description"; ?>" />
<title><?php echo "$title" ?></title>
</head>
<body> <jdoc:include type="component" /> </body>
</html>
Just use PHP include() function
In top.php
<meta name="viewport" content="width=device-width />
<meta name="description" content="<?php echo **code meta description** ?>" />
<title><?php echo **code to get title** ?></title>
And in your current file just include the file(top.php) like
<?php defined( '_JEXEC' ) or die; ?>
<!doctype html>
<html lang="<?php echo $this->language; ?>">
<head>
<?php include("top.php"); ?>
</head>
<body>
<jdoc:include type="component" />
</body>
</html>
I don't know if this is a good way, but you can unset all css and js in the following style:
unset($doc->_styleSheets[$this->baseurl.'/path/to/some.css']);
unset($doc->_scripts[$this->baseurl.'/path/to/some.js']);
I recommend not to remove meta tage like content-type or x-ua-compatible. These tags support your website in some browsers. And the favicon link is helpful by bookmarks.
I work with PHP includes, and I need to put HEAD information in one of them. Is this possible, or can I only put a HEAD section on top of the index.php?
I'm asking this because the PHP includes has queries which I need in order to get OG image data (for social media) into the head. For example: I have a file WEBSHOP.PHP and in this file there is a product with an image. I want that image to show on the timeline in FaceBook.
This is an example of my (shortened version) of index.php:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<? include webshop.php; ?>
</body>
This is an example of my (shortened version) of webshop.php:
<!-- some mysql query to get variables as $pic and $row->meta_title -->
<head>
<meta property="og:image" content="http://forteuitgevers.nl/images/boeken/<? echo $pic; ?>" />
<meta property="og:title" content="<? echo $row->meta_title; ?>" />
<meta property="og:description" content="<? echo $row->meta_des; ?>" />
<meta property="og:url" content="http://<? echo $_SERVER['HTTP_HOST']; ?>/<? if (!empty($url_array[1])) { echo $url_array[1]; echo '/' ; } ?><? if (!empty($url_array[2])) { echo $url_array[2] ; } ?>" >
</head>
<!-- some code to view the webshop item -->
You're going to have to change the structure of your PHP files a bit in order to get all the header tags into one <head> section. If you include the webshop.php file before you start generating your HTML output you can then access the PHP variables when you write the head section. Something like this:
index.php:
<?php include webshop.php; ?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<meta property="og:title" content="<?php echo $row->meta_title; ?>" />
<!-- other meta tags using variables from webshop.php -->
</head>
<body>
<!-- print out HTML code from webshop.php -->
<?php echo $doc_body; ?>
</body>
Then in webshop.php you'll have to save any HTML output with output buffering so you can add it into the HTML code in the proper place. Something like this:
<?php
// sql queries to get data
ob_start();
?>
<!-- html code to show up in the body section to view webshop items -->
<?php
$doc_body = ob_get_clean();
?>
Check out the PHP.net manual page on Output buffering for more info on ob_start and ob_get_clean.
Yes you can. However this is bad style. And you are making your HTML wrong:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<? include webshop.php; ?>
</body>
this will lead into
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<head>
<meta property="og:image" content="http://forteuitgevers.nl/images/boeken/<? echo $pic; ?>" />
<meta property="og:title" content="<? echo $row->meta_title; ?>" />
<meta property="og:description" content="<? echo $row->meta_des; ?>" />
<meta property="og:url" content="http://<? echo $_SERVER['HTTP_HOST']; ?>/<? if (!empty($url_array[1])) { echo $url_array[1]; echo '/' ; } ?><? if (!empty($url_array[2])) { echo $url_array[2] ; } ?>" >
</head>
</body>
However HTML does not like that the head tag is inside of the body tag. But most browser will still show it correctly.
To be sure: Check your result with a HTML Validator.
ok so I've created this website and want to convert it to php just for fun. The website structure looks like any 'normal' web structure. like this:-
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body class="fish">
</body>
</html>
ok so i included from the head to the beginning of the body tag in header.php file. so header.php looks like this:- `
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body class="fish">`
now here is the problem. Each page should have it's own title, body class! and each page will also obviously have it's own meta description and content. How will I accomplish this guys? I was think of creating a function that base the meta description and body class on the page title. But is there a smatter way to accomplish this? Thanks
Either use a template engine or a MVC framework (such as CakePHP or CodeIgniter) which have template engines already incorporated in them.
Inside your header.php do something like this:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $_tpl['title'] ?></title>
<meta name="description" content="<?php echo $_tpl['meta_desc'] ?>">
</head>
<body class="<?php echo $_tpl['body_class'] ?>">
On your page, before you use include('header.php'), define the vars as follows:
$_tpl = array();
$_tpl['title'] = 'My Title';
$_tpl['meta_desc'] = 'My meta description.';
$_tpl['body_class'] = 'fish';
As others have said though, don't re-invent the wheel. You'd be better to investigate some of the already-established templating engines for PHP:
Smarty
RainTPL
You should be creating a template to do this if it will be dynamic. You have many options on how you with to pass the data, whether it be a database, an object, array, etc. It is really tough to generate the data based on the page title, unless you are using a very persistent format to title each page.
<head>
<meta property="og:title" content="<?= $values['title'] ?>" />
<meta property="og:type" content="website" />
<meta property="og:url" content="<?= $values['url'] ?>" />
<meta property="og:image" content="<?= $values['image'] ?>/>
<meta property="og:site_name" content="<?= values['name'] ?>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="ROBOTS" content="NOODP">
<link rel="icon" type="image/png" href="<?= $values['image'] ?>" />
<title><?= $values['title'] ?></title>
<? if(isset($values['css'])) : ?>
<? foreach($values['css'] as $css) : ?>
<link href="/css<?= $css['data'] ?>" rel="stylesheet" type="text/css" />
<? endforeach ?>
<? endif ?>
<? if(isset($values['js'])) : ?>
<? foreach($values['js'] as $js) : ?>
<script src="/js<?= $js['data'] ?>" type="text/javascript"></script>
<? endforeach ?>
<? endif ?>
</head>
I know that that this topic has been elaborated on many times, but I can still not figure out how to do it. Maybe My site structure is not like it should; I don't know.
Anyway, I have the following:
An Index.php containing a heading in which I have :
<meta name="description" content="<?php echo $pg_desc; ?>"/>
<meta name="keywords" content="<?php echo $pg_key; ?>"/>
This index.php is also containing all the referenced stylesheets and jquery code.
Then I have all content of the pages in SQL. Each page in my database is formatted as follows (as a longtext):
<?php ##commented out page name just for my own convenience ?>
<div class="#"
body
</div>
Now I thought I had to put
$pg_desc = "my page is about"
and
$pg_key = "you,name,it"
in the php part, but that did not seem to work. Can anyone help me?
You would need to declare that variables before outputting them to meta tags.
For example:
<?php
$pg_key = "you,name,it";
$pg_desc = "my page is about";
?>
<meta name="description" content="<?php echo $pg_desc; ?>"/>
<meta name="keywords" content="<?php echo $pg_key; ?>"/>
How can I reference variables from an included file before it's been included? Or can I somehow include the file (so I can lead its variables later) before its HTML is literally inserted into the body tag? Or can I contain all of home's body content in one big variable that I can echo as well in the index?
Here's what I'm trying to do:
index.php
<html>
<head>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
home.php
<?php
$title="home page";
$description="this is the home page";
$keywords="home, awesome, yes";
?>
this is the home page content that gets inserted into the body!
Just move the include statement to the top of the file.
This will expose all values, functions and variables to all subsequent lines.
<?php include 'home.php'; ?>
<html>
<head>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
</head>
<body>
</body>
</html>
Short answer version: You can't. You'll get an 'Undefined variable' notice if you do that.
I find it is usually much more convenient to have a header.php (and a footer.php for that matter) which gets included in the index, home, contact or whatever other file. The advantage is that you don't have redundant code, and if you need to make a modification in the header or footer, you need to only modify one file.
So for example, 'about_us.php' would look like:
<?php
include('path/to/header.php');
#body goes here
include('path/to/footer.php');
?>
And your header would be something like:
<?php
$title = ucfirst(str_replace('_', ' ', substr(basename($_SERVER['PHP_SELF']), 0, -4));
?>
<html>
<head>
<title><?php echo $title; ?> page</title>
<meta name="description" content="this is the home page" />
<meta name="keywords" content="home, awesome, yes" />
</head>
<body>
The $title variable will be the file name, minus the extension, with all underscores replaced by spaces and the first letter of the first word capitalized. So basically about_us.phpwould be converted into "About us". This is not necessarily a general solution, but I gave it as an example keeping in mind that you wanted to use a dynamic title in your original example. For dynamic description and keywords, based on the file name you could also assign different values with the help of a switch() statement.
UPDATE:
Another solution, although kind of the reverse of what you're asking, but at the same time much closer to what you're looking for would be to write the header.php like
<html>
<head>
<title><?php echo $title; ?> page</title>
<meta name="description" content="<?php echo $desc; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
</head>
<body>
... the footer like ...
</body>
</html>
... and then include them in your other files:
<?php
$title = 'Your title';
$desc = 'Your description';
$keywords = 'The, big, brown, fox, jumps, over, the, lazy, dog';
include('path/to/header.php');
?>
<!-- body goes here -->
<?php
include('path/to/footer.php');
?>
This way, you are assigning all the variables BEFORE you are including the files in which they are being referenced, you have distinct files for all the links and you don't need fancy switches. Also as a side note, wrapping the body's HTML in PHP is simply bad practice. Try to keep the HTML separated from the PHP as much as possible in general. It will help both you, and whoever is going to do work on the code in the future.
Hope this helps !
I would have a look at using a template system. Separating your code from the content will save you a lot of trouble in the future. it will also allow you to change the html template easily in the future. plus you can see your template without having to run the php code.
have a look at smarty templates
http://www.smarty.net/
you would then build a template file: "template.tpl"
<html>
<head>
<title>{$title}</title>
<meta name="description" content="{$description}" />
<meta name="keywords" content="{$keywords}"/>
</head>
<body>
{$home_content}
</body>
</html>
and some php code to run:
<?php
require_once('Smarty.class.php');
$smarty = new Smarty();
$smarty->assign('title' , 'Your title');
$smarty->assign('description' , 'Your description');
$smarty->assign('keywords' , 'The, big, brown, fox, jumps, over, the, lazy, dog');
$smarty->assign('home_content' , 'this is the home page content that gets inserted into');
$smarty->display('template.tpl');
?>
And that is just scratching the surface of what a templating system can do. you can repeating or optional bocks, include other templates, etc etc.