Jump to content

What am I doing wrong with this 'php include' coding?


Go to solution Solved by maxxd,

Recommended Posts

Hi all, I hope you can help as I am going out of my mind trying to work out what I'm doing wrong. Long story short, I want to have my new site's navigation links as an include so that if I need to update them I only do it the once rather than have to go through every page. The list of links shown in pic 'newsitehelp02' should appear at line 30 on 'newsitehelp01'. When I upload all the relevant pages to my host I don't get any php error messages - the navigation links just don't show up at all. What am I doing wrong? Thanks!

newsitehelp02.png

newsitehelp01.png

a bunch of points -

  1. three ... in a relative file system path is invalid.
  2. you should be learning, developing, and debugging code on a localhost development system, such xampp.
  3. do you have php's error_reporting set to E_ALL and display_errors set to ON, preferably in the php.ini on your development system, so that php will help you by reporting and displaying all the errors it detects?
  4. is the main page a .php page?
  5. what does the 'view source' of the resulting web page show in the browser?
  6. you should use 'require' for things your code must have.
  7. your goal is to produce ONE valid web page. the file that you require is not a complete web page, it is only the code/content that you want to be required at that point in the web page.
  8. you need to validate the resulting web pages at validator.w3.org
  9. all the navigation links you have shown are all the same. you should be dynamically building the web pages using a Content Management System (CMS), in which case the navigation links would be dynamically built too, based on the defined pages, instead of manually creating and managing a bunch of pages yourself.
22 hours ago, mac_gyver said:

a bunch of points -

  1. three ... in a relative file system path is invalid. Thanks - have removed them. It just says '/page/site-header.php' now.
  2. you should be learning, developing, and debugging code on a localhost development system, such xampp. Okay, thanks.
  3. do you have php's error_reporting set to E_ALL and display_errors set to ON, preferably in the php.ini on your development system, so that php will help you by reporting and displaying all the errors it detects? I don't have anything on my PC. I'm writing my code in Microsoft Expression web and then just uploading to my web host (who assure me that php is part of my package). 
  4. is the main page a .php page? Yes it is.
  5. what does the 'view source' of the resulting web page show in the browser? It shows the exact same code as my attached pic, except line 30 is not there.
  6. you should use 'require' for things your code must have. Noted, thanks will do.
  7. your goal is to produce ONE valid web page. the file that you require is not a complete web page, it is only the code/content that you want to be required at that point in the web page. Noted, thanks.
  8. you need to validate the resulting web pages at validator.w3.org Tried that and it gave me an error message. "Sorry, I am unable to validate this document because its content type is application/octet-stream, which is not currently supported by this service."
  9. all the navigation links you have shown are all the same. you should be dynamically building the web pages using a Content Management System (CMS), in which case the navigation links would be dynamically built too, based on the defined pages, instead of manually creating and managing a bunch of pages yourself. Yeah that's simply because I haven't built those pages yet so leaving it as the index url - will update them in due course. My fist real task was to solve this bloomin' php include riddle!

 

15 minutes ago, Ocean_Voyager said:

have removed them. It just says '/page/site-header.php' now.

the file system path/filename must be to where the file is located on the disk, either using a relative path (relative to the file with the include/require starting in it) or an absolute path. a leading / refers to the root of the current disk, which is doubtful where that file is located, and which will be producing a php error about a non-existent path/file.

you must get php to help you by reporting and displaying all the errors it detects. you can temporarily set php's error_reporting/display_errors in your code (you will want to remove the settings when you are done learning, developing, and debugging). you can add the following immediately after the first opening <?php tag in the main file -

ini_set('display_errors', '1');
error_reporting(-1);

 

  • Like 2

First an editorial comment:  Please do not take screen shots of code for your questions.  We can't use that in our replies or help.  We have a code block for a reason.  You can easily copy/paste snippets of relative code into the code block, and that makes it possible for us to make edits based on the original code.  Nobody here likes having to re-type parts of your code.

 

Paths in your html are relative to the document root.  The document root is a function of the web server configuration for the server or vhost you have configured.

