Jump to content

PHP Include not working


Go to solution Solved by requinix,

Recommended Posts

Thank you for your time.

 

I am not sure where this really belongs the HTML, CSS, Application Design or one of the PHP topics. So I figured I would post here.

In my search I have found topics that come close to my question but have not provided a fix for my issue. So I'm asking for help. That and I am a beggining Web Developer.

 

 

Problem: the php includes I use are not displayed on web pages in subdirectories. If I add ../ to the php include so it is like this: <?php include ("../menu.php"); ?> on the newpage.php only some of the included files are shown on the page in the subdirectory. All links on the file newpage.php now act as though they were written <a href="../menu.php">link</a> and don't work. They are acctually written <a href="menu.php">link</a>.

 

Directory & Files

 

/

index.php

header.php

footer.php

menu.php <!-- contains all links for site -->

style.css

subdir/

newpage.php

 

 

newpage.php include code

 

<div id="links">
<?php
    include ("menu.php");
?>
</div>

 

 

 

Question? How do I fix it so that the menu.php and other included files are shown on the subdir pages? How do I make the links properly reflect the location. Since absolute or relative pathing appears to muck it up.

 

FYI: I do not have CLI access to the server. Only ftp access.

 

Thanks again for any help you may provide.

Link to comment
https://forums.phpfreaks.com/topic/278020-php-include-not-working/
Share on other sites

  • Solution

If the problem is that the include()s are simply not working then you need to use absolute paths when giving filenames: "menu.php" alone isn't enough.

include ($_SERVER["DOCUMENT_ROOT"] . "/menu.php");
If the problem is the links lead to 404s then you need to use absolute paths when giving URLs: "menu.php" alone isn't enough.

Menu

If the problem is that the include()s are simply not working then you need to use absolute paths when giving filenames: "menu.php" alone isn't enough.

include ($_SERVER["DOCUMENT_ROOT"] . "/menu.php");
If the problem is the links lead to 404s then you need to use absolute paths when giving URLs: "menu.php" alone isn't enough.

<a href="/menu.php">Menu</a>

Thank yo for your responce.

 

They are not getting 404 errors. The included files are just not showing up. I will give the $_SERVER["DOCUMENT_ROOT"]

 a try. I did not correctly interpret the use of $_SERVER["DOCUMENT_ROOT"]. I thought it was a setting in the PHP ini file that was set. Not something to be used in coding.

 

Thanks again,

I'll post back the status

Thank you requinix for responding.

 

I have tried these iterations and it is still not working. I just figured I would run through multiple posabilities.

 

1st attempt:

<div id="links">
<?php
    include ($_SERVER["DOCUMENT_ROOT"] . "menu.php");
?>
</div>

 

2cnd attempt:

<div id="links">
<?php
    include ($_SERVER["DOCUMENT_ROOT"] . "/menu.php");
?>
</div>

 

3rd attempt:

<div id="links">
<?php
    include ($_SERVER["../"] . "/menu.php");
?>
</div>

 

Though, if I understand it correctly it should be as you typed "DOCUMENT_ROOT". I was just trying ideas to see if it wold change anything.

 

4th attemp:

<div id="links">
<?php include ($_SERVER["DOCUMENT_ROOT"] . "/menu.php"); ?>
</div>

 

5th attemp:

<div id="links">
<?php
    include ($_SERVER["DOCUMENT_ROOT"] . "serverip/menu.php");
?>
</div>

 

6th attemp:

<div id="links">
<?php
    include ("serverip/menu.php");
?>
</div>

 

 

Am I missing something here? Everyting works for the directories above the subdir/ folder. The css works for the subdir/newpage.php. The included header, footer and menu pages are not showing for the subdir/newpage.php. Permissions are  755 or rwxr-xr-x for the subdir/ and files in the subdir/

It gives the path

 

/var/www/html/dd/subdir

 

I tried using thse a couple hours after my last post. They did not work either.

<div id="links">
<?php
    include ("serverip/var/www/html/dd/menu.php");
?>
</div>

 

And

 

<div id="links">
<?php
    include ("/var/www/html/dd/menu.php");
?>
</div>

The server just had updates applied. using <?php include ("../menu.php"); ?> works now when it did not before. I would rather the $_SERVER["DOCUMENT_ROOT"] would work. For now at least the ../ does link correctly now, when before it would assume the subdir path instead.

 

Thanks for your help :-)

Well, requinix's solution should not work according that log (/var/www/html/dd/subdir), it should be work only if you moved the menu.php file outside of the project.

 

In RedHat /CentOS /Fedora the web root directory is named html, so in that case "dd" is your project folder not a web root as you posted in your first post.

Edited by jazzman1
  • 2 weeks later...

Well, requinix's solution should not work according that log (/var/www/html/dd/subdir), it should be work only if you moved the menu.php file outside of the project.

 

In RedHat /CentOS /Fedora the web root directory is named html, so in that case "dd" is your project folder not a web root as you posted in your first post.

 

Thank you for the correction on the webroot. I can see the difference in the server root versus the directory root for a project, I'll keep that in mind for next time. I do have multiple project folders in the webroot of /var/www/html/  . So I understand you correctly, the $_SERVER["DOCUMENT_ROOT"] should not work as that refferences the /var/www/html/ and not /var/www/html/dd/ ? Is that right?

 

So the fix should really be the absolute path issue as requinix suggested?

 

 

So I understand you correctly, the $_SERVER["DOCUMENT_ROOT"] should not work as that refferences the /var/www/html/ and not /var/www/html/dd/ ? Is that right?

 

It depends how looks like the root directive in Apache conf, by default is:

DocumentRoot "/var/www/html"

When you create new projects on the same web root, you should avoid using absolute paths unless you really need them.

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.