require_once, undefined function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nik Coughin

    require_once, undefined function?

    Say I have a script:



    That looks like this:
    <?php
    $baseDir = "http://" . $_SERVER[ "SERVER_NAM E" ] . "/test";

    require_once( $baseDir . '/helpers/helper.php' );

    something();
    ?>

    something() is a function in helper.php.

    I get:
    Call to undefined function: something() in
    /home/mydomain/public_html/test/admin/foo.php on line 6

    The require_once doesn't fail (well, I don't get an error message), so I'm
    assuming that it finds helper.php OK. something() definitely exists in
    helper.php.

    If I put foo.php and helper.php in the same folder, lose all the $baseDir
    crap and use require instead of require_once it works fine (not sure if it's
    the $baseDir part or the require_once that is tripping it up), however I
    need them to be in seperate folders and I'd rather use require_once than
    require in case I end up including something twice by accident, it's so much
    easier not to have to worry about it.

    I've been fiddling with this for about an hour now and it's starting to get
    really annoying.

    Any ideas?

    --
    "Come to think of it, there are already a million monkeys on a million
    typewriters, and the Usenet is NOTHING like Shakespeare!" - Blair Houghton
    -=-=-=-=-=-=-=-=-=-=-=-

    -=-=-=-=-=-=-=-=-=-=-=-


  • Daniel Tryba

    #2
    Re: require_once, undefined function?

    Nik Coughin <nrkn!no-spam!@woosh.co. nz> wrote:[color=blue]
    > $baseDir = "http://" . $_SERVER[ "SERVER_NAM E" ] . "/test";
    >
    > require_once( $baseDir . '/helpers/helper.php' );
    >
    > something();
    > ?>
    >
    > something() is a function in helper.php.[/color]

    You are requesting the php file by http, that will probably mean that
    the php file in interpreted on that webserver and you are only getting
    yhe output of the script, _not_ the script _itself_ (jus tlike making
    the request from a webbrowser). If you really want to include remove
    script (and IMHO you shouldn'y), make sure you are actually getting php
    (either by outputting the unprocessed PHP or generating PHP by the use
    of PHP).

    --

    Daniel Tryba

    Comment

    • Nik Coughin

      #3
      Re: require_once, undefined function?

      Daniel Tryba wrote:[color=blue]
      > Nik Coughin <nrkn!no-spam!@woosh.co. nz> wrote:[color=green]
      >> $baseDir = "http://" . $_SERVER[ "SERVER_NAM E" ] . "/test";
      >>
      >> require_once( $baseDir . '/helpers/helper.php' );
      >>
      >> something();[color=darkred]
      >>>[/color]
      >>
      >> something() is a function in helper.php.[/color]
      >
      > You are requesting the php file by http, that will probably mean that
      > the php file in interpreted on that webserver and you are only getting
      > yhe output of the script, _not_ the script _itself_ (jus tlike making
      > the request from a webbrowser). If you really want to include remove
      > script (and IMHO you shouldn'y), make sure you are actually getting
      > php (either by outputting the unprocessed PHP or generating PHP by
      > the use of PHP).[/color]

      Well, that makes sense. I don't want to include a remote script. I just
      want to be able to include files in other files from anywhere in my
      directory tree. Maybe I should be putting the folders of files that I want
      included in my include path instead.


      Comment

      • Daniel Tryba

        #4
        Re: require_once, undefined function?

        Nik Coughin <nrkn!no-spam!@woosh.co. nz> wrote:[color=blue]
        >
        > Well, that makes sense. I don't want to include a remote script. I just
        > want to be able to include files in other files from anywhere in my
        > directory tree. Maybe I should be putting the folders of files that I want
        > included in my include path instead.
        >[/color]

        Good idea, alternatives are using relative paths (need extra work when
        script gets moved) or create an absolute path with eg document_root.

        --

        Daniel Tryba

        Comment

        • Tim Tyler

          #5
          Re: require_once, undefined function?

          Nik Coughin <nrkn!no-spam!@woosh.co. nz> wrote or quoted:
          [color=blue]
          > I just want to be able to include files in other files from anywhere in
          > my directory tree. [...][/color]

          I currently use the following for that:

          function path_relative_t o_here($path) {
          $from = realpath(dirnam e($_SERVER["SCRIPT_FILENAM E"]));
          $to = realpath(dirnam e(__FILE__)."/".$path);
          return relative_path($ to,$from);
          }

          function relative_path($ targetfile, $basedir = '.') {
          $basedir = realpath ($basedir);
          $targetfile = realpath ($targetfile);

          // on windows, check that both paths are on the same drive
          if (substr ($basedir, 0, 1) != substr ($targetfile, 0, 1)) {
          return false;
          }

          // split each path into its directories
          $base_parts = split ('\/', str_replace ('\\', '/', $basedir));
          $target_parts = split ('\/', str_replace ('\\', '/', $targetfile));

          // ensure that there are no empty elements at the end (c:\ would cause it)
          for ($i = count($base_par ts) - 1; $i >= 0; $i--) {
          if ($base_parts[$i] == '') {
          unset ($base_parts[$i]);
          } else {
          break;
          }
          }
          for ($i = count($target_p arts) - 1; $i >= 0; $i--) {
          if ($target_parts[$i] == '') {
          unset ($target_parts[$i]);
          } else {
          break;
          }
          }

          // get rid of the common directories at the beginning of the paths
          $common_count = 0;
          for ($i = 0; $i < count($base_par ts); $i++) {
          if ($target_parts[$i] == $base_parts[$i]) {
          $common_count++ ;
          } else {
          break;
          }
          }
          for ($i = 0; $i < $common_count; $i++) {
          unset ($base_parts[$i]);
          unset ($target_parts[$i]);
          }

          // build the resulting string
          $cnt = count($base_par ts);

          return str_repeat ('../', $cnt).implode('/', $target_parts);
          }

          To use it:

          require_once(pa th_relative_to_ here("relative_ path_to_file.in c.php"));

          Improvements welcomed.
          --
          __________
          |im |yler http://timtyler.org/ tim@tt1lock.org Remove lock to reply.

          Comment

          • ==ACIDRAIN==

            #6
            Re: require_once, undefined function?

            "Nik Coughin" <nrkn!no-spam!@woosh.co. nz> wrote in message news:<yhJ8d.102 69$JQ4.687507@n ews.xtra.co.nz> ...[color=blue]
            > Say I have a script:
            >
            > http://www.mydomain.com/test/admin/foo.php
            >
            > That looks like this:
            > <?php
            > $baseDir = "http://" . $_SERVER[ "SERVER_NAM E" ] . "/test";
            >
            > require_once( $baseDir . '/helpers/helper.php' );
            >
            > something();
            > ?>
            >[/color]

            http:// don't use absolute path.you may use related path or document
            root environment variables.:-)
            [color=blue]
            > something() is a function in helper.php.
            >
            > I get:
            > Call to undefined function: something() in
            > /home/mydomain/public_html/test/admin/foo.php on line 6
            >
            > The require_once doesn't fail (well, I don't get an error message), so I'm
            > assuming that it finds helper.php OK. something() definitely exists in
            > helper.php.
            >
            > If I put foo.php and helper.php in the same folder, lose all the $baseDir
            > crap and use require instead of require_once it works fine (not sure if it's
            > the $baseDir part or the require_once that is tripping it up), however I
            > need them to be in seperate folders and I'd rather use require_once than
            > require in case I end up including something twice by accident, it's so much
            > easier not to have to worry about it.
            >
            > I've been fiddling with this for about an hour now and it's starting to get
            > really annoying.
            >
            > Any ideas?[/color]

            Comment

            • Nik Coughin

              #7
              Re: require_once, undefined function?

              Tim Tyler wrote:[color=blue]
              > Nik Coughin <nrkn!no-spam!@woosh.co. nz> wrote or quoted:
              >[color=green]
              >> I just want to be able to include files in other files from anywhere
              >> in my directory tree. [...][/color]
              >
              > I currently use the following for that:
              >[/color]
              8<

              Thanks a lot Tim, I'll have a play with it. I've just popped everything
              into the same folder for now, but it's a bloody horrible messy way to do
              things.


              Comment

              Working...