i have html code inside a php variable
<?
$HTML_CODE='
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>'.$pg->pgTitle.'</title>
<link rel="stylesheet" href="'.$cssFile.'" type="text/css" />
</head>
<div id="header">
';
echo $HTML_CODE;
?>
When i view the source of my page by in browser ( right click-> view source )
The code starts in line 4
1.
2.
3.
4.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
5.<html>
6.<head>
7.<title>page title</title>
8.<link rel="stylesheet" href="style.css" type="text/css" />
9.</head>
10.<div id="header">
i've used trim function, it's now start in line 3!!
This isn't an issue; no errors will result, but at least one of those lines is due to your code:
$HTML_CODE='
<!DOCTYPE HTML PUBLIC ...
this introduces a line feed in. Additionally, I expect you have some whitespace before your php opening tag <?.
try
<? $HTML_CODE='<!DOCTYPE
with out the line breaks in your code, also try removing the linebreak between
'; and echo
Remove the unnecessary page breaks from your initialisation of variable
$HTML_CODE='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>'.$pg->pgTitle.'</title>
<link rel="stylesheet" href="'.$cssFile.'" type="text/css" />
</head>
<div id="header">';
Related
So I have a header file:
<html>
<head>
<link href="/design_header.css" rel="stylesheet" type="text/css" />
</head>
<body>
content
</body>
</html>
And I want to place this header in my file
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
meta tags etc.
</head>
<body>
<div id="container_big">
<?php include 'header_login.php'; ?>
content
</div>
</body>
</html>
The problem is now I have regular <html>, <head>, <body> tags inside the <body> tag of my webpage. In the html validator I receive errors like
Stray start tag html.
Stray end tag head.
An body start tag seen but an element of th
e same type was already open.
How to do it properly? Btw. I also place a footer to the bottom of the webpage the same way.
Your header file should only contain the HTML text you want for the header.
As it will be inserted into another webpage, it should not be a full HTML document.
Having only HTML for Header in Header
One option is to instead include in your header file only the HTML that is for the header (used by all pages that include it). However this has the downside that your not following the recommendation to have CSS loading before is rendered.
See: https://stackoverflow.com/a/1642259/1688441
Header File
<link href="/design_header.css" rel="stylesheet" type="text/css" />
content
Other files
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
meta tags etc.
</head>
<body>
<div id="container_big">
<?php include 'header_login.php'; ?>
content
</div>
</body>
</html>
Having only HTML for Header in HeaderFile and Separate HeadFile
You could have have a separate global_head_include.html (or txt, php, etc) file and put your CSS code there.
Header File (for include)
Header content
File with CSS includes (for include)
<link href="/design_header.css" rel="stylesheet" type="text/css" />
Whatever else is global...
Other files
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
meta tags etc.
<?php include 'global_head_include.php'; ?>
</head>
<body>
<div id="container_big">
<?php include 'header_login.php'; ?>
content
</div>
</body>
</html>
This is how I set out my headers and footers in my current PHP site:
header.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
<title>James Wright - Young Software Developer</title>
<link rel="stylesheet" href="style.css" />
<meta name="description" content="The online home of a young web designer and software developer." />
<link rel="icon" type="image/png" href="/img/ico.png" />
</head>
<body>
<div id="holder">
<div id="menu"></div>
<div id="mainContent">
footer.php:
</div>
<div id="mainReflect"></div>
<p class="footer">© James Wright <?php echo date("Y"); ?>. Minimum resolution: 1024x768 <a href="http://validator.w3.org/check?uri=referer" target="_blank"><img
src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Transitional" height="31" width="88" style="vertical-align: middle;"/></a>
<a href='http://www.powermapper.com/products/sortsite/'><img src='http://www.powermapper.com/images/badge-v1/sortsite-badge-small-5.png' width="80" height="15" alt='Broken link checker and accessibility checker top 5% - SortSite' style='vertical-align: middle;'/></a>
</p>
</div>
</body>
</html>
Then whenever I create a new page, all I need to do is this:
<?php include("header.php"); ?>
<p>Main body content!</p>
<?php include("footer.php"); ?>
it produces output when it is saved as filename.html
but when i try to change it to filename.php
then open the link in the brown using filename.php
it doesnt produce output anymore
NOTE: i removed some of the code too long
<?php
echo"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
print code requirement
function printpage()
{
window.print();
}
</script>
print code requirement
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>WildFlowers by FCT</title>
<link href='http://fonts.googleapis.com/css?family=Oswald:400,300' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Abel' rel='stylesheet' type='text/css'>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<style type="text/css">
#wrapper #header-wrapper #header h1 a strong {
font-family: Georgia, "Times New Roman", Times, serif;
size: '100';
font-size: 40px;
</body>
</html>
";
?>
You have to remove <?php and ?>. Between these two tags there must be php-code and no html.
Edit: Maybe no errors are reported, because your server is configured to suppress errors.
Put some space after the echo..And use single quote['] insted of double.And change all single quotes from your code to double quote.(")
eg: -
<?php echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body> Bla bla bla
</body>
</html>';
?>
The problem in your code is, you just started the codes with double quote.So the PHP server consider the next double quote is the closing of the echo statement.So there is nothing to print inside the first two double quote.But if you take the page source, you can see
<!DOCTYPE html PUBLIC
This is because you open PHP tag at the beginning of file:
<?php
Close it before your HTML markup:
<?php
// your php code
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
You can not have html in your php tags, if you want to use html you have to end the close your php first so something like this:
<?php
// php code here
?>
html code here
<?php
// php here
?>
If You need to write html inside php code use the echo statement.
ex:
<?php echo"
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head></head>
<body>
//your code here
</body>
</html>";
?>
Why I am not able to end a javascript inside a PHP heredoc?
The rest of the code below this line:
</script>
become not part of PHP's code. They become HTML code.
It is like the end script code ends the PHP block.
$headerContent = <<<HEAD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>$title</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-2" />
<script>
</script> // Here is the problem
</head> // code here and below becomes not part of PHP.
<body>
.
.
.
HEAD;
Any tips for solving this problem?
Although I can't reproduce this with HEREDOC (it's possible that different versions of PHP behave differently in this respect), </script> is equivalent to ?> in PHP code, because it's a counterpart to <script language="php">. Example:
<script language="php"> $a = 1; </script>
Test: <?= $a ?>
So wherever you encounter problems with the ?> closing tag, you'll also encounter the same problems with the </script> closing tag. One option would be to store this in a variable and use it. Example:
<?php
$endScript = '</' . 'script>';
$headerContent = <<<HEAD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>$title</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-2" />
<script>
$endScript
</head>
<body>
.
.
.
HEAD;
I'm trying to make the whole <head> section its own include file. One drawback is the title and description and keyword will be the same; I can't figure out how to pass arguments to the include file.
So here is the code:
index.php
<?php include("header.php?header=aaaaaaaaaaaaaaaaaaaaa"); ?>
<body>
.....
..
.
header.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="shortcut icon" href="favicon.ico">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content=" <?php $_GET["header"]?> " >
<meta name="Description" content=" <?php $_GET["header"]?> " >
<title> <?php $_GET["header"]?> </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
Obviously this doesn't work; how can I pass arguments to an included file?
Include has the scope of the line it's called from.
If you don't want to create new global variables, you can wrap include() with a function:
function includeHeader($title) {
include("inc/header.php");
}
$title will be defined in the included code whenever you call includeHeader with a value, for example includeHeader('My Fancy Title').
If you want to pass more than one variable you can always pass an array instead of a string.
Let's create a generic function:
function includeFile($file, $variables) {
include($file);
}
Voila!
Using extract makes it even neater:
function includeFileWithVariables($fileName, $variables) {
extract($variables);
include($fileName);
}
Now you can do:
includeFileWithVariables("header.php", array(
'keywords'=> "Potato, Tomato, Toothpaste",
'title'=> "Hello World"
));
Knowing that it will cause variables $keywords and $title to be defined in the scope of the included code.
index.php:
<?php
$my_header = 'aaaaaaaaaaaaaaaaaaaa';
include 'header.php';
?>
and header.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="shortcut icon" href="favicon.ico" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content=" <?php echo $my_header ?> " />
<meta name="Description" content=" <?php echo $my_header ?> " />
<title> <?php echo $my_header ?> </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
It's not an ideal solution, but I understand it's your first steps in php.
PS. Your Doctype doesn't match the code. I've adjusted your header html to be XHTML.
You can't pass arguments to include, but it has access to all variables you've already set. From the include documentation:
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward.
Thus:
index.php
<?php
$header = 'aaaaaaaaaaaaaaaaaaaaa';
include("header.php");
?>
header.php
<title> <?php echo $header; ?> </title>
Well marc, when you are using include, you can simply just set up a variable to use:
<?php
$var = "Testing";
include("header.php");
?>
In your header file:
<?php
echo $var;
?>
Allow your previously defined variables are usable in any include you have.
you are over thinking it
<?php
$header = "aaaaaaaaaaaaaaaaa";
include("header.php");
?>
::EDIT::
Decided I would add value
The included file will gain the scope of where you included it. So if you include a file INSIDE a function:
<?php
$get_me = "yes";
function haha()
{
include("file.php");
}
haha();
// And file.php looks like
echo $get_me; // notice + blank
?>
More over, you include the same file more than once to great effect.
<?php
$output = "this";
include("cool_box.php");
$output = "will";
include("cool_box.php");
$output = "work";
include("cool_box.php");
?>
And even use this to load templates that become part of a method in a class. So you can do something like:
<?php
class template
{
private $name;
function __construct($name)
{
$this->name = preg_replace("/[^a-zA-Z0-9]/", "", $name);
}
function output(array $vars)
{
include($this->name.".php"); // Where $vars is an expected array of possible data
}
}
$head = new template("header");
$body = new template("body");
$head->output();
$head->output(array("content" => "this is a cool page"));
?>
defining a variable as a pseudo-argument/workaround before an include() - as recommended by many - is a bad idea. it introduces a variable in the global scope. define a function in the included file instead to catch the arguments u want to pass.
This is good approach. I however would do it a bit inside out. Define a layout, a wrapper for your webpage and include your content file into it:
layout.phtml
<html>
<head>
... your headers go here
</head>
<body>
<? include $content ?>
</body>
</html>
Your content template file can look like this e.g.
content.phtml
<h1>hello world</h1>
<p>My name is <?= $name ?></p>
Then, you would have your main script (index) that will handle logic, connects to database etc.
index.php
$content = 'content.phtml';
$name = 'Marc'; //Can be pulled from database
include 'layout.phtml';
This way, you can nicely separate business logic and presentation. And it can help you cut repetitive code for parts of page like logo or navigation which are repeated on the whole site.
If you include a file it is just like inserting that code into the parent file. You could simply do this:
<?php
$parameter = "Hello World";
include("header.php");
?>
and then in the header.php
<?php
$parameter = isset($parameter) ? $parameter : "Default Text";
// Use accordingly
?>
I used the isset() method to verify that it has a value already and is instantiated.
I noticed nobody suggested using a template engine. I came looking here because for the project I'm working with, a template engine isn't possible and that might be your situation too, however I thought it might be worth mentioning these: Twig (my preferred engine) and Smarty both allow passing specific variables to includes.
I highly recommend the use of a template engine whenever possible, as it simplifies your front end code, adds a layer of abstraction between your front end and back end, and both Twig and Smarty automatically clean the variables you pass to them which helps mitigate XSS attacks.
Twig Example
header.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="shortcut icon" href="favicon.ico">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content="{{ header }}" >
<meta name="Description" content="{{ header }}" >
<title> {{ header }} </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
index.html
{% include 'header.html' with { 'header' : '<script>alert("this shouldnt work")</script>'} only %}
Body Text
{% include 'footer.html' %}
Smarty Example
header.tpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="shortcut icon" href="favicon.ico">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content="{$header}" >
<meta name="Description" content="{$header}" >
<title> {$header} </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
index.tpl
{include 'header.tpl' header='<script>alert("this shouldnt work")</script>'}
Body Text
{include 'footer.tpl'}
I don't know what is wrong with this code, it is a bug or I made a mistake somewhere; xDebug show nothing.
Class script
class theme {
function theme() {
//show header (meta, style, htmldoctype, script, and title)
$this->htmlheader();
//show main content
//show footer
}
function htmlheader() {
require "localsettings.php";
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n<html>\n<head>\n";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n";
echo "<title>$site_name - $page_title</title>\n";
echo "</head>\n";
}
}
index.php
require "theme.class.php";
$html = new theme();
//display result
$html->theme();
Output (incorrectly repeated)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>site title - </title>
</head>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>site title - </title>
</head>
When you name the function the same as the class, it is a "constructor", and gets called when the class is instantiated. Thus, your function theme() is called both here:
$html = new theme();
and here:
$html->theme();
Remove the latter, and you should be good to go.