PHP Include Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
HokieTracks
Forum Newbie
Posts: 9
Joined: Tue Mar 24, 2009 5:45 pm

PHP Include Problem

Post by HokieTracks »

Hi,
On my website I have an external style sheet which affects a layout.php file. I use a include command to place this layout.php file on every page I want it. This works great on every single file that is in the same folder. But, when I try using it in a different folder it doesn't work. I have no errors just nothing displays.

Here is the code for layout.php:
<html>
<div id="header">
<header>
<a href="http://hokietracks.com"><img src="http://hokietracks.com/index/images/linkedlogo.png" border=0></a>
</header>
</div>

<body>
<link rel=StyleSheet href="http://hokietracks.com/index/style.css"
type="text/css">

<div id="quote">
<?php

$quotes[] = "That was then, I bleed orange and maroon now. -JT Thompson";
$quotes[] = "We will prevail, we are Virginia Tech. -Nikki Giovanni";
$quotes[] = "I don't know what a Hokie is but god is one of 'em. -Lee Corso ESPN";
$quotes[] = "One minor set back for one major comeback. -Victor Macho Harris";

srand ((double) microtime() * 1000000);
$randomquote = rand(0,count($quotes)-1);

echo "<p>" . $quotes[$randomquote] . "</p>";

?>
</div>

<div id="search">
<form action="http://hokietracks.com/index/search.php" id="cse-search-box">
<div>
<input type="hidden" name="cx" value="partner-pub-5087843239980019:43xe111o30d" />
<input type="hidden" name="cof" value="FORID:11" />
<input type="hidden" name="ie" value="ISO-8859-1" />
<input type="text" name="q" size="15" />
<input type="submit" name="sa" value="Search" />
</div>
</form>

<script type="text/javascript" src="http://www.google.com/coop/cse/brand?fo ... "></script>
</div>


<div style="margin-top: -56px">
<div style="margin-left: 100px">
<div id="outside">
<ul id="navigation-1">
<li><a href="" title="All Headlines" target="_self" >News</a>
<ul class="navigation-2">
<li><a href="http://hokietracks.com/index/forums/football.php" title="Football" target="_self" >Football</a></li>
<li><a href="#" title="Men's Basketball" target="_self" >Men's Basketball</a></li>
<li><a href="#" title="Women's Basketball" target="_self" >Women's Basketball</a></li>
</ul>
</li>
<li><a href="#" title="Forums" target="_self" >Forums</a>
<ul class="navigation-2">
<li><a href="#" title="sample" target="_self" >sample</a></li>
<li><a href="#" title="sample" target="_self" >sample</a></li>
<li><a href="#" title="sample" target="_self" >sample</a></li>
</ul>
</li>
<li><a href="#" title="sample" target="_self" >sample</a>
<ul class="navigation-2">
<li><a href="#" title="sample" target="_self" >sample</a></li>
<li><a href="#" title="sample" target="_self" >sample</a></li>
<li><a href="#" title="sample" target="_self" >sample</a></li>
</ul>
</li>
<li><a href="#" title="sample" target="_self" >sample</a>
<ul class="navigation-2">
<li><a href="#" title="sample" target="_self" >sample</a></li>
<li><a href="#" title="sample" target="_self" >sample</a></li>
<li><a href="#" title="sample" target="_self" >sample</a></li>
</ul>
</li>
</ul>
</div>

</style>
<div id="menu-vertical">
<div style="margin-top: 3px">
<div style="margin-left: -145px">
<div class="buttonscontainer">
<div class="buttons">
<a href="http://hokietracks.com/index/home.php">Home</a>
<a href="">Login</a>
<a href="http://hokietracks.com/index/register.php">Register</a>
<a href="">News</a>
<a href="http://hokietracks.com/index/photo.php">Photo of the Week</a>
<a href="http://hokietracks.com/index/video.php">Video of the Week</a>
<a href="">Forums</a>
<a href="">Links</a>
<a href="">About Us</a>
<a href="">Contact Us</a>
</div>
</div>
</div>
</div>
</div>
</div>

