Jump to content


Photo

Creating links that will always work inside include()


  • Please log in to reply
5 replies to this topic

#1 Anthony

Anthony
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 16 June 2006 - 06:00 PM

I'm trying to figure how I can make my all my includes always work no matter where I save the file that is calling the include.

For example.

Let's say I have file1 and it calls file2.php like so:

include('file2.php');

Now if I were to save file1.php into another folder the above would no longer work. I tried just adding a "/" in front of file2.php but that just errors out because it then goes back to the root of the PHP include path that is defined in php.ini.

In a perfect world, say I moved file1.php two folders deep for some reason. The include would still work because even though I never touched file1.php it would still write out in the code:

include('../../'file2.php');

So it seems that I would clearly need some variable that would update itself based on where the file and concatenate it to "file2.php" in this case.

I've tried a ton of $_SERVER variables though and have had no luck. I'm starting to get the feeling that I'm making this a lot harder than it actually is.

Any help would be greatly appreciated.
Donate to PHP Freaks:

#2 dptr1988

dptr1988
  • Members
  • PipPipPip
  • Advanced Member
  • 372 posts

Posted 16 June 2006 - 06:15 PM

If you have access to your 'php.ini' you could try setting the include_path variable
If you don't have access to 'php.ini' you could try using the $_SERVER['SCRIPT_FILENAME'] variable to determine the location of the script you are running and then adjust you include path.

Need more help with your project? One of the thousands of programmers, web designers or artists at <a href="http://www.rentacode...d_6764522">Rent A Coder</a> would be happy to help.

Disclaimer: Free advice is usually worth what you paid for it. ( or at least when it's coming from me! )

#3 Anthony

Anthony
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 16 June 2006 - 06:23 PM

[!--quoteo(post=384693:date=Jun 16 2006, 02:15 PM:name=dptr1988)--][div class=\'quotetop\']QUOTE(dptr1988 @ Jun 16 2006, 02:15 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
If you have access to your 'php.ini' you could try setting the include_path variable
If you don't have access to 'php.ini' you could try using the $_SERVER['SCRIPT_FILENAME'] variable to determine the location of the script you are running and then adjust you include path.
[/quote]

I've been playing around with PATH_TRANSLATED and others but they seem to tell me the where the include file is sitting not really where the file itself is sitting.

My system ideally would work like this:

1. A config.php file that sets variables and is included on every page
2. The variables that are set in config.php are then used inside the page that is called to update the includes so t he page can always find the included files no matter where they are saved.
Donate to PHP Freaks:

#4 dptr1988

dptr1988
  • Members
  • PipPipPip
  • Advanced Member
  • 372 posts

Posted 16 June 2006 - 06:29 PM

I don't think I understand what you are trying to do. If I have it right, you have a include file that is always moving around and file1.php and file2.php are staying in the sampe place? Or are file1.php and file2.php moving around? It would help me if you could post the which files are moving, which ones are not.



Need more help with your project? One of the thousands of programmers, web designers or artists at <a href="http://www.rentacode...d_6764522">Rent A Coder</a> would be happy to help.

Disclaimer: Free advice is usually worth what you paid for it. ( or at least when it's coming from me! )

#5 Anthony

Anthony
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 16 June 2006 - 06:39 PM

[!--quoteo(post=384698:date=Jun 16 2006, 02:29 PM:name=dptr1988)--][div class=\'quotetop\']QUOTE(dptr1988 @ Jun 16 2006, 02:29 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I don't think I understand what you are trying to do. If I have it right, you have a include file that is always moving around and file1.php and file2.php are staying in the sampe place? Or are file1.php and file2.php moving around? It would help me if you could post the which files are moving, which ones are not.
[/quote]

The include files are always staying in the same place. There will be a config file, a header file, nav file, footer file, etc.

The pages could potentially be contained anywhere inside of the site. Now with HTML its easy, put a "/" before linking to any file and it always goes back to the server root and finds the file itself. With PHP its not so easy....or so I think.
Donate to PHP Freaks:

#6 dptr1988

dptr1988
  • Members
  • PipPipPip
  • Advanced Member
  • 372 posts

Posted 16 June 2006 - 06:50 PM

So you want to find the absolute path of your include files? The filenames in PHP need to be based on the real filesystem of your server rather then the virtual one that HTML uses.
Does you $_SERVER['SCRIPT_FILENAME'] variable work? If it does then you can use that varible in your include files to find the absolute path to them. Then you could use the full path every time you included those files in the pages. I don't know what else to do ( in my limited knowledge ) if you can't use the $_SERVER['SCRIPT_FILENAME'] variable.
Need more help with your project? One of the thousands of programmers, web designers or artists at <a href="http://www.rentacode...d_6764522">Rent A Coder</a> would be happy to help.

Disclaimer: Free advice is usually worth what you paid for it. ( or at least when it's coming from me! )




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users