Jump to content

PHP Include Statement


alfman05302001

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
Link to comment
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.
Link to comment
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
Link to comment
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]
Link to comment
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();

Link to comment
Share on other sites

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.