Web Analytics Made Easy -
StatCounter include path question(s) - CodingForum

Announcement

Collapse
No announcement yet.

include path question(s)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • include path question(s)

    Hey All
    Trying to sort out an approach to includes.
    On a WAMP stack with a document root of C:\wamp\www\
    and a desire to employ an includes directory at C:\wamp\includes\
    from a working directory of C:\wamp\includes\someWebsite the following works fine...
    PHP Code:
    <?php
    $path 
    '../../includes';
    set_include_path(get_include_path() . PATH_SEPARATOR $path);
    echo 
    ini_get('include_path');
    include(
    'foo.php');// e.g. ../../includes/foo.php
    include('bar.php');//eg bar.php 
    ?>
    returning BOTH foo.php AND bar.php after outputting the include_path .;C:\php5\pear;../../includes
    My goal, however is to establish this on a permanent basis!
    (that is... where I no-longer have to keep track of how many DOTS & how many SLASHES are required to the includes from a given directory.)
    I gather that this can be accomplished in the php.ini, however include_path is NOT currently set in my php.ini so...
    1. I'm confused as to how and where the initial include path of .;C:\php5\pear is established, and...
    2. I'm concerned that -- ultimately -- I may NOT have access to php.ini in a shared host situation.
    In short, this is really giving me a headache.
    A little guidance here would sure be welcome.

  • #2
    Hi,

    To be honest I cannot see the problem... or the question. That is probably because of my poor English but as far as I understand you want to make include with relative paths? If I am right the best is to use following logic: http://cz.php.net/include_once (see my first post with chdir).

    But if you have different level directories then you still need to use dirname(__FILE__) + some function to join two urls. Here is the one I have created for that purpose:

    PHP Code:
    static function JoinUrlAndPath($url$path) {
        if (
    substr($path07) == 'http://') {
          return 
    $path;
        } else if (
    substr($path01) == '/') {
          if (
    preg_match('#http://[^/]*#'$url$match)) {
            return 
    $match[0].$path;
          }
        } else if (
    substr($path02) == '..') {
          
    // remove last slash in the url if such exists
          
    if (substr($urlstrlen($url)-11) == '/')
            
    $url substr($url0strlen($url)-1);
          
    $url substr($url0strrpos($url'/'));
          if (
    $url == 'http:/') return false;
          
    $path substr($path3strlen($path)-3);
          return 
    $this->JoinUrlAndPath($url$path);
        } else if (
    substr($path02) == './') {
          
    $path substr($path2strlen($path)-2);
          return 
    $this->JoinUrlAndPath($url$path);
        } else {
          
    $domainPath substr($url0strrpos($url'/'));
          if (
    $domainPath == 'http:/'$domainPath $url;
          return 
    $domainPath.'/'.$path;
        }
        return 
    false;
      } 
    of cause you will need to change function logic because that one joins HTTP url with relative path but the logic is the same... you have c:\somedir\pathhere\ and relative file url ./.././relative/path.php

    Case you need a help with that function I could rewrite it for you

    Comment


    • #3
      Thanks PHP6,
      I'll try again.
      Maybe (likely) "include_path" doesn't work the way I think it does. I could be wrong (happens quite a lot), but I read that...
      in a format similar to the system PATH environment variable: "include_path" specifies a list of directories where the functions -- require(), include(), file() et. al. -- will be directed look to find files specified in their arguments? It seems to me that this is not so much "make[ing] include with relative path" as it is hardwiring shortcuts to a list of absolute paths? Assuming that's not too far-off-base, it is my wish to simply add "c:\wamp\includes " to that list, under the impression that IN ANY SCRIPT, at ANY LEVEL DIRECTORY, where filename.ext is contained in said includes folder, passing filename.ext as an argument of a function call such as "include()", "require()", "file()" etc. will -- in fact -- return and load filename.ext while permitting successfull calls for otherfiles.ext in the current directory or other directories specified in the include_path ???? Again, I could be wrong, but it appears to me that this is exactly what the code posted above does... only temporarily.
      PHP Code:
      <?php 
      $path 
      '../../includes'set_include_path(get_include_path() . PATH_SEPARATOR $path); 
      ?>
      Again, it is my VAGUE understanding that one can hard-code the include_path directive in php.ini,
      however I am confused by the fact that while the include_path directive remains UN-SET in my php.ini (it is commented out)
      echo ini_get('include_path'); returns an include_path of .;C:\php5\pear
      while there exists NO SUCH DIRECTORIES as "php5" OR "pear" in the file system.
      Last edited by fuzzy1; Apr 11, 2009, 10:43 AM.

      Comment


      • #4
        Now I fully got you question... you want to make your script in the way so from other scripts you will just make include and you want PHP to search given directory for such files. To be hones I have never tried such things... I will try to find some solution.

        IMHO: I know that even using relative paths is not the correct style of software development and of cause relaying on some path in php.ini file or setting argument from script is even worse...

        Lets simple imagine situation, your script is going to be used on some shared hosting... I am more than sure that setting values in php.ini file will be automatically blocked by admin for security reasons... that mean your script will not work.

        Comment


        • #5
          I am more than sure that setting values in php.ini file will be automatically blocked by admin for security reasons... that mean your script will not work.
          I agree completely! Which of course begs the question of portability. I have read that you can include a distinct php.ini within your working directory... but I don't see how. I have also read that it may be possible to override via .htaccess, but I have VERY limited experience using .htaccess files. Maybe you know something about that?

          Comment


          • #6
            I have some experience with .htaccess and I know how to overwrite PHP settings within given directory but never used it (once I have tried such trick to show zend key directory but it does not work for me, may be you will have success, you need to make some tests). Here is the probably very easy doc about that: http://support.tigertech.net/php-value
            I have read that you can include a distinct php.ini within your working directory
            I have never heard about such thing and of cause never tried... but I guess there should be some assumption because logically if someone will have rights to use their own php.ini file, it could be easily used for hacking purposes

            Comment

            Working...
            X