PHP: // get html body of web page function importTemplate( $filepath ) { // get source documents $src_temp = file_get_contents( "script/server/template.php" ); $src_body = file_get_contents( $filepath ); // get html body $begin_pos = strpos( $src_temp , "<body>" ) + 6; $end_pos = strpos( $src_temp , "</body>" ); $html = substr( $src_temp , $begin_pos , ($end_pos-$begin_pos) ); // return stream to browser header('Content-Type: text/html; charset=ISO-8859-4'); echo $html; } What I'm trying to do, is have a site-wide template document and a body definition document. In the bodydef file, you create the webpage in the body area as usual and include a php script. The idea is that when the user visits the bodydef file, it replaces the body of the template file with the body of the bodydef file and sends that back to the browser as a html page. The purpose of all this being to be able to have a site-wide template; the basic outlay of the website - the navigation, the banner logo etc. is the same everywhere (prevents inconsistency). my problem here is, I can create a html document as you can see from the php code, however I dont know how to send back the $html variable as a html document. Basically, I want to create a custom html document and send it back to the browser as a webpage; I dont want the page which the user visits to be visible to them,only the custom authored html that is sent back to the browser. sorry, if its a bit confusing.
Yeah, but I think it has to be "return $html;" rather than "echo $html;" in the function if he is going to use that.
no, i mean't outside of the function. Its bad practise to output within a function other than 'return'ing it
well, the problem to begin with is that the code is incomplete. As it is, it only grabs the body of the bodydef file and does not place that in the template file. to do that, the code must be: PHP: // get source documents $src_temp = file_get_contents( "script/server/template.php" ); $src_body = file_get_contents( __FILE__ ); // get html body $begin_pos = strpos( $src_temp , "<body>" ) + 6; $end_pos = strpos( $src_temp , "</body>" ); $temp_html = substr( $src_temp , $begin_pos , ($end_pos-$begin_pos); $html = str_replace( "<!--page_content-->" , $temp_html , $src_temp ); // return stream to browser echo $html; the problem is that when the script is run, it returns both the output of the function AND the body of the page in the bodydef file. EDIT: maybe I didnt make this clear enough: I want to dynamically create a html document and return that to the browser and nothing else; if the script which returns that document to the browser contains html, that should be completely ignored; the dynamically created file should be sent only
if you only want the echo $html to be displayed, then why not exit; after the function has been called? PHP: // get html body of web page function importTemplate( $filepath ) { // get source documents $src_temp = file_get_contents( "script/server/template.php" ); $src_body = file_get_contents( $filepath ); // get html body $begin_pos = strpos( $src_temp , "<body>" ) + 6; $end_pos = strpos( $src_temp , "</body>" ); $html = substr( $src_temp , $begin_pos , ($end_pos-$begin_pos) ); $html = str_replace( "<!--page_content-->" , $html , $src_temp ); // return stream to browser return $html; } //to run $complete = importTemplate('filename') if (!empty($complete)) { //if the returned value is NOT empty echo $complete; exit; //terminates this script } else { //else, runs this script instead of displaying fetched html ... if you want to execute the code you load ($src_body) then try using eval.
well, that works well apart from one problem: the body content is taken from the wrong part of the page. At the moment, it shows a section of the php script instead of the body of the page.
so $src_body = eval($src_body); but if the $src_body file is 'this' file then it will make an infinite loop?
It appears that the code works near-perfectly when placed at the end of a bodydef file. The html body is shown correctly, however it is shown twice. I wonder why this is?