Http request failed when including file through url from subdomain

假如想象 提交于 2019-12-24 23:59:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!