Jump to content


Photo

PHP Include not working

includes html directories css

Best Answer requinix, 15 May 2013 - 12:35 PM

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>
Go to the full post


  • Please log in to reply
9 replies to this topic

#1 port80

port80

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 15 May 2013 - 05:31 AM

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.



#2 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,149 posts
  • LocationWA

Posted 15 May 2013 - 12:35 PM   Best Answer

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>


#3 port80

port80

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 15 May 2013 - 08:39 PM

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



#4 port80

port80

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 16 May 2013 - 05:24 AM

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/



#5 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,663 posts
  • LocationMississauga, Canada

Posted 16 May 2013 - 11:20 AM

Well, go to newpage.php file and give us the result of:

// current directory
echo getcwd() . "\n";


#6 port80

port80

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 17 May 2013 - 01:02 AM

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>



#7 port80

port80

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 17 May 2013 - 01:55 AM

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



#8 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,663 posts
  • LocationMississauga, Canada

Posted 17 May 2013 - 05:55 AM

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, 17 May 2013 - 05:55 AM.


#9 port80

port80

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 26 May 2013 - 02:07 PM

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?



#10 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,663 posts
  • LocationMississauga, Canada

Posted 26 May 2013 - 05:41 PM

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com