I have created a simple webpage, which includes both a header and footer as separate php files, shown below
<?php
$PageName = "Home Page";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/header.php";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/footer.php";?>
this is the header
<?php
print("<!DOCTYPE html>");
print("<html lang='en-UK'>");
print("<head>");
print("<title>");
print($PageName);
print("");
print("</title>");
print("<meta http-equiv='content-type' content='text/html; charset=utf-8' >");
print("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
$CSSRoot = "/MyPage/StyleDefault.css";
print("<link rel='stylesheet' type='text/css' href=$CSSRoot>");
print("</head>");
print("<body>");
print("<h1>My Page</h1>");?>
and footer
<?php print("</body></html>");?>
but when I view it the header elements appear in the body as shown below
header information appearing in the body
I want to make clear this does not, yet, cause any problems, but I want to know what the cause is.
Thanks
EDIT
brain fart moment putting the code in the comments, sorry.
new index
<?php
$PageName = "Home Page";
$CSSRoot = "/MyPage/StyleDefault.css";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/header.php";
?>
<h1>My Page</h1>
<?php
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/footer.php";?>
new header
<!DOCTYPE html>
<html lang="en-UK">
<head>
<title><?php echo $PageName;?></title>
<meta http-equiv='content-type' content='text/html; charset=utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<link rel='stylesheet' type='text/css' href="<?php echo $CSSRoot;?>">
</head>
<body>
new footer
</body></html>
new output
<html lang="en-UK"><head></head><body>
<title>Home Page</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="/MyPage/StyleDefault.css">
<h1>My Page</h1>
</body></html>
this is how you should do this.
header.php
<!DOCTYPE html>
<html lang="en-UK">
<head>
<title><?php echo $PageName;?></title>
<meta http-equiv='content-type' content='text/html; charset=utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<link rel='stylesheet' type='text/css' href="<?php echo $CSSRoot;?>">
</head>
<body>
index.php
<?php
$PageName = "Home Page";
$CSSRoot = "/MyPage/StyleDefault.css";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/header.php";
?>
<h1>My Page</h1>
<?php include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/footer.php";?>
footer.php
</body>
</html>
This should work, I don't know why you would want to print all the html tags using php, while you can just output em normal.
I beleive I have found the problem, the sources panel in chrome shows that just before the doctype and end body tags are two characters (ie where the header and footer files begin), represented by red dots, according to http://apps.timwhitlock.info/unicode/inspect?s=%EF%BB%BF%EF%BB%BF this is a ZERO WIDTH NO-BREAK SPACE, I don't know how they appeared and I can't seem to get rid of them, but it seems likely that they are the cause of the problem.
UPDATE
having looked around the prolbem is that the php files where using UFT-8 BOM (byte order mark) which inserted the offending characters, changing it to UTF-8 (which is under encoding in Notepadd++) solved the problem, thanks for all the help
Related
In PHP I am writing:
$link = "<a href='/?s=cqs3&importo_desiderato=5000&categoria_cqs='. $cat .'>5.000€</a>";
But the value "$cat" is not showing anything.
Any suggestion?
Consider that if I write in HTML:
5.000€
It works...
Probably it is something I am missing in the sintax.
Thank you!
I solved like this:
<?php
$cat = $_GET['categoria_cqs'];//(to get the value from the session)
$link = "<a href='/?s=cqs3&importo_desiderato=5000&categoria_cqs=$cat'>5.000€</a>";
?>
I still cannot understand why I had to get it again (in HTML it worked without it) but that is fine. Thank you for your help!
The error is in the quotes, your line should look like this:
$link = '5.000€';
Remember that the string must be closed with the same quotation mark as it was opened.
Additionally, remember that the character " " and ' ' can "escape" themselves, as MoarCodePlz said.
I don't know what your real purpose is, if it is to create this directly in PHP or not, but maybe the code below will help you, I created a variable that will store the website URL that will pass the $cat variable as a parameter, to see the result I used the <a> tag in html
The code would look like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$cat = 'Pathname';
$link = "https://yourwebsite.com/?s=cqs3&importo_desiderato=5000&categoria_cqs=' . $cat . '";
?>
5.000€
</body>
</html>
If you want to get the result only in PHP, use the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$cat = 'Pathname';
$link = "<a href='/?s=cqs3&importo_desiderato=5000&categoria_cqs=" . $cat . "'>5.000€</a>";
echo $link;
?>
</body>
</html>
Edit
You say that your code works only using HTML and not in PHP. Well, I've been analyzing it here and I saw that this was happening due to the fact that the file is in a directory, right? PHP was not understanding that the URL was from this directory, at least for me the error was this one in addition to the quotes
Please try the code below, I believe it will work in both PHP and HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$cat = $_GET['categoria_cqs'];
$link = '5.000€';
echo $link;
?>
5.000€
</body>
</html>
I have trouble with the contruction of a working well formmated DOM, through php.
The source-code is diplayed right, but all the dev-tools of Chrome, Firefox and Edge, display the head-tag inside the body-tag. Can you please help me to spot the mistake, beacuse the frontend is now faulty displayed.
it look like this:
php-snippet:
<?php
session_start();
//doctype
echo "<!DOCTYPE HTML>\n";
//html
echo "<html>\n";
//html-head
echo "<head>\n";
include "inc/head.html";
echo "</head>\n";
//html- body start-end
echo "<body>\n
some content
</body>\n</html>\n";
?>
head.html:
<meta http-equiv='content-type' content='text/html; charset=UTF-8' />
<meta name='author' content='MGM'>
<script type='text/javascript' src='http://code.jquery.com/jquery-2.2.0.min.js'></script>
<link rel="shortcut icon" href="media/favicon.png" type="image/png">
<link rel='stylesheet' type='text/css' href='media/desktop.css'>
sourcecode html:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv='content-type' content='text/html; charset=UTF-8' />
<meta name='author' content='MGM'>
<script type='text/javascript' src='http://code.jquery.com/jquery-2.2.0.min.js'></script>
<link rel="shortcut icon" href="media/favicon.png" type="image/png">
<link rel='stylesheet' type='text/css' href='media/desktop.css'></head>
<body>
some content
</body>
</html>
You can either use file_get_contents() for this
$content = file_get_contents('head.php');
print $content;
Or use the include function but receive its output.
$content = include('head.php');
print $content;
NOTICE
Keep in mind, that if you decide to use include for this, it will execute the code inside head.php first, which file_get_contents() wouldnt.
Maybe this also helps you.
I would suggest using PHP's output buffer, changing your code to look like this:
<?php
session_start();
ob_start();
?>
<!DOCTYPE HTML>
<html>
<head>
<?php include "inc/head.html"; ?>
</head>
<body>
some content
</body>
</html>
<?php
echo ob_get_clean();
?>
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.
I'm converting a couple sites from HTML to PHP for dynamic elements and have been able to do so with the header and footer (using php include()). However, I'm confused on how to do the head section. This is what I have with plain HTML:
<head>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->
<meta charset="UTF-8" />
<meta name="description" content="Liberty Resource Directory. The ultimate curated directory to find what you need."/>
<meta name="keywords" content="ethan glover, lrd, liberty resource directory"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href="stylesheets/lrdstylesheet.css" rel="stylesheet" media="screen">
<title>Liberty Resource Directory</title>
</head>
I can easily add the HTML5Shim script, meta charset, viewport (yes I will remove that max scale), and the stylesheet link.
Here's the problem:
How can I write the .php file in a way that I can pass an individual pages description, keywords and title to it? (That way I can put the entire above code in a php file and just include it on every page.)
Or will I simply have to exclude the description, keywords and title, and rewrite those parts every time?
Here's the answer: (Courtesy of Alejandro Arbiza)
head.php
<head>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->
<meta charset="UTF-8" />
<meta name="description" content="<?php echo $description;?>"/>
<meta name="keywords" content="<?php echo $keywords;?>"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="../stylesheets/lrdstylesheet.css" rel="stylesheet" media="screen">
<title><?php echo $title;?></title>
</head>
index.html (to include above code)
<?php
$description="Liberty Resource Directory. The ultimate curated directory to find what you need.";
$keywords="ethan glover, lrd, liberty resource directory";
$title="Liberty Resource Directory";
include 'scripts/head.php';
?>
The final result:
http://libertyresourcedirectory.com/
You can use variables for the description and the keywords (or anything else you want for that matter). Then, when the time comes to construct the page, you would just set the variables with the corresponding values.
<head>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->
<meta charset="UTF-8" />
<meta name="description" content="<?php echo $description; ?>"/>
<meta name="keywords" content="<?php echo $keywords; ?>"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href="stylesheets/lrdstylesheet.css" rel="stylesheet" media="screen">
<title>Liberty Resource Directory</title>
</head>
So, lets say you have page1.php and page2.php:
<?php
// page1.php
$description = "This is page one";
$keywords = "page one";
include 'header.php';
?>
<!-- Page content -->
<?php include 'footer.php'; ?>
and
<?php
// page2.php
$description = "This is page two";
$keywords = "page two";
include 'header.php';
?>
<!-- Page content -->
<?php include 'footer.php'; ?>
Of course, I am assuming here that the whole HTML header is inside the header.php file, that is including <html>, <head> and <body>.
I would like to give a title to each of my pages, but all my pages are linked to my index.php:
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Test</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php include("top_bar.php");?>
<?php include("header.php");?>
<?php include("container.php");?>
<?php include("footer.php");?>
</body>
</html>
Here is how my site is: http://prntscr.com/47nn7h
All my pages have the title I put for index.php, but how to add a title to a specific page (example, when I go to the page members.php)?
members.php:
<?php include "index.php";?>
Thanks.
Replace the existing <title> tag with this.
<title><?php echo $pagetitle; ?> </title>
in your <head> block.
Make sure that $pagetitle actually contains the desired title before you emit the tag. It's not clear from your question where these titles are coming from - you'll probably need some PHP right at the top of the page to set all this up.
.
You could use this method and add the
$pageTitle = 'Title of Page';
to your content page (i.e. member page)