Php require_once or include inside HTML head tag does not put code inside the head, but in the body

I want to share the contents of my HTML header tag between several pages, but before I use require_once in the HTML header tag, I come across a strange error. Let me explain better.

If I have the following code:

<!DOCTYPE html> <html> <head> <title>Datos Soluciones Informáticas</title> <meta charset="UTF-8"> <link rel="stylesheet" href="/css/main.css"> </head> 

and I check the code, everything looks as expected.

enter image description here

However, if I move the contents of the head tag to an external file /snippets/head.php

 <meta charset="UTF-8"> <link rel="stylesheet" href="/css/main.css"> 

And then write the following code in my index.php file:

 <!DOCTYPE html> <html> <head> <title>Datos Soluciones Informáticas</title> <?php require_once('/snippets/head.php'); ?> </head> 

The check shows that the code is not inserted in the right place:

enter image description here

The problem is not only that the check does not work, but the page does not behave as expected. I have the same problem with include instead of require_once

The original result from the view: localhost in chrome source looks good, but the page doesn't display well

 <!DOCTYPE html> <html> <head> <title>Datos Soluciones Informáticas</title> <meta charset="UTF-8"> <link rel="stylesheet" href="/css/main.css"> </head> <body> 

I am using Xampp 6.1 Build 7601 on a computer running Windows 7 as my local environment.

Does anyone know what I am missing?

+4
source share
2 answers

Another shot:

Is your included file with invisible byte (BOM) included? Then delete it by setting the correct character encoding.

See http://php.net/manual/en/function.include.php which contains this comment:

AVOID ORDERING ORDERING ZERO BYTE!

I am having problems with include / require (once or not). I created include-opening.php that had the initial page structure and then included this page on all other pages. The result looked “broken”, so I compared the inclusion or just pasting the html code on the page. The hardcoded version is displayed fine even if the source code is exactly the same.

So, I opened the include file with notepad ++ and set the encoding to UTF-8 (without specification) and voila, now everything works fine.

+10
source

Try to include the file without the first slash, for example:

 <?php require_once('snippets/head.php'); ?> 
0
source

Source: https://habr.com/ru/post/1386824/


All Articles