DLR Posted April 7, 2010 Share Posted April 7, 2010 I'm trying to code using includes for efficiency - but also need to be mindful of the "headers already sent" problem. So I'm using the model suggested in the php manual - function "get_include_contents()". It works fine for some calls to the function - but not others. Here is the function function get_include_contents($pathname,$filename) { $allowed = array("CEL150.php","celcius_conn.php",$_SESSION['header'],$_SESSION['conn-read'] ); if(! in_array($filename,$allowed)) { $_SESSION['error_msg'] = "Failed access to file Error 201-0"; $_SESSION['error_code'] = 1; header("Location: ../apps/error.php"); } else { if (is_file($pathname.$filename)) { ob_start(); include $pathname.$filename; $contents = ob_get_contents(); ob_end_clean(); chdir(dirname($_SERVER['SCRIPT_FILENAME'])); return $contents; } else { $_SESSION['error_msg'] = "Failed access to file Error 201-1"; $_SESSION['error_code'] = 1; header("Location : ../apps/../apps/error.php"); exit(); } } } Now this call works fine get_include_contents("","CEL150.php"); and so does this get_include_contents("../connect/",$_SESSION['conn-read']); but this does not get_include_contents("../client_info/",$_SESSION['header']); But if I use include("../client_info/" . $_SESSION['header']); instead of the function it works fine (but obviously I get header already sent messages). The whole idea of using the function is to avoid the problems of "headers already sent". But why oh why does it not work? This call for help after countless ours of problem solving - actually no soloutions, just innumerable options. Any suggestions? Thanks David Quote Link to comment https://forums.phpfreaks.com/topic/197862-functions-works-for-some-calls-not-others-why-how-to-overcome/ Share on other sites More sharing options...
five Posted April 7, 2010 Share Posted April 7, 2010 so basically you only care about the output of included files code.. why not use file_get_contents() ? Quote Link to comment https://forums.phpfreaks.com/topic/197862-functions-works-for-some-calls-not-others-why-how-to-overcome/#findComment-1038373 Share on other sites More sharing options...
DLR Posted April 7, 2010 Author Share Posted April 7, 2010 Thanks for the suggestion - but it does not work either. The file - $_SESSION['header'] - just contains html code - see below - does the problem somehow lie with this file? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>SignForce job management area</title> <link rel="stylesheet" type="text/css" href="../client_info/signforce.css"> </head> <body> <table width="100%" border="0" class="header"> <tr> <td><img src="../client_info/SignForce-70.jpg" width="175" height="38" alt="SignForce logo"> <br> Custom made, world-class signs & signage, delivered & installed anywhere in Southern Africa</td> <td class="logout"> <a href="../index.php"><img src="../logout.jpg" width="91" height="33" alt="logout" border="0"></a> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/197862-functions-works-for-some-calls-not-others-why-how-to-overcome/#findComment-1038404 Share on other sites More sharing options...
five Posted April 7, 2010 Share Posted April 7, 2010 well assuming $_SESSION variables contain filenames right? not the actual header, which is supposed to be in a file.. <?php $allowed = array("CEL150.php","celcius_conn.php",$_SESSION['header'],$_SESSION['conn-read'] ); function get_include_contents($fullPath) { global $allowed; $filename = pathinfo($fullPath, PATHINFO_FILENAME); $pathname = pathinfo($fullPath, PATHINFO_DIRNAME); if (! in_array($filename, $allowed)) { $_SESSION['error_msg'] = "Failed access to file Error 201-0"; $_SESSION['error_code'] = 1; header("Location: ../apps/error.php"); } else { if (is_file($pathname.$filename)) { $cwd = pathinfo($_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'], PATHINFO_DIRNAME); $contents = file_get_contents('http://' . $cwd . $filename); } else { $_SESSION['error_msg'] = "Failed access to file Error 201-1"; $_SESSION['error_code'] = 1; header("Location : ../apps/../apps/error.php"); exit(); } } } something like that will do a file_get_contents pretty much simulating what a user would see in his browser if he used this url. helps? Quote Link to comment https://forums.phpfreaks.com/topic/197862-functions-works-for-some-calls-not-others-why-how-to-overcome/#findComment-1038442 Share on other sites More sharing options...
katierosy Posted April 8, 2010 Share Posted April 8, 2010 Please do not use any extra method. Just ensure that there is no extra space towards the bottom, after headers line. The extra space char is considered as header and results in warning massage. Quote Link to comment https://forums.phpfreaks.com/topic/197862-functions-works-for-some-calls-not-others-why-how-to-overcome/#findComment-1039004 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.