Jump to content


Photo

PHP Include Statement


  • Please log in to reply
4 replies to this topic

#1 alfman05302001

alfman05302001
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 14 May 2006 - 04:28 AM

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

#2 _will

_will
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 14 May 2006 - 05:06 AM

instead of trying include('this' OR 'that') (which won't work...), somthing as simple as

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


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

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

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.

#3 alfman05302001

alfman05302001
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 14 May 2006 - 05:15 AM

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

if ($bIsTopLevel) {
   include('config.inc.php');
}
else {
   include('../config.inc.php');
}
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

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

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

#4 _will

_will
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 14 May 2006 - 05:25 AM

[!--quoteo(post=373662:date=May 14 2006, 01:15 AM:name=alfman05302001)--][div class=\'quotetop\']QUOTE(alfman05302001 @ May 14 2006, 01:15 AM) View Post[/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....

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



#5 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 14 May 2006 - 06:26 AM

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();


Life's too short for arguing.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users