require_once, undefined function?

This is a discussion on require_once, undefined function? within the PHP Language forums, part of the PHP Programming Forums category; Say I have a script: http://www.mydomain.com/test/admin/foo.php That looks like this: <?php $baseDir = &...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-06-2004
Nik Coughin
 
Posts: n/a
Default require_once, undefined function?

Say I have a script:

http://www.mydomain.com/test/admin/foo.php

That looks like this:
<?php
$baseDir = "http://" . $_SERVER[ "SERVER_NAME" ] . "/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
-=-=-=-=-=-=-=-=-=-=-=-
http://www.nrkn.com/
-=-=-=-=-=-=-=-=-=-=-=-


Reply With Quote
  #2 (permalink)  
Old 10-06-2004
Daniel Tryba
 
Posts: n/a
Default Re: require_once, undefined function?

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


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

Reply With Quote
  #3 (permalink)  
Old 10-06-2004
Nik Coughin
 
Posts: n/a
Default Re: require_once, undefined function?

Daniel Tryba wrote:
> Nik Coughin <nrkn!no-spam!@woosh.co.nz> wrote:
>> $baseDir = "http://" . $_SERVER[ "SERVER_NAME" ] . "/test";
>>
>> require_once( $baseDir . '/helpers/helper.php' );
>>
>> something();
>>>

>>
>> something() is a function in helper.php.

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


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.


Reply With Quote
  #4 (permalink)  
Old 10-06-2004
Daniel Tryba
 
Posts: n/a
Default Re: require_once, undefined function?

Nik Coughin <nrkn!no-spam!@woosh.co.nz> wrote:
>
> 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.
>


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

Reply With Quote
  #5 (permalink)  
Old 10-06-2004
Tim Tyler
 
Posts: n/a
Default Re: require_once, undefined function?

Nik Coughin <nrkn!no-spam!@woosh.co.nz> wrote or quoted:

> I just want to be able to include files in other files from anywhere in
> my directory tree. [...]


I currently use the following for that:

function path_relative_to_here($path) {
$from = realpath(dirname($_SERVER["SCRIPT_FILENAME"]));
$to = realpath(dirname(__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_parts) - 1; $i >= 0; $i--) {
if ($base_parts[$i] == '') {
unset ($base_parts[$i]);
} else {
break;
}
}
for ($i = count($target_parts) - 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_parts); $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_parts);

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

To use it:

require_once(path_relative_to_here("relative_path_ to_file.inc.php"));

Improvements welcomed.
--
__________
|im |yler http://timtyler.org/ tim@tt1lock.org Remove lock to reply.
Reply With Quote
  #6 (permalink)  
Old 10-06-2004
==ACIDRAIN==
 
Posts: n/a
Default Re: require_once, undefined function?

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


http:// don't use absolute path.you may use related path or document
root environment variables.:-)

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

Reply With Quote
  #7 (permalink)  
Old 10-06-2004
Nik Coughin
 
Posts: n/a
Default Re: require_once, undefined function?

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

>
> I currently use the following for that:
>

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.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:56 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0