Jump to content

Creating links that will always work inside include()


Anthony

Recommended Posts

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:

[code]include('file2.php');[/code]

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:

[code]include('../../'file2.php');[/code]

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[!--quoteo(post=384693:date=Jun 16 2006, 02:15 PM:name=dptr1988)--][div class=\'quotetop\']QUOTE(dptr1988 @ Jun 16 2006, 02:15 PM) [snapback]384693[/snapback][/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.
Link to comment
Share on other sites

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.


Link to comment
Share on other sites

[!--quoteo(post=384698:date=Jun 16 2006, 02:29 PM:name=dptr1988)--][div class=\'quotetop\']QUOTE(dptr1988 @ Jun 16 2006, 02:29 PM) [snapback]384698[/snapback][/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.
Link to comment
Share on other sites

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.
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.