Why is the first, second line not working, and the third working?

include_once($_SERVER['HTTP_HOST']."/connection.php"); include_once($_SERVER['SERVER_NAME']."/connection.php"); include_once("../connection.php"); 

And even so they do not work:

 include_once("$_SERVER['SERVER_NAME']/connection.php"); 

The script works from the "Soft" directory (1st level), a connection.php lies in the root of the site (0th level).
What is the ficus-picus?

  • And why did you decide that they should work at all? Do you really have a connection.php file really found in the $_SERVER['HTTP_HOST'] or $_SERVER['SERVER_NAME'] relative to the current path? - user6550
  • @klopp But why then the command print "http://".$_SERVER['SERVER_NAME']; shows http://www.site_name.ru ? - I_CaR
  • one
    Obviously, because the $_SERVER['SERVER_NAME'] variable contains the value of www.site_name.ru . Consequently, the constructs expand to include_once("www.site_name.ru/connection.php") . And in the file system there is simply no such path relative to the directory from which the invocations are called. - user6550

2 answers 2

“Ficus” here is that “global variables” should not be chosen randomly, as you do, but use only those that are suitable for a particular task.

Theoretically, HTTP_HOST and SERVER_NAME can be used for INCLUD, but I very much doubt that a folder with the name of the server was created in the root of the website on the disk. Most likely, you just confuse, like all beginners, a file on a disk and an HTTP resource on the server. Here is a good article that explains the difference .

Most likely you need DOCUMENT_ROOT , but first you have to decide which way you need it , and then try to build it with variables.

If we analyze these examples in detail, then let's assume that our site is located in /home/www/example.com/htdocs . Then the first two examples will lead to

 /home/www/example.com/htdocs/Soft/example.com/connection.php 

while should lead in

 /home/www/example.com/htdocs/connection.php 

at the same time, in DOCUMENT_ROOT, on the correctly configured server, the path you need should be located /home/www/example.com/htdocs - and that’s what you need to substitute in the inclusive.

And the third example is incorrect from the syntactic point of view. If you write inside double quotes, you can do this:

 include_once("$_SERVER[DOCUMENT_ROOT]/connection.php"); include_once("{$_SERVER['DOCUMENT_ROOT']}/connection.php"); 

but I would prefer the option through the dot.

  • But after all, print produces in the first two versions this: (b ** be here Enter is programmed ugly !!!) this is: $http_adr = "http://".$_SERVER['SERVER_NAME']; print $http_adr; // http://site_name.ru $http_adr = "http://".$_SERVER['SERVER_NAME']; print $http_adr; // http://site_name.ru $http_adr = "http://".$_SERVER['SERVER_NAME']; print $http_adr; // http://site_name.ru - I_CaR

Because include in php points to the address of the directory, not the url of the page as in javascript. If $ _SERVER ['SERVER_NAME'] contains www.example.com, we get include_once('www.example.com/connection.php') , which is meaningless from the point of view of php, you probably need $ _SERVER ['DOCUMENT_ROOT']