blsn Posted May 4, 2013 Share Posted May 4, 2013 Hi, I am building a web site and have included files that get used from the root and different sub directories within my site. For example: <link rel="stylesheet" href="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>/css/style.css"> These are the outputs of $_SERVER['DOCUMENT_ROOT']: Testing server (http://localhost/mywebsite): C:/xampp/htdocs/mywebsite Share host server (http://www.mywebsite.com): /home/mywebsite/public_html How to define the above path correctly to work both on testing and hosting server? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 4, 2013 Share Posted May 4, 2013 $_SERVER['DOCUMENT_ROOT'] is the file system (disk) path to your document root folder. it is NOT used in html links/href statements on a web page. you must use a URL in links/href statements. you can use a domain relative URL - href="/css/style.css" (the leading slash refers to the root of the domain of the current page.) or you can use an absolute URL - href="http://<?php echo $_SERVER['HTTP_HOST']; ?>/css/style.css" Quote Link to comment Share on other sites More sharing options...
blsn Posted May 4, 2013 Author Share Posted May 4, 2013 Thank you for your reply but this solution is good only for hosting server. How can I use one code for both hosting and testing server? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 4, 2013 Share Posted May 4, 2013 did you BOTHER to try it on your localhost server? Quote Link to comment Share on other sites More sharing options...
blsn Posted May 4, 2013 Author Share Posted May 4, 2013 I've tried it on the local host server. The outputs path is http://localhost/ Should be: http://localhost/mywebsite p.s. The output for the share hosting is o.k. (http://www.mywebsite.com). Any advise on this matter would be appreciated. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 4, 2013 Share Posted May 4, 2013 you should actually be using a virtual host setup on your localhost system so that there isn't any difference in the path (you can find how to do this common task by using google.) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.