<div id="ads">
<script type="text/javascript"><!--
google_ad_client = "pub-5087843239980019";
/* 120x600, created 3/28/09 */
google_ad_slot = "4627603943";
google_ad_width = 120;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</dv>

<title>Hokie Tracks</title>
<link rel="shortcut icon" href="http://hokietracks.com/index/images/favicon.ico">
<link rel="icon" type="image/gif" href="http://hokietracks.com/index/images/ani ... vicon1.gif">

</body>
</html>
Here is one of the files in a different folder:
<body>
<html>

<?php
include ("http://hokietracks.com/index/layout.php");
?>

<div id="contnt">
<div id="h1">
Football Forums
</div>
</div>

</body>
</html>
tech603
Forum Commoner
Posts: 84
Joined: Thu Mar 19, 2009 12:27 am

Re: PHP Include Problem

Post by tech603 »

You should use the relative path for your link to the style sheet. For instance if your stylesheet is in the same folder you are processing your code you should do something like this:
<link rel=StyleSheet href="style.css" type="text/css">
Now lets say you go into a folder above this you would do something like this

Code: Select all

<link rel=StyleSheet href="../style.css" type="text/css">
And for every folder you navigate away from this style sheet, you would follow sequence ../.../style.css would be if you were two folders deep. ect..

Hope this helps
HokieTracks
Forum Newbie
Posts: 9
Joined: Tue Mar 24, 2009 5:45 pm

Re: PHP Include Problem

Post by HokieTracks »

Yes, I know that. But, my style sheet is defined in my layout.php which is included in every page. But, when I try to include layout.php anywhere outside of the same folder nothing appears not even text unaffected by the layout.php or the style sheet which is defined in the layout.php file. If that makes sense.
tech603
Forum Commoner
Posts: 84
Joined: Thu Mar 19, 2009 12:27 am

Re: PHP Include Problem

Post by tech603 »

Code: Select all

 
include ("http://hokietracks.com/index/layout.php");
Same thing applies here, your trying to include a file via http, which i've seen cause issues in the past.


From php.net
Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E.g. if your include_path is libraries, current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/libraries/ and then in /www/include/. If filename begins with ./ or ../, it is looked for only in the current working directory or parent of the current working directory, respectively.

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.
I would suggest switching to use a relative path and not the url, if that doesn't work then we have atleast excluded that theory.

Hope that helps
HokieTracks
Forum Newbie
Posts: 9
Joined: Tue Mar 24, 2009 5:45 pm

Re: PHP Include Problem

Post by HokieTracks »

Well I tried this:

Code: Select all

<body>
<html>
 
<?php
include ("/index/layout.php");
?>
 
<div id="contnt">
<div id="h1">
Football Forums
</div>
</div> 
 
</body>
</html>
And now I at least got an error message meaning its trying to do something.
Warning: include(/index/layout.php) [function.include]: failed to open stream: No such file or directory in /home/techair/public_html/index/forums/football.php on line 5

Warning: include() [function.include]: Failed opening '/index/layout.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/techair/public_html/index/forums/football.php on line 5
Football Forums
This makes no sense because I am sure the file exists.
tech603
Forum Commoner
Posts: 84
Joined: Thu Mar 19, 2009 12:27 am

Re: PHP Include Problem

Post by tech603 »

Code: Select all

include ("/index/layout.php");
Try removing the beginning / if that doesn't help try doing it like this

Code: Select all

include ("../index/layout.php");
Both should work. Let me know if that helps
HokieTracks
Forum Newbie
Posts: 9
Joined: Tue Mar 24, 2009 5:45 pm

Re: PHP Include Problem

Post by HokieTracks »

tech603 wrote:

Code: Select all

include ("/index/layout.php");
Try removing the beginning / if that doesn't help try doing it like this

Code: Select all

include ("../index/layout.php");
Both should work. Let me know if that helps
Neither way works. Is it maybe trying to find the file in the wrong place. Like http://hokietracks.com/index/forums/index/layout.php instead of http://hokietracks.com/index/layout.php
Post Reply