Jump to content

PHP Include not working


port80

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
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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 :-)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 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?

Link to comment
Share on other sites

 

 

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.