0

Recently, I have installed a new Ubuntu server which incorporates PHP7. Previously, on PHP5.3 (or it may have been a later version of PHP5, I could use $_SERVER['HOME'] which returned the home path of the server (i.e. /var/www/). However, this is not possible in PHP7 as $_SERVER['HOME'] is no more.

If I have a site which has a document root of /var/www//httpdocs, how do I get the /var/www/ part. I can not hard code it as my test server (a Synology NAS) does not use PHP7 and can not be upgraded to PHP7 and thus, I need to use a variable that is constant for PHP5 and PHP7 which return the same result to forumate the same result as $_SERVER['HOME'] did in PHP5.

I tried using $_ENV['HOME'] but this appears to print nothing from my system when a simple die($_ENV['HOME']); is executed.

Can someone put me out of my misery please?

Thanks

Muhammad Muazzam
  • 2,810
  • 6
  • 33
  • 62
adelphiaUK
  • 49
  • 1
  • 7
  • Just a note: `$_SERVER["HOME"]` was never a standard PHP provided header. It's probably because the webserver is also a different version now. Just do a `print_r($_SERVER)` you'll find what you need. – apokryfos Jul 08 '17 at 09:26
  • Thanks @apokryfos. I did try that but the only one I could see was the document root but I need it to be the PARENT of the document root. Maybe I need to look at "string dirname ( string $path [, int $levels = 1 ] );". Would you concur on that? – adelphiaUK Jul 08 '17 at 09:33
  • Well, first you can try`getenv("HOME")` and in the worse case `exec("echo ~")` to try to get it a bit more reliable. If not then what you're suggesting would work assuming you maintain a similar setup in all environments. – apokryfos Jul 08 '17 at 09:36

2 Answers2

1

OK, thanks to @apokryfos, I have sorted the issue. I perhaps should have mention I wanted to include a file outside of the document root (oops, my bad).

The getenv('HOME') for some unknown reason returns an empty string on my server, but the method I used to overcome this was to use the dirname option.

The final result was:

set_include_path(get_include_path().PATH_SEPARATOR.dirname($_SERVER['DOCUMENT_ROOT'],1)."/includes");

Thanks again for your assistance.

adelphiaUK
  • 49
  • 1
  • 7
0

In case this is useful. I've recently upgraded from PHP-7.0 to PHP-7.1 and ran into the same issue. The $_SERVER["HOME"] was no longer set. While I wasn't really using it for anything I did find it a bit curious so this is what I did to fix it: Install and enable PHP-7.1-FPM and disable the PHP-7.1 apache2 mod.

a2dismod php7.1 
a2enmod proxy_fcgi setenvif
a2enconf php7.1-fpm
service apache2 restart

The idea should be similar for version 7.0

Though I didn't need to do the next step, the question Environment variables and PHP seems to suggest setting the environment variable manually may be required as well.

Hope it helps.

apokryfos
  • 38,771
  • 9
  • 70
  • 114