user20072008
user20072008

Reputation: 379

Multiple <HTML> tags in a single page

I am fairly new to PHP. I have been developing a PHP website code which requires a common header (header.php). I have included common header.php page in content.php. Now I see 2 2 and 2 tags in the HTML view source.

Code is something like this -

<?php
      include "header.php";

      //some php code.
?>
<html>
<head> <script src="test.js"> </script></head>
   <body>
        <h1> Test Page </h1>
   </body>
</html>

In page source, my code looks like this,

<html>
<head> <script src="header.js"> </script></head>
   <body>
        <h1> Menu Page </h1>
   </body>
</html>
<html>
<head> <script src="test.js"> </script></head>
   <body>
        <h1> Test Page </h1>
   </body>
</html>

Please suggest if this code is OK or multiple html or head tags are not good for SEO and/or performance. Also please suggest workaround.

Upvotes: 1

Views: 7007

Answers (7)

Eamonn Kenny
Eamonn Kenny

Reputation: 2052

It turns out that there are sometimes comment lines at the top of a html file that are used to configure the html for different browsers. If you run a script to strip the comment of something like this:

<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]>  <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en-GB"> <!--<![endif]-->

you will inadvertently end up with the appearance of 4 html loops which then causes a parser such as beautifulsoup to fail badly.

Upvotes: 0

MightyPork
MightyPork

Reputation: 18861

No, you can have only one <HTML> per page.

What you are looking for is a "layout pattern".

<!DOCTYPE HTML>
<HTML>

    <HEAD>
        <!-- your link to stylesheet, jquery etc -->
    </HEAD>

    <BODY>
        <!-- here include your header -->

        <!-- and your content -->
    </BODY>

</HTML>

Both header and content don't have any <HTML> or the rest of it - it's just <DIV>'s etc.

If you are struggling with the basic HTML format, here are some basic html templates you can use.


For any bigger application, it makes sense to use a framework (such as Laravel) and some templating engine.

However, for this simple example, you could have something like this:

[file index.php]
- resolve what content to show based on $_GET
- include layout, tell it what title and content to show

[layout]
as shown above, add into <head> a <title> tag
in body, include the content files

[additional files for content, header etc]
...

Upvotes: 2

Raphael
Raphael

Reputation: 265

You should use only one <html><head><body> and <h1> Tag to write valid html-code.

If you try to build a template like engine with php you could build it like that:

header.php

<!DOCTYPE HTML>
<html>
<head>
    <script src="my.js"></script>
</head>
<body>

index.php

<?php
     include_once("header.php");
?>
<h1><?php echo $page_title; ?></h1>

<div class="main">
    <p>This is my Content</p>
</div>

<?php
    include_once("footer.php");
?>

footer.php

<script src="additional.js"></script>
</body>
</html>

Upvotes: 1

GGio
GGio

Reputation: 7653

I think this is what you are looking for:

your header.php should contain the following html ONLY

<html>
   <head>
     <title></title>
     ....
   </head>
   <body>

Now your body page should contain only divs and content that goes within <body></body> only.

Then you have footer.php that ends the html like this:

   </body>
</html>

so in your main body file you would have the following:

<?php
   include "header.php";

   //some php code.
?>

<h1> Test Page </h1>

<?php
   include "footer.php";
?>

Upvotes: 1

SerhatCan
SerhatCan

Reputation: 590

You need to delete html and body tag etc in header.php. That will solve your problem

Upvotes: 0

Hongyi Li
Hongyi Li

Reputation: 1069

No. You can only have one set of

<html> </html>

tags

Upvotes: 1

rje
rje

Reputation: 6428

A Html document contains exactly 1 HTML tag with a head and body tag inside it, by definition. So your code is not valid HTML.

Upvotes: 1

Related Questions