phpdocument-rootregister-globals

Substitute for $DOCUMENT_ROOT without register_globals?


I have a website (hand-coded HTML/CSS/PHP) with many hundreds of pages, each with multiple references to $DOCUMENT_ROOT - for example:

<?php include("$DOCUMENT_ROOT/home/index.html"); ?>

Under PHP 5 that worked fine, but register_globals has been deprecated and removed from PHP 7, so it doesn't work anymore.

I don't want to hard-code the path to the file because my testing server and live server use different paths to the files - for example:

Testing server: /home4/testing/public_html/home/index.html

Live server: /home4/live/public_html/home/index.html

(Plus, just on general principles, it seems like a bad idea to hard-code paths needlessly.)

I gather that, syntactically, $_SERVER['DOCUMENT_ROOT'] or $_SERVER["DOCUMENT_ROOT"] can replace $DOCUMENT_ROOT.

I tried a simple search-and-replace on my source HTML:

<?php include("$_SERVER["DOCUMENT_ROOT"]/home/index.html"); ?>

and

<?php include("$_SERVER['DOCUMENT_ROOT']/home/index.html"); ?>

Both don't work - I get ERROR 500.

The first one pretty obviously has a problem with string quoting (too many " in the wrong places).

But the second one doesn't work either (also ERROR 500).

How can I get my site working on PHP 7?

Is there something I can put in .htaccess or php.ini to define $DOCUMENT_ROOT?

Or is there a different syntax that'll work for $_SERVER["DOCUMENT_ROOT"]?

Or is there a better solution?

My main goal is to get it working again ASAP (it's a company website, and it's down because Hostgator has pulled PHP 5 support for security reasons), without creating worse problems.


Solution

  • I found a solution. It's ugly, but it seems to work.

    I did a brute-force replacement of:

    "$DOCUMENT_ROOT
    

    with:

    $_SERVER['DOCUMENT_ROOT']."
    

    ...across the entire website.

    It's ugly, but it's working. Suggestions for improvement are welcome.