How to specify paths in PHP?

I am doing a small home computing project in PHP and creating such links ...

<a href=\"list_beer.php?order_by=name\">Beer</a>

It seems to be picking the right file. When I turn it on, it seems I need a way.

include_once ("/var/www/common.php");

I am sure this is not possible. Perhaps you could tell me what is the best practice for these things, so that I can create scripts that are not tied to a specific path (or operating system, this applies) and so that the file selected in the first example is known / can i control Perhaps some settings in php.ini or apache?

Thank.

+3
source share
4 answers

In fact, you need an absolute path for both.

for web resources, it should start with the web root - /

, , .
$_SERVER['DOCUMENT_ROOT'] .

<a href="/list_beer.php?order_by=name">Beer</a>

include_once ($_SERVER['DOCUMENT_ROOT']."/common.php");

+2

PHP:

 include_once './common.php';

script, .

 include_once 'common.php';

PHP include_path, common.php, common.php .

ROOT index.php, :

 const ROOT = __DIR__; // as of PHP 5.3
 define('ROOT', dirname(__FILE__)); // if you don't have PHP 5.3
+3

:

  • . , script ( script).
  • $_SERVER['DOCUMENT_ROOT'].
+2

php ini

include_path = var/www/includes

, , .

, include/require.

:

/var/www/html/example.com/very/long/way/down/index.php

;

'settings.php';

settings.php /var/www/includes/, .

ini apache, .htaccess

, , , , , ini_get ini_set.

include, /database, .

+2

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


All Articles