glassfish Posted October 29, 2014 Share Posted October 29, 2014 (edited) For the includes I use the following: include($_SERVER['DOCUMENT_ROOT'] . "/cms/includes/connection.php"); And for these following examples I use "http://localhost/": <img class="header_image" src="http://localhost/cms/assets/image/capture.png" /> <script type="text/javascript" src="http://localhost/cms/jquery/jquery.js"></script> <link rel="stylesheet" type="text/css" href="http://localhost/cms/assets/style.css" /> With these examples I use "http://localhost/" because "$_SERVER['DOCUMENT_ROOT']" is not working with these above. My Question: Is there a function I could be using and replace it with "http://localhost/"? I am looking to have it like following: When I upload the web app onto the web server it still should be working out. EDIT: "Un-linking" those links which happen did not work. Edited October 29, 2014 by glassfish Quote Link to comment https://forums.phpfreaks.com/topic/292131-function-for-using-absolute-path/ Share on other sites More sharing options...
cyberRobot Posted October 29, 2014 Share Posted October 29, 2014 Have you tried root-relative links? Try changing this <img class="header_image" src="http://localhost/cms/assets/image/capture.png" /> To this: <img class="header_image" src="/cms/assets/image/capture.png" /> Quote Link to comment https://forums.phpfreaks.com/topic/292131-function-for-using-absolute-path/#findComment-1495095 Share on other sites More sharing options...
glassfish Posted October 29, 2014 Author Share Posted October 29, 2014 (edited) cyberRobot, The problem I have had with that is that I may have to use "../../" to go "up" with the directories. And "for some reason" I have had issues with this, when for example including the stylesheet file in the header file and the header file gets included in a sub-directory. Edited October 29, 2014 by glassfish Quote Link to comment https://forums.phpfreaks.com/topic/292131-function-for-using-absolute-path/#findComment-1495096 Share on other sites More sharing options...
cyberRobot Posted October 29, 2014 Share Posted October 29, 2014 cyberRobot, The problem I have had with that is that I may have to use "../../" to go "up" with the directories. I'm not sure what you mean. Could you provide an example? Root-relative links always start in the website's root folder. So I'm not seeing why you would need to use syntax associated with document-relative links. Are you talking about the include() function from the original post? If so, the include() function should still use the $_SERVER['DOCUMENT_ROOT'] variable. To reference a file outside (or above) the root, you can use "..". For example: include($_SERVER['DOCUMENT_ROOT'] . "/../file-outside-root.php"); Quote Link to comment https://forums.phpfreaks.com/topic/292131-function-for-using-absolute-path/#findComment-1495099 Share on other sites More sharing options...
glassfish Posted October 29, 2014 Author Share Posted October 29, 2014 include($_SERVER['DOCUMENT_ROOT'] . "/cms/includes/header.php"); This gets inlucded in a sub directory for example than the stylesheets will not load anymore, when I have used "/" or also "../", that is why I have used "http://localhost/" to load the styelsheets, with that it works then. This was what I actually meant. Though, I have had just checked it and using the following indeed works for myself now: <link rel="stylesheet" type="text/css" href="/cms/assets/style.css" /> With the "forward slash" at the beginning. So, basically always starting with the "project name" in the "href" could be working then. Quote Link to comment https://forums.phpfreaks.com/topic/292131-function-for-using-absolute-path/#findComment-1495101 Share on other sites More sharing options...
cyberRobot Posted October 29, 2014 Share Posted October 29, 2014 With the "forward slash" at the beginning. The forward slash at the beginning is what makes it a root-relative link. Without the slash, it becomes a document-relative link. Just to clarify, do your images, style sheets, etc. work now...without "http://localhost"? Quote Link to comment https://forums.phpfreaks.com/topic/292131-function-for-using-absolute-path/#findComment-1495103 Share on other sites More sharing options...
QuickOldCar Posted October 29, 2014 Share Posted October 29, 2014 Unless you start adding items in a sub directory. You can also do something like this $server_host = "http://".$_SERVER['HTTP_HOST']; echo "<script type='text/javascript' src='".$server_host."/cms/jquery/jquery.js'></script>"; <script type="text/javascript" src="//<?php echo $_SERVER['HTTP_HOST'];?>/cms/jquery/jquery.js"></script> Quote Link to comment https://forums.phpfreaks.com/topic/292131-function-for-using-absolute-path/#findComment-1495104 Share on other sites More sharing options...
mac_gyver Posted October 29, 2014 Share Posted October 29, 2014 With these examples I use "http://localhost/" because "$_SERVER['DOCUMENT_ROOT']" is not working with these above. the reason $_SERVER['DOCUMENT_ROOT'] doesn't work for things the browser requests (images, and external css/javascrpt files) is because $_SERVER['DOCUMENT_ROOT'] contains file system path information on your server. it has no meaning to the browser. there are two different things going on - 1) include expects a file system path and a filename, because php is reading and including the file through the file system on your server. include can use a url, but this is not common, since it requires some settings to be enabled and including a .php file through a url doesn't include the actual php code, it includes the output from the .php file, the same as if you browsed to the file being included. 2) the <img > <script > and <link > tags you put on your web page contain URLs to the images, javascript, and css files. it's the browser that requests these and the browser needs to know the URL to use. Quote Link to comment https://forums.phpfreaks.com/topic/292131-function-for-using-absolute-path/#findComment-1495120 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.