Perhaps someone can help.

I will have several php scripts located in:

/home/myname/www/filesdir

It is myunderstanding that MySQL 'connect' password, etc. within an 'include' file should NOT be web accessible.

So I would like to put the 'include. file in:

/home/myname/private

What I don't know is how to code the include statement in the /home/myname/www/filesdir/myscript.php file.

And would it be different if I used:

/home/myname/www/subdir/filesdir/myscript.php instead?

Umh, the above is all on a Unix, of course, but can I use the same coding to test on a WIN2K PC environment?

Thanks a lot for any help.

    Simply put a file called "db_connect.inc" or whatever else you want to name it that connects to your DB in your current include directory and put the following in any page that needs to use your DB.

    Include "db_connect.inc";

    Just make sure your include directory is outside the root of your site and you should be fine.

      Thanks, but this doesn't solve the problem. My 'include' directory will be back several levels (as per my example), and just a raw Include "db_connect.inc"; won't access it.

      That's what I'm stumped about...how to do it!

        Whatever directory you put it in should be in your include directory in your php.ini file. Then by just saying include 'this', it will look through the directories specified in your ini file. You don't need to say include 'absolute/path/to/this/script'

          LordShryku:

          Thanks for the reply.

          However, the 'include' directory on my ISP's server (and others who may use the script) may be different that I have no control over. I just need to know how to 'call' it from within a .php script IF it is in a unique location on the server BELOW 'www' accessible files.

            all i know is that u cant access a file which is not included on your WWW directory where in the WWW is your root or absolute path

              in your phpinfo.php file, see if your include_path contains '.'

              example:
              include_path = ".:/home/myname/www/filesdir"

              If the '.' is there, you can include files relative to wherever your current script is. Then you can create your own include directory outside of the public folder and just path to that with your include calls.

              example:
              <?
              include("../../includes/db_conn.php");
              ?>

                jrbarkhu:

                Thank you for the examples. Sorry to be so 'dense' about this, but that's why I posted! And I'm still confused.

                (EDIT🙂 Darn... backslashes will NOT display correctly below :mad:

                I'll have to use '&' in place of a BACKslash

                My Include Paths:
                1. On My PC: .;c:&php4&pear
                2. On Unix:  .:/usr/local/lib/php
                

                So, I apparently must use 'different' statements whether I'm on the PC environment or the Unix?

                If my php script calling an include is at sub-dir:

                1. PC = c:&www&test&blah
                2. Unix=/home/myname/www/mydomain/mydir 
                

                And my include file is actually at:

                1. PC = c:&private
                2. Unix=/home/myname/private
                

                What do I use? If I'm testing a script on the PC, do I then need to change the location for it to work on the Unix, or can there be TWO separate include statements (one for PC, one for Unix)?

                Note: I do NOT have 'root' access on my ISP's AIX Unix to change any 'path' statements.

                --- Currently sitting on the 'Dunce Stool', I am...

                  Just to be on the safe side, name your include file "include.php" instead of "include.inc". Reason being is that if for some odd reason someone were to be able to access your .inc file via a web browser, it will print the .inc file out as text, or allow them to download it. If it's a .php file, they won't be able to see anything.

                  Just a note.

                    The good news is that it looks like your include_path will allow for relative pathing. Therefore, in order for your include calls to seamlessly work on both Unix and PC, create two identical directories (one for Unix, one for PC) and save these directories in the same position relative to their respective root public directories - perhaps on the same level as the root public directory. This is also assuming that both machines have matching file structures within each root public directory.

                    Example:

                    The public folder on your Unix machine is:
                    Unix=/home/myname/www/mydomain/mydir

                    The include folder on your Unix machine would be:
                    Unix=/home/myname/www/mydomain/includes

                    The public folder on your Windows machine is:
                    c:&www&test&blah

                    The include folder on your Windows machine would be:
                    C:&www&test&includes

                    An include call from a file saved in the root public folder would look something like this:
                    <?
                    include("../includes/file.php");
                    ?>

                      Well, I really appreciate all the help efforts here, but perhaps I am still miscommunicating.

                      I just setup up a test file on the Unix:

                      <? 
                      include("../includes/myinclude.php"); 
                      ?>
                      

                      And my file to include on the Unix:

                      <?php
                      echo "This is from the myinclude.php file";
                      ?>
                      

                      My Result (one of numerous errors):

                      Warning: main(../includes/myinclude.php): failed to open stream: No such file or directory in /home/myname/www/mydomain/mytest/mymaintest.php on line 2
                      
                      Warning: main(): Failed opening '../includes/myinclude.php' for inclusion (include_path='.:/usr/local/lib/php') in /home/myname/www/mydomain/mytest/mymaintest.php on line 2
                      

                      One of various errors from the following combinations tried:

                      1. Putting the mymaintest.php file in:
                        /home/myname/www/mydomain/mytest/ (subdir)

                      2. Putting the myinclude.php file in:
                        A. /home/myname/nonwww/ (subdir)
                        B. /home/myname/nonwww/includes (subdir)
                        C. /home/myname/www/mydomain/ (subdir)
                        D. /home/myname/www/mydomain/mytest/ (subdir)
                        E. /home/myname/www/mydomain/mytest/includes (subdir)

                      My gosh, this should not be THAT difficult I would think???

                      The objective is to NOT have an include file be an '.inc' file, AND...AND... NOT be in a Publicly assessible directory or sub-directory.

                      Right now, I can't get squat to work :mad:

                      Typically, for NOT DBconnect, Password, etc. 'sensitive' files, my regular desire would be to have an 'includes' sub-directory right off the working .php script file (wherever that might be).

                      But for the DON'T give the World access types, then BELOW 'www' ... on the Unix AND for testing on the PC.

                      ARRRGGGHHHHH!!!

                        Well, I went to php.net and am now trying to make some sense out of the following:

                        Stan Insyder
                        06-Jan-2003 04:39
                        It appears that a *nix machine and a Win machine handle includes a bit differently. (This applies for require() also).

                        Here's an example to explain the problem... I have a file in the root dir /index.php which includes a file in /functions/ setting my includes to functions/file.php works fine, but if I include a file from inside file.php to say file2.php a *nix based machine looks for a file as if it were starting off in the root, while a win machine stays in the functions directory, where the last file was included. I've had this problem on two win server's and two linux servers, so I'm pretty sure it's not my config error.

                        Anyway, this is my quick way of solving the problem.

                        if (eregi("nix",$_SERVER['SERVER_SOFTWARE'])) {

                        Unix/Linux style include

                        $includeDirPath = "functions/";
                        }
                        else if (eregi("nix",$_SERVER['SERVER_SOFTWARE'])) {

                        Windows style include

                        $includeDirPath = "";
                        }

                        Obviously with every include I typed it out as such... include $includeDirPath . "file.php";

                        Hope that helps anyone experiencing the same problem as I was!

                        Umh, both of the 'eregi' lines sure look the same to me...how can one be for Unix and the other for Windows???

                        I have no clue about this:

                        pelican at thajungle dot com
                        01-Oct-2002 11:04
                        In order to get Windows 2K Server, php 4.2.3, and Apache 1.3.24 all to play nice together, I had to make one of the "include_path" arguments "/." NOT "."

                        Hope this helps.

                        ARRRGGGHHHH.

                        If my test script is in: /home/myname/www/mydomain/test.php ...
                        and if I set up:
                        /home/myname/www/mydomain/includes ...
                        and stick myinclude.php in this subdirectory ...
                        and use:

                        ./includes/myinclude.php (in my test file)...

                        Yes, that works. But this does NOT solve my problem for placing 'include' files OUTSIDE OF PUBLIC WWW ACCESS...and I am NOT referring to a Public_Html folder.

                        I can NOT modify or access ANYTHING inside of my ISP's 'root' privileges realm.

                        ARRRGGGHHHHHH.

                          Well, I may have a 'workaround' for the Unix:

                          <? 
                          include("/home/myname/includes/myinclude.php"); 
                          ?>
                          

                          This puts the include OUTSIDE of public access (www), but isn't there still a risk of making it so easy for someone (especially if on the same ISP Unix machine) to find my DB Password, etc. file???

                            Write a Reply...