not using full url

He has: 688 posts

Joined: Feb 2001

I'll try to describe this quickly. It's a php problem but it may have a simple html solution. I am establishing a variable in php on a primary page before calling an include file that will make use of the variable. This worked in my tests as the variable (ie. $test = "this is a test") was able to be printed from the other file.

But when I changed the include from just the file name in the same folder "test.php" to the full url "http://www.mysite.com/test.php", everything fails!

In my site, files are not all in the same folders so I need to find other ways I can call write the name of a file but without using it's full url? ../ may work. Does ../subfolder/file.php mean 'go to the top/main folder then work down again'? Any other methods? Is there a function in php that will stick the domain part in for you?

Just looking for work around suggestions.
Thanks.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

../ will work without issue.

Other solutions are to set it up to read the path instead of the directory (relative path works?).

Dean (textism.com) had an interesting solution for his /refer/ script (see his site for details, and that code for more details).

He has: 688 posts

Joined: Feb 2001

../ didn't work either. It spits out:

Warning: Failed opening '../header.php' for inclusion (include_path='') in ......

So far I know that including "header.php" will work. Using the full url will not create an error but the variable value will not carry thru to the included file. And Using ../ creates complete failure to even include the second page.

Sad Any other ways?

Busy's picture

He has: 6,151 posts

Joined: May 2001

if ../ didnt work, try ../../ or even ../../foldername/

it all depends where the folders are and from where your starting from. example using a folder 2 back, 1 over is different if the calling files is in a upper directory than the lower one
Take a quick peek at [url=http://ezhtml.net/ezgraphics.html]here[/ur] for a directory tree example (third way down)

He has: 1,016 posts

Joined: May 2002

/ = root directory (in PHP it's the server's root dir, but in HTML it's your domain's root dir)
./ = same directory
../ = One directory back
../../ = Two directories back

If you can't get it to work, then write us the FULL URL where your PHP script is, and also where is the file you're trying to include. This way it will be a lot easier for us to help you.

mairving's picture

They have: 2,256 posts

Joined: Feb 2001

Try using this:

<?php
include(\"$DOCUMENT_ROOT/test.php\");
?>

This will bring up the file to your public_html directory which would translate to http://yoursite.com/test.php

Mark Irving
I have a mind like a steel trap; it is rusty and illegal in 47 states

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Right, and Dean's solution was to make $DOCUMENT_ROOT a smaller variable for multiple uses.

mairving's picture

They have: 2,256 posts

Joined: Feb 2001

Quote: Originally posted by Suzanne
Right, and Dean's solution was to make $DOCUMENT_ROOT a smaller variable for multiple uses.

Yeah, I guess if I would have clicked on that link and looked around. Using $DOCUMENT_ROOT is a quick and dirty approach. A better solution generally is creating at least two variables. One that is the Root or whichever directory you want to be root and another variable for the system path (i.e. /home/username/directory). The second variable is useful for calling stuff that is in directories under the www directory which means that they won't show up over the web. A good place to stick usernames and passwords for databases.

Mark Irving
I have a mind like a steel trap; it is rusty and illegal in 47 states

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

heh, I meant for fifeclub to look around. Wink

He has: 688 posts

Joined: Feb 2001

Thanks everybody. What I ended up doing was taking everybody's suggestions and a ton of examples from php.net and I tested all of them. Out of 12 testpages, I got 3 methods to work (call the other page and properly pass the variable to that other page). One way was using the ../ but I found that it could be dependant on how many folders down you were so that left two ways:

1) include("$DOCUMENT_ROOT/includes/header.php");
(and a 'require' version of this document_root method)
2) include "/home/mikesus/public_html/includes/header.php";
(which is just using the full path name)

Smiling

One final question though. An alternate 'cousin' of the document_root method worked but used "require" instead of "include".
( require($DOCUMENT_ROOT . "/includes/header2.php"); )
Is there an advantage/disadvantage to using require instead of include?

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Quote: Originally posted by fifeclub
Is there an advantage/disadvantage to using require instead of include?

yeah, i've been wondering that my self, I usually use the require instead if include - I just didn't want to make a new thread about it...

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Quote: From PHP.net
The documentation below also applies to require(). The two constructs are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless.

He has: 688 posts

Joined: Feb 2001

Last update from me on this subject. I started putting the DOCUMENT_ROOT method of calling my header on every page. It worked fine everywhere until I got to my photo album area ("Gallery"). For some unknown reason DOCUMENT_ROOT won't work here, but the 'full path' method does. So that seems to be the best solution for avoiding errors. (I guess)

Wink

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.