The types of things you can refer to in an html script are only things that the server understands to have an associated URL.  In other words, they are things that you could use a url to access directly from your website.

PHP works entirely with files, and file system paths on your workstation.  Using PHP code to open include/require files always requires a file system path.

When you include a file, the "working directory" for PHP is the path/location in the server file system where the script exists. 

So to include a php script from another php script you have 2 options:

  1. Provide the fully qualified path to the script. 
    1. Because this path is operating system dependent, you typically don't want to do this as it requires you to add configuration in a file or even edit your source code purely because you moved servers.
    2. A Relative Path
      1. This is relative, again to the directory where the script that is including this code is.

 

Relative paths can utilize 2 "special files":

"."  is the current directory.

".." is the parent directory.

 

We don't know what the file system layout is for your project, so we can only guess at the correct path relative to the index.php? or whatever file it is that you are trying to do this php include in is named.

If this directory has a sub-directory named "page" and you have put the site-header.php script into that directory, then the relative path you want to use will be:

include("page/site-header.php");

This is how you specify a subdirectory path relatively.  

If you include a leading "/" that is considered the "root" directory of the filesystem.

One other thing you might want to do is change include to require_once().  With a require_once() the script will actually error out rather than continue to run along without regard to the fact that your include failed.

  • Like 1
15 hours ago, gizmola said:

First an editorial comment:  Please do not take screen shots of code for your questions.  We can't use that in our replies or help.  We have a code block for a reason.  You can easily copy/paste snippets of relative code into the code block, and that makes it possible for us to make edits based on the original code.  Nobody here likes having to re-type parts of your code.

I'm sorry, I just assumed you'd see what I'd typed and see a glaring error. I certainly didn't expect you to have to retype anything, apologies :(

 

If this directory has a sub-directory named "page" and you have put the site-header.php script into that directory, then the relative path you want to use will be:

include("page/site-header.php");

This is how you specify a subdirectory path relatively.

If you include a leading "/" that is considered the "root" directory of the filesystem.

One other thing you might want to do is change include to require_once().  With a require_once() the script will actually error out rather than continue to run along without regard to the fact that your include failed. I made those changes but I get this error message now (that's to mac_Gyver's advice); 

Warning: require_once(page/site-header.php): failed to open stream: No such file or directory in /homepages/29/d1007800584/htdocs/page/tester-include.php on line 26

Fatal error: require_once(): Failed opening required 'page/site-header.php' (include_path='.:/usr/lib/php7.4') in /homepages/29/d1007800584/htdocs/page/tester-include.php on line 26

Sorry, forgive my ignorance but I'm really confused now as I cannot understand why it says homepages/29/etc. when the layout is as you describe; root/page/site-header.php

  • Solution

 /homepages/29/d1007800584/htdocs is your site root - this is set by your host at the server level. If site-header.php and tester-include.php are both in the /page/ directory, the include from tester-include.php should be simply `require_once('./site-header.php');` If that still doesn't work, try `require_once(dirname(__FILE__).'/site-header.php');` (if site-header.php is in a sub-directory called /root/, add that into the require).

As an aside, your host should be on php 8.1 at the least - 7.4 reached end of life in November 2022. You may want to ask them why they haven't upgraded yet. If they can't give you a decent answer, there are plenty of low-cost modern hosts around - ask here. Someone can point you in the right direction.

  • Like 1
26 minutes ago, maxxd said:

 /homepages/29/d1007800584/htdocs is your site root - this is set by your host at the server level. If site-header.php and tester-include.php are both in the /page/ directory, the include from tester-include.php should be simply `require_once('./site-header.php');` If that still doesn't work, try `require_once(dirname(__FILE__).'/site-header.php');` (if site-header.php is in a sub-directory called /root/, add that into the require).

As an aside, your host should be on php 8.1 at the least - 7.4 reached end of life in November 2022. You may want to ask them why they haven't upgraded yet. If they can't give you a decent answer, there are plenty of low-cost modern hosts around - ask here. Someone can point you in the right direction.

Thank you, thank you, thank you all so much! That was it! Phew! 

I'll ask my host about the outdated php.

Edited by Ocean_Voyager

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.