问题
I'm trying to make reliable code that will work both on my local server and on my actual server for website so I won't have to change paths etc. to my files. I cannot use relative paths since I'm using a subdomain so browser doesn't see folders above that path so what I'm trying to do is on my local server use root folder to guide my code where the files are and on webserver I retrive servername. The error that I get right now is
Warning: include(http://cms.domain.com/serverdetails.php): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /var/www/html/domain/administration/cms/DBconnection_patches.php on line 11
I have main domain and a subdomain cms.domain with cms files are structured like so
domain
-index.php
-js
-files.js
-administration (subdomain folder)
-index.php
-otherfiles.php
It is a normal subdomain made through Vhost, on linux ubuntu 14.04 apache2.
if($_SERVER['SERVER_NAME']=='domain.com'){
$local = false;
}elseif($_SERVER['SERVER_NAME']=='localhost'){
$local = true;
}
if($local){
include $_SERVER['DOCUMENT_ROOT'] . '/domain/administration/serverdetails.php';
}else{
include 'http://cms.'.$_SERVER['SERVER_NAME'] . '/serverdetails.php';
}
回答1:
Dude you cannot include a .php
file using absolute URLS!!!
Since while generating the URL the server(Apache/nginx/whatever) is supposed to not output the content of the file, (if properly configured). Instead it it supposed to execute the file and return the output of the executed code.
You shall need to use the internal file path
to include the file.
And for that dirname(__FILE__)
shall be useful in calculating the relative path
.
来源:https://stackoverflow.com/questions/33174435/http-request-failed-when-including-file-through-url-from-subdomain