I would like to write a function to return a string with html code to customize head title, description and keywords for multiple pages. I started with my index.php file and two auxiliary, _head.php and _functions.php. What do I have to do to implement this function?
index.php:
<?php include "_functions.php; ?>
<html>
<?php echo make_head("My title", "My description", "My keywords); ?>
<body>
...
</body>
</html>
_functions.php:
function make_head(title, description, keywords) {
return file_get_contents("_head.php");
}
_head.php
<head>
...
<meta name="description" content="$description">
<meta name="keywords" content="$keywords">
<title>$title</title>
...
</head>
_functions.php:
function make_head($title, $description, $keywords) {
$head = include "_head.php";
return $head
}
_head.php
<head>
...
<meta name="description" content="<?php echo $description; ?>" >
<meta name="keywords" content="<?php echo $keywords; ?>" >
<title><?php echo $title; ?></title>
...
</head>
You can use include.
In your function make_head, you can do something like this:
function make_head(title, description, keywords) {
$html = include "_head.php";
return $html;
}
When you include something, it loads it to your current state. So if you use $title or $description or $keywords in your _head.php file, they will be in the same scope and they can be used.
This code has a number of vulnerabilities and including files like this is never a good idea. I can see this code is only entry level though and probably just for practice so to get this working you need to do something like the following:
<?php include "_functions.php; ?>
<html>
<?php echo make_head("My title", "My description", "My keywords); ?>
<body>
Then in your make_head function, return the HTML code
function make_head(title, description, keywords) {
return "<head>
...
<meta name='description' content=' . "$description" .'>
<meta name='keywords' content=' . "$keywords" . '>
<title>$title</title>
...
</head>";
}
This approach leave you open and I certainly wouldn't use it in production.
Related
I'm trying to create a function 'header' which will print html content (doctype, html, head, body, etc) - but when I'm looking in a site source, all of that stuff is in one line, not in a tree hirearchy...
public function header() {
print(
'<!DOCTYPE HTML>'
. '<html>'
. '<head>'
. '<meta charset="utf-8"/>'
);
And when I'm looking in the web source the output looks like:
<!DOCTYPE HTML><html><head><meta charset="utf-8"/>
I would like it to look more like standard html tree:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
How can I do that? What are the options ?
EDIT:
Some of You showed me an echo option - it works, but it looks really bad in a php file - like:
public function header() {
echo "<!DOCTYPE HTML>
<html>
<head>
<meta charset='utf-8' />
</head>
<body>
</body>
</html>
";
The most classic way, using echo :
echo '<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
';
see below methods for printing HTML inside PHP code block
FOR SHORT HTML CONTENTS
echo ' <div class="myClass"> Some Text here. Some text here </p> ';
FOR SHORT HTML CONTENTS WITH PHP variable concatenation
$myName='Optimum';
echo ' <div class="myClass"> My Name is '. $myName .' </p> ';
FOR LONG CONTENT
$html.='';
$phpVariable ='Dummy content'
$html.='<div class="wrapper">'
$html.='<div class="content">';
$html.='<p> My content here'. $phpVariable .' </p>';
$html.='</div>';
$html.='</div>';
echo $html;
According to your scenario
<?php
public function header() { // SOME NECESSARY PHP CODE ?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="<?php //echo get_chartset ?>"/>
<link rel="stylesheet" type="text/css" href="<?php //echo_css_path ?>">
}
?>
This will echo/ print clean HTML code in front.
You could introduce tabs when you print:
print("<!DOCTYPE HTML>"
. "<html>"
. "\t<head>"
. "\t\t<meta charset=\"utf-8\"/>"
);
Another approach would be to simply just separate your html template file (Format it however you want) and then just require it with passed data in your function like so
my_view.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1><?= $heading ?></h1>
</body>
</html>
Your function
function showTemplate($view, array $data) {
extract($data);
require $view . '.php';
}
That way you can just call to output your view with data, like this
showTemplate('my_view', [ 'heading' => 'Awesome Page' ]);
This way your template and data would be more organized and pretty.
Another way, for doing this :
<?php
function myheader() {
?><!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<?php } ?>
I redefined the function name to avoid conflict, (off-topic).
I don't know how such a code should be indented ...
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); ?>" />
How can I add a different title, keyword and description in every page's <head> of my simple php website dynamically?
I have included file header.php in all of my pages, how can i know in what page the user in?
For example, I have php files register.php, and login.php, and I need different titles, keywords and descriptions.
I do not want use the $_GET method.
Thanks!
Set variables at the top of each page that will be read by header.php. Then insert the values of the variables in the right places in header.php. Here is an example:
register.php:
<?php
$title = "Registration";
$keywords = "Register, login";
$description = "Page for user registration";
include('header.php');
?>
header.php
<html>
<head>
<meta name="keywords" content="<?php echo $keywords; ?>" />
<meta name="description" content="<?php echo $description; ?>" />
<title><?php echo $title; ?></title>
</head>
<body>
Put the output inside a function (within your header.php) and insert its parameters at appropriate places into the markup.
function html_header($title = "Default") {
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $title ?></title>
</head>
…
<?php
}
you can try this:
for example the $page variable is your page name:
<?php
switch($page)
{
case 'home':
$title = 'title';
$keyword = 'some keywords..';
$desc = 'description';
break;
case 'download':
$title = 'title';
$keyword = 'some keywords..';
$desc = 'description';
break;
case 'contact':
$title = 'title';
$keyword = 'some keywords..';
$desc = 'description';
break;
}
if(isset($title))
{
?>
<title><?php echo $title; ?></title>
<meta name="keywords" content="<?php echo $keyword; ?>" />
<meta name="description" content="<?php echo $desc; ?>" />
<?php
}
else
{
?>
<title>default</title>
<meta name="keywords" content="default" />
<meta name="description" content="default" />
<?php
}
?>
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.
How can I add a different title, keyword and description in every page's <head> of my simple php website dynamically?
E.g
<title>this is title</title>
<meta name="keywords" content="keyword1, keyword2" />
<meta name="description" content="this is description" />
You can create method to return title for current page, depending on where the user is, and then use it like this.
<title><?php echo get_title(); ?></title>
same with the keywords
<meta name="keywords" content="<?php echo get_keywords(); ?>" />
<meta name="description" content="<?php echo get_description(); ?>" />
The implementation will depend on how you navigate on the website. For example, if you have only index.php, and choose content by $_GET["page"], you can have something like this
function get_title() {
switch($_GET["page"]) {
case "home":
return "Welcome to my home page";
case "guestbook":
return "Welcome to guestbook";
}
}
or you can make it all in one like
function get_headers() {
// here set $title, $description and $keywords according to current page
// ....
// then just generate html
$html = "<title>$title</title>";
$html .= "<meta name='description' content='$description' />";
$html .= "<meta name='keywords' content='$keywords' />";
return $html;
}
and then again do something like this
<head>
...
<?php echo get_headers(); ?>
...