-1

Please bear with with me as I am new to web dev. I am attempting to use the include function in php to display a header on a web page.

I am using two very simple files, placed in the same directory and am using XAMPP.

index.php

<html>
<body>
    <h1>Hello</h1>

    <?php 
    include('header.php');
    ?>
</body>
</html>

header.php

<p>Hello</p>

When I open index.php, the statements from header.php never show up. All I see is one "Hello", as printed in index.php.

I have attempted to follow many tutorials online similar to this (ex https://www.w3schools.com/php/php_includes.asp, https://www.tutorialrepublic.com/php-tutorial/php-include-files.php), but none of them work for me.

Please let me know if you have any advice.

cmjw
  • 61
  • 1
  • 8
  • Is the `header.php` in the same folder? – biesior Mar 01 '21 at 18:20
  • 1
    @biesior "I am using two very simple files, placed in the same directory and am using XAMPP." – j08691 Mar 01 '21 at 18:22
  • 1
    that simple code *should* work - just tested it locally and it shows *'properly'*. However I'd suggest to go with some little bit more advanced templating system, pure including files just becoming non-comfortable sooner than later – biesior Mar 01 '21 at 18:25
  • Thanks for testing it on your own machine. Do you know of any reasons why this code wouldn't work on mine? I looked into if I might be referencing the file path incorrectly, but that doesn't seem to be the case. – cmjw Mar 01 '21 at 18:27
  • If you view the page source is the PHP code there? – user3783243 Mar 01 '21 at 18:41
  • Yes it is @user3783243. The first tag and the php code are highlighted in red, which I believe means a syntax error – cmjw Mar 01 '21 at 18:42
  • Are you requesting with `file://` or `http://`? Sounds like you aren't going through the web server which bypasses PHP processing – user3783243 Mar 01 '21 at 18:46
  • I think that may be the problem @user3783243. Thank you for your input. I just have one last question, sorry it's a little basic. How do I change the index.html / index.php file that opens when I go to localhost? – cmjw Mar 01 '21 at 18:55
  • 1
    Do you mean if you load `http://localhost/`? If so https://httpd.apache.org/docs/2.4/mod/mod_dir.html#directoryindex should control which resource is served – user3783243 Mar 01 '21 at 18:59

1 Answers1

1

For security reasons you should use the following statements instead of include, unless you have a good reason to not do so:

Either

require_once 'header.php';

or

require 'header.php';

This will cause an E_ERROR in case the file was not found or you don't have permissions to include it. Otherwise you run into the risk not executing code which is mandatory for your program.

Tintenfisch
  • 58
  • 1
  • 4
  • Thank you for your response. I tried both require_once and require, however index.php still does not appear as it should. How do I access the error log to see if it threw either of those errors? – cmjw Mar 01 '21 at 18:31
  • 1
    Can you open the HTML source code and have directly a look into it? You should see - at least if you use `require` - an error message. You can try to post the resulting HTML source code into your question. – Tintenfisch Mar 01 '21 at 18:32
  • I don't see an error in my IDE (VSCode) or when inspecting the page in a web browser. I'm not sure if I am looking in the correct place however. – cmjw Mar 01 '21 at 18:40
  • 1
    Your PHP code may be not executed at all. In XAMPP you would view the page using your webbrowser by typing http://127.0.0.1 into the browser or so. – Tintenfisch Mar 01 '21 at 18:49