Jump to content

misleading paths !


ajoo

Recommended Posts

Hi all !

 

My program works just fine on xampp but the exact same code ported over to VM running ubuntu12.04 / apache 2.4/ php 5.6 and mysql 5.5 gives errors. These errors occur when the program executes the require_once / include commands with paths. More specifically the following works on xampp :

require_once("/path/to/file.php")

but on the VM this gives a "no such file or folder present" error. On the VM the following works :

require_once("path/to/file.php") 

I wonder if this is a know issue and that on a linux server the paths are defined like this or do they change from one linux strain to another? I have not decided on a web-host just yet and so I am just wondering if I should make changes to the paths.  

 

Thanks all !

 

Link to comment
Share on other sites

Either the path is correct, or it isn't. This has nothing to do with VMs, the Linux distribution or whatever.

 

If you're trying to use absolute Windows paths on a Linux machine, that of course won't work, because the two operating systems have entirely different filesystem layouts. That's why I suggested to create the paths dynamically based on an environment variable which contains the application directory:

<?php

require_once getenv('MY_APP_BASE_DIR').'/path/to/file/within/base/directory';

$_SERVER['DOCUMENT_ROOT'] is usually the wrong answer, because that implies your entire application resides in the document root -- which it shouldn't. Only the frontend scripts like index.php belong there, the core application should be outside of any publicly accessible directory.

Link to comment
Share on other sites

 

For what it's worth, you can leave the root with

$_SERVER['DOCUMENT_ROOT']/../

 

 

An application doesn't need to now the entire path to where it sits. It can just start from where it is.

 

For example:

Say the app is here

/some/long/path/to/app/root

 

And your root/index.php needs to reference a file in root/includes, all you need is include './includes/somefile.php'

To go one directory above the app root you would just go include '../logs/anotherfile.php'

Link to comment
Share on other sites

An application doesn't need to now the entire path to where it sits. It can just start from where it is.

 

It depends. What about includes within includes? If the primary include file is being brought in at different levels, document-relative links can be problematic.

 

I also prefer to use root-relative links so that I can duplicate a page without needing to modify the path to the website template, for example. I may be duplicating a page that ends up in a different directory.

Link to comment
Share on other sites

What about includes within includes?

 

It works the same way with a slight modification depending on who is calling who. I am working on an app where an include form includes an ajax chained select form that is also in the includes. No problem.

 

I also prefer to use root-relative links so that I can duplicate a page without needing to modify the path to the website template, for example. I may be duplicating a page that ends up in a different directory.

 

I was going to edit my post that that is a side benefit. You can move the app around without having to mess with changing paths.

 

Interested in @Jaques1 comments on this method.

Link to comment
Share on other sites

Relying on relative paths is not a good idea.

 

First, it's always unclear what the paths are relative to. Different PHP APIs handle this differently, and if any part of the application (maybe a library) calls chdir(), you're screwed.

 

Traversing deep directory structures quickly leads to incomprehensible paths like

../../../some/script.php

Changing the structure is also hell, because all paths depend on each other. If you're lucky, your IDE will assist you, but I wouldn't bet on that.

 

And relative paths again assume that the entire application sits in the document root, which really shouldn't be the case for any modern project. This greatly increases the attack surface and the risk of leaking internal data (configuration files etc.).

 

Applications today should be split into a public interface within the document root (all scripts which are meant to be directly accessible) and the application core outside of the document root. All paths should start with the (absolute) path of the core directory which can be stored in an environment variable, as explained in #3:

require_once getenv('MY_APP_BASE_DIR').'/path/to/file/within/core/directory';

Even better: Use the environment variable to include a configuration script which defines shortcuts for all relevant directories.

// include bootstrapping script which provides core features
require_once getenv('MY_APP_BASE_DIR').'/bootstrap.php';

// now use specialized configuration parameters or functions to resolve paths
load_library('foo/bar.php');
load_functions('database.php');
load_functions('security.php');
Link to comment
Share on other sites

  • 1 month later...

Hi Guru Jacques !

 

getenv('MY_APP_BASE_DIR') yields nothing. Do I have to define  'MY_APP_BASE_DIR'  somewhere for this to take effect ?

 

Maybe something like this in the folder containing the index.php: 

 define('MY_APP_BASE_DIR', dirname(__FILE__));

Thanks 

Link to comment
Share on other sites

Hi Guru Jacques, 

 

Thanks for the reply,

 

The Environment variable is now echoing out in PHP.

 

Just for my information, is there some command to help me check, inside ubuntu/linux, for the value i set for SetEnv variable in Virtualhost ? I tried printenv but it does not list the environment variable I defined in the Virtual host. 

 

Thank you.

Link to comment
Share on other sites

Archived

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

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