4

I am building a html page but want to break the header out so I dont have to keep changing all the files.

I am attempting to add a php.include file and save the pages out as .php files.

For some reason the header.php file is not showing in my file when i view it.

here is my code. Is this the correct way??

<!DOCTYPE html>

<html lang="en">

    <head>
    <meta charset="utf-8" />
    <title>Chartego | Creating Socially Inflential People with great images and videos</title>

    <meta http-equiv="content-type" content="text/html; charset=utf-8" />

</head>
<body>

<?php include 'header.php'; ?>


  <div id="wrap-inner">
content
</div>

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Paul Designer
  • 845
  • 1
  • 14
  • 21
  • Are you getting any errors (if you turn errors on)? – putvande Oct 31 '13 at 11:32
  • Do you have the `header.php` file in de same directory as the main file? – Carl0s1z Oct 31 '13 at 11:33
  • yes saved in the same directory , when i inspect the element using chrome the php seems to be commented out – Paul Designer Oct 31 '13 at 11:35
  • What kind of file is the main file? Is it, for example, `.html` or `.php`? – SubjectCurio Oct 31 '13 at 11:39
  • `` Perhaps your editor is commenting out those lines for you. Try with notepad and check once. And is this code placed in `.html` or `.php` file ? – GoodSp33d Oct 31 '13 at 11:44
  • My guess is that like `2-Stroker` said is, maybe your editor is doing just that and the syntax resembles that of what `.shtml` files use to include files, syntax being `` which is not what you want, I'm sure. @PaulDesigner – Funk Forty Niner Oct 31 '13 at 16:13

6 Answers6

2

when i inspect the element using chrome the php seems to be commented out

The PHP is not passing through a PHP parser before getting to the browser, so the browser is receiving the PHP code instead of the server executing it.

Make sure that:

  • You are loading the page over HTTP (e.g. not just double clicking the file in your file manager)
  • The server you are using supports PHP
  • The server is configured to treat the file as PHP (this is usually done by giving it a .php file extension)
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Perhaps its not treated as PHP File, but server wouldnt comment out those PHP Tags right ? Any explanation for that ? – GoodSp33d Oct 31 '13 at 11:45
  • 1
    You are looking at it in a DOM inspector (i.e. you aren't looking at the raw source code). The browser is trying to perform error recovery on invalid HTML. – Quentin Oct 31 '13 at 11:50
2

This is what I got, I took your code and saved it as index.php

<!DOCTYPE html>

<html lang="en">

    <head>

    <title>Chartego | Creating Socially Inflential People with great images and videos</title>
    <meta http-equiv="content-type" content="text/html" charset="utf-8"> 
   </head>
<body>

<?php 
   include ('header.php'); 
?>


  <div id="wrap-inner">
content

</div>

I created this in header.php

<?php
  echo "Hello World";
?>

This is the output:

Hello World
content
StBlade
  • 287
  • 6
  • 18
0

OK. A few things.

  1. Check that your file is actually being interpreted as PHP rather than just bog-standard HTML.
  2. Turn on all your error reporting (error_reporting = E_ALL | E_STRICT | E_WARNING in php.ini)
  3. Make sure your errors are displayed (display_errors = on in php.ini)

Try using require rather than include as require will halt execution if it can't load the file and show an error

DaveyBoy
  • 2,928
  • 2
  • 17
  • 27
0

You need to check if html page can run the php code or not.
If you are using Apache server for running html page, then the way to execute PHP on a .html page is to modify your .htaccess file. Put following code in .htaccess file:

//for .html:
AddType application/x-httpd-php .html
// for .htm
AddType application/x-httpd-php .htm

For more details, read following link : http://php.about.com/od/advancedphp/p/html_php.htm

Another possible reason can be : File is not available on the given location , so for that check the error_log.

Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
0

Your Script header.php must begin with " < ? php ' and finish with ' ? > ' and go to the php settings and uncheck 'short open tag'.

majd hwas
  • 95
  • 2
  • 2
  • 10
0

I know this question is old and has an answer already, but in my case the problem was that I didn't have the php module for Apache installed.

Installing it fixed the issue for me:

sudo apt install libapache2-mod-php7.4
borizzzzz
  • 620
  • 1
  • 6
  • 17