Best way to access / create SITE_ROOT and SERVER_ROOT values ​​in PHP?

I need 2 different paths, one for include and one for js / css, etc. I use mod_rewrite. The following works great ....

Currently, all my files contain this at the top

define('SERVER_ROOT',   'C:/wamp/www/site_folder/');
define('SITE_ROOT',     'http://localhost/site_folder/');

and then the files are called like this:

require_once SERVER_ROOT . 'st_wd_assets/inc/func_st_wd.php';    

<link rel="stylesheet" type="text/css" href="<?php echo SITE_ROOT;?>st_pages/user_area/css/user_area.css" media="screen"/>

as you are likely to see, it will be a huge task to update the top of each file every time I move versions between the local host and my server.

What is the best / standard way to determine these ROOT values?

I do not see a solution in a super-global $_SERVER? Do people usually use VirtualHosts? But then you still would not have to define the ROOT constants?

+3
source share
6 answers

. VirtualHosts.

.

  • .
  • DOCUMENT_ROOT $_SERVER .
  • , -. php_value auto_prepend_file .htaccess
  • , , , .
  • , mod_rewrite - -, , - .
+2

config.php

define('SERVER_ROOT',   'C:/wamp/www/site_folder/');
define('SITE_ROOT',     'http://localhost/site_folder/');

.

require_once ('config.php');

, - . - .

+1

/ SERVER_ROOT. - SITE_ROOT , , .

0

VirtualHosts, $_SERVER ['DOCUMENT_ROOT'] . , "C:/wamp/www"

, SERVER_ROOT , ( ):

define('SERVER_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);

SERVER_ROOT "C:/wamp/www/site_folder/" .

, , , .php "C:/wamp/www/site_folder/". .php, .

0

php_uname('n'), , php. :

$configFile = 'config/' . php_uname('n') . '.config.php';

if (file_exists($configFile)) {
    include($configFile);
}

config/your_computer_name.config.php config/live_servers_name.config.php.

0

docroot. settings.inc.

//settings.inc:

define('SITE_ROOT',     'site_folder');
require_once (dirname(__FILE__)) . '/st_wd_assets/inc/func_st_wd.php';

require_once (dirname(__FILE__)) . "/settings.inc";

, css . "/".

0

Source: https://habr.com/ru/post/1748298/


All Articles