PHP shared includes on MAMP development environment

Hi

I’m assigning $_SERVER[‘DOCUMENT_ROOT’] to a variable so that I can use includes from all over a website’s directory structure regardless of where I place the script. Problem is, I’m using MAMP on a Mac and instead of

$_SERVER[‘DOCUMENT_ROOT’]

giving me

http://localhost:8888/

it is giving me

/Applications/MAMP/htdocs

When the variable is assigned the above value, it doesn’t ‘invoke’ (for my lack of a better technical term) the localhost and the php doesn’t work.

Also, I intend this line of php code (which assigns the absolute path to the variable) to be in my head.php file, which is included at the top of all the pages. I do this so that I don’t have to add this line into every file. BUT if I’m in a different directory to head.php, how will it know how to find the include?

Should I use a global variable? Or a constant? But then do I have to use sessions? Please help. So appreciated.

Nick

Should be in a file that is located in the root, then included from there.


<?php

$rootUri = str_replace( $_SERVER['DOCUMENT_ROOT'], '/', dirname( __FILE__ ) );
  $rootUri = preg_replace( '~[/\\\\\\\\]+~', '/', "/$rootUri/" ); # Clean up

echo '<a href="', $rootUri, 'page.html">page</a>';

Hi everyone thanks for the great replies. After researching and trying more, I realized that php5 protects against ‘cross site attacks’ by not allowing includes of files with absolute URLS in them. Fair enough. I was considering turning that off on my localhost just to continue with development, but then i realized it might not work when I make it live on my public host, so I don’t wanna risk that.

This is my problem: menu.php contains the HTML that forms my navigation menu that appears throughout the site at the top of every page. So to avoid putting it in every file, I use include “menu.php”; on all the pages. Pretty elementary technique, so that I don’t have to make changes to every page (there are possibly hundreds) when I need to change it.

Problem is, if I’m viewing an HTML file in a sub-directory i.e. rootdirectory/associates/associate1.html then the relative URLS in the links in my menu.php include file don’t work i.e. <a href=“index.php” > because I’m in a sub-directory.

So I thought to use absolute URL’s, but then I’m going to have to change them all once I move it from my localhost to my live host. So I decided to assign a variable at the top of that menu.php include file and then put that variable in the links i.e.

<?php $rootdir = ‘http://localhost:8888/rootdirectory/’; ?>

and then

<a href=“<?php echo $rootdir; ?>index.php”>Home</a>

and so on for every link.

But it says that’s not allowed because you can’t include a file with absolute URLs because that’s a security risk.

So I tried DOCUMENT_ROOT:

I tested it by <?php echo $rootdir; ?> and it works; it echoes the document root. But the document root i.e. applications/MAMP/rootdirectory/ isn’t going to work because I need it to say http://localhost:8888/rootdirectory/ instead.

I tried SERVER_NAME but using SERVER_NAME in an include doesn’t work either because it returns localhost/ instead of localhost:8888/ which is basically useless.

So I don’t know what to do. Does this make it clear? Thanks everyone.

If he wants to redirect to a certain page, he’ll want to use HTTP_HOST or SERVER_NAME
If he wants to include from his MAMP folder structure, he’ll want to use DOCUMENT_ROOT

I think that what we need now is, some example code that shows us how he’s intending to use them.

You are going about it the wrong way…you do not include using HTTP URLs!

Run php_info()

You will see a report of server information, including globals in your apache environment. You should see that ones such as HTTP_HOST and SERVER_NAME have no slash following them.

DOCUMENT_ROOT gives you the local root path.

SERVER_NAME should be the remote root path that you’re after.

Thanks so much for the reply. I definitely learned something there. It doesn’t solve the problem though, because in the URL bar I have to enter

http://localhost:8888/

and not

localhost/ (because SERVER_NAME returns localhost/ )

so assigning SERVER_NAME won’t work. I was thinking about just concatenating http:// and :8888/ to either end of it but there’s that pesky / at the end of localhost/

how do I solve this?

Thanks for your help.

Nick