williamr Posted October 1, 2008 Share Posted October 1, 2008 I apologize if this question comes off a bit amateur but is it possible to do a PHP include as it would display and not as it would function without using the absolute location of the PHP file? It would obviously ease the load on my server to use relative location for all of my includes but this is one obstacle I'm not sure I can overcome. Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/126661-help-with-relative-and-absolute-includes/ Share on other sites More sharing options...
revraz Posted October 1, 2008 Share Posted October 1, 2008 I really don't understand your question, but there is no reason you can't use a relative path. Quote Link to comment https://forums.phpfreaks.com/topic/126661-help-with-relative-and-absolute-includes/#findComment-655020 Share on other sites More sharing options...
williamr Posted October 1, 2008 Author Share Posted October 1, 2008 In my short time working with PHP, what I've noticed is that if you place an include with an absolute link, the code will appear as it would display. For example, if I have a script that pulls the titles of songs off of my server and displays them at random, an absolute include would simply display the song title because it would only use what would be displayed to the user. But if I did it relatively, it would add the entire script to the page because it's pulling the file locally. But I don't want to use absolute links. Our server seems to struggle resolving the address. Quote Link to comment https://forums.phpfreaks.com/topic/126661-help-with-relative-and-absolute-includes/#findComment-655030 Share on other sites More sharing options...
dropfaith Posted October 1, 2008 Share Posted October 1, 2008 ive never had that problem with a relative path Quote Link to comment https://forums.phpfreaks.com/topic/126661-help-with-relative-and-absolute-includes/#findComment-655036 Share on other sites More sharing options...
PFMaBiSmAd Posted October 1, 2008 Share Posted October 1, 2008 Using a URL is not the same as an absolute or relative file system path. It causes a separate http request from php to your web server to request the page in the URL. Quote Link to comment https://forums.phpfreaks.com/topic/126661-help-with-relative-and-absolute-includes/#findComment-655038 Share on other sites More sharing options...
williamr Posted October 1, 2008 Author Share Posted October 1, 2008 That explanation helps somewhat. So would it then be possible to have an include make an HTTP request using relative links in the php include? Quote Link to comment https://forums.phpfreaks.com/topic/126661-help-with-relative-and-absolute-includes/#findComment-655043 Share on other sites More sharing options...
PFMaBiSmAd Posted October 1, 2008 Share Posted October 1, 2008 No. A relative URL only has meaning in a browser because a browser will take the URL of the current page and append a relative URL to it, forming the absolute URL that is needed in a http request. The syntax for a relative URL in an include statement would be treated as a file system path. When an include uses a URL, you only receive the content that is output by the included page. This does not include the php code and it does not share variables, in addition to taking the extra time needed for the extra http request to be serviced by the web server. Including file through the file system, either using absolute file system paths or relative file system paths, is much faster and is how the include statement is intended to be used. Quote Link to comment https://forums.phpfreaks.com/topic/126661-help-with-relative-and-absolute-includes/#findComment-655054 Share on other sites More sharing options...
williamr Posted October 1, 2008 Author Share Posted October 1, 2008 So for what I'm trying to do, which is to display what is being outputted by the PHP file, sticking with URLs is my best bet? Quote Link to comment https://forums.phpfreaks.com/topic/126661-help-with-relative-and-absolute-includes/#findComment-655057 Share on other sites More sharing options...
PFMaBiSmAd Posted October 1, 2008 Share Posted October 1, 2008 File system access takes at most a few 10's of milliseconds for each include statement. Using a URL will take a minimum in the range of 200-500 milliseconds for each include statement, for the http request to be serviced and the results to be returned. You should only use a URL because you must, not because it is easiest, because it is not the fastest and the performance of your web site will suffer as a result. Quote Link to comment https://forums.phpfreaks.com/topic/126661-help-with-relative-and-absolute-includes/#findComment-655075 Share on other sites More sharing options...
williamr Posted October 1, 2008 Author Share Posted October 1, 2008 Right. If it were up to me, I wouldn't use URL's at all because it is very clear there is a strain on the server, but there doesn't seem to be an alternative to including what is only being output by the PHP file. If there is an alternative, I guess that's what I'm looking for in this thread. Quote Link to comment https://forums.phpfreaks.com/topic/126661-help-with-relative-and-absolute-includes/#findComment-655090 Share on other sites More sharing options...
PFMaBiSmAd Posted October 1, 2008 Share Posted October 1, 2008 When you include through the file system and the content/php in the included file is parsed/executed along with the main file's content/php, you get the same combined results. This is how everyone includes headers, menus, forms, general content, footers... Why is this somehow not what you want? Also, unless you have unlimited monthly bandwidth, including using a URL consumes your bandwidth two times. Once when the include gets the content and again when that content is sent to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/126661-help-with-relative-and-absolute-includes/#findComment-655126 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.