Jump to content

Archived

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

alfman05302001

PHP Include Statement

Recommended Posts

Ok so I am sure this is a fairly simple problem, but I am not quite sure how to go about fixing it. I am using an include statement on my website and it works in every page except the top index page. The problem is the way I am linking to the file. I am wrting my includes as include(..//directory/file.php).

As I stated this works everywhere except the top level. I need to know if there is a way to write it so it can work on both top level and sub level files.

Website is [a href=\"http://www.oalodge43.org\" target=\"_blank\"]www.oalodge43.org[/a]

The site originaly started as a static html site (hence the .html extentions (php is processing it as a php file)) made from a Dreamweaver template that every page on the website is tied to.

Is there a way to do something like this "include('this' OR 'that'); that will not produce erros or warnings

Thanks for your help
Jonathan

Share this post


Link to post
Share on other sites
instead of trying include('this' OR 'that') (which won't work...), somthing as simple as

[code]
if ($bIsTopLevel) {
   include('config.inc.php');
}
else {
   include('../config.inc.php');
}
[/code]


Another alternative would be to move all your pages to the second level of your web root, and just have an index.php in the top level. All of the files that you had originally in the top level could be moved to /main or /static. In the index you put

[code]
header('Location: main/index.php');
[/code]

When a user accesses your site they will get forwarded automatically to your sublevel, and you don't have to worry about the headache of finding the right path based on which page the user is viewing, because they are always ../ from the web root.

Share this post


Link to post
Share on other sites
[!--quoteo(post=373659:date=May 14 2006, 01:06 AM:name=_will)--][div class=\'quotetop\']QUOTE(_will @ May 14 2006, 01:06 AM) [snapback]373659[/snapback][/div][div class=\'quotemain\'][!--quotec--]
instead of trying include('this' OR 'that') (which won't work...), somthing as simple as

[code]
if ($bIsTopLevel) {
   include('config.inc.php');
}
else {
   include('../config.inc.php');
}
[/code]
Another alternative would be to move all your pages to the second level of your web root, and just have an index.php in the top level. All of the files that you had originally in the top level could be moved to /main or /static. In the index you put

[code]
header('Location: main/index.php');
[/code]

When a user accesses your site they will get forwarded automatically to your sublevel, and you don't have to worry about the headache of finding the right path based on which page the user is viewing, because they are always ../ from the web root.
[/quote]


The second option I would require me to redo all the linking for my pages as they were not relative (for some strange reason when I coded the site I used the full url). The first option would work, but I would need to use it on a second page too as the first include statement has a page that has its own include statement (the db connection info) but the code should be the same I think for both pages (is that correct?). The one peice that confuses me is how the variable is defined?

Thanks for your help and fast reply

Jonathan

Share this post


Link to post
Share on other sites
[!--quoteo(post=373662:date=May 14 2006, 01:15 AM:name=alfman05302001)--][div class=\'quotetop\']QUOTE(alfman05302001 @ May 14 2006, 01:15 AM) [snapback]373662[/snapback][/div][div class=\'quotemain\'][!--quotec--]
The second option I would require me to redo all the linking for my pages as they were not relative (for some strange reason when I coded the site I used the full url). The first option would work, but I would need to use it on a second page too as the first include statement has a page that has its own include statement (the db connection info) but the code should be the same I think for both pages (is that correct?). The one peice that confuses me is how the variable is defined?
[/quote]

PHP will include files with a path relative to the original script. So if your index2.php includes a file that includes another file PHP won't find it because it is looking relative to the original script, which is in a different location than the other files that include your class...

Try and keep all your includes in the same directory, or, since we previously defined $bIsTopLevel, in your include you could do another if-then....

[code]
if (isset($bIsTopLevel) && $bIsTopLevel == true) {
  include('../mysql_access.inc.php');
}
else {
  include('../../mysql_access.inc.php');
}

[/code]

Share this post


Link to post
Share on other sites
you can use absolute path:
if you use a hosting server, your root directory might be like this:

/home/yourusername/

your html document maybe at /home/yourusername/public_html
so, the file to include can be /home/yourusername/public_html/include/file.php

if you don't know the root directory put this in a any php file to find out:

echo getcwd();

Share this post


Link to post
Share on other sites

×

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.