Forum Moderators: coopster

Message Too Old, No Replies

Buffer a PHP file with variables in the URL

PHP ob_start buffer file with variables

         

mazeteam

2:29 am on Feb 3, 2010 (gmt 0)

10+ Year Member



Hello all,

since my previous query was answered rather quickly, maybe you guys can find a solution to this one.

I am using the ob_start function (and obviously the end_flush) to buffer a php file into a string within a master file.

CODE:-
=====
ob_start();
include($file);
$string = ob_get_contents();
ob_end_clean();
=====

To start this, the master file is accessed as (for example) "MasterFile.php?file=index.php" and so the file index.php is passed through this script. This is necessary as I have been rebuilding some pages with custom code that this master file then changes into generic blocks of code, which makes it easier when I update the site.

This all works fine if the file is basic without variables in the URL... but if (for example) the file index.php has a php switch function inside it, running it through this script always returns the "else" outcome as though the variable wasn't set.

For example, going to "MasterFile.php?file=index.php?page=gallery" will return the same result as it would without the "?page=gallery" added on. And yes I know there are two ?'s in the URL, but I'm not sure how to change that as the URL to access the master file is set through a .htaccess redirect (so if you went to "index.php?page=gallery", it redirects to "MasterFile.php?file=index.php?page=gallery".

So, is there a way of buffering any php file that has variables in the URL through this script so that the variables will play out correctly and not revert to default?
(the pages being sent through this script can have up to 10 or more different variables at once, especially if the page being sent through is the result from an email contact form - so I don't want to have to alter the master file with code similar to "if 'x' variable is set, then put this variable in the buffered file" if you see what I mean!)

thanks in advance

jamie

2:44 pm on Feb 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



it looks like you have two ? marks:

MasterFile.php?file=index.php?page=gallery

that char before page should be an &

mazeteam

3:02 am on Feb 6, 2010 (gmt 0)

10+ Year Member



I did actually say that I'm aware of there being two "?"'s in the URL... I have run a test and the file operates normally if the second "?" is replaced with an "&".

example:
<snip>
(normal version with two "?"'s - as set through .htaccess)

<snip>
(altered version, where second "?" is replaced with an "&")


SO, my question is this, Can the ? be changed to a & within the PHP script OR the .htaccess file?

I have tried the following code, but it doesn't work:
=====
if(!$file == "" && (strstr($file,"?")))
{
$OpenFile = str_replace( "?", "&", $file);
}
else { $OpenFile = $file;}

echo $OpenFile; // This is here to show what value "file" is set as. //

// Buffer Script //
ob_start();
include($OpenFile);
$string = ob_get_contents();
ob_end_clean();
// end buffer //

echo $string;
=====

Alternatively, is there a way to get ANY variable that could be set in the URL that is NOT "file" and add those variables onto the end of the $file value within the buffer include script?

[edited by: eelixduppy at 5:11 pm (utc) on Feb 6, 2010]
[edit reason] removed URLs [/edit]

rocknbil

3:46 am on Feb 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you try encoding the second ?

MasterFile.php?file=index.php%3Fpage=gallery

mazeteam

4:14 am on Feb 6, 2010 (gmt 0)

10+ Year Member



I'm just trying something else now:

=====

function selfURL() {
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2) {
return substr($s1, 0, strpos($s1, $s2));
}

function GetTrueVars($text, $xhtml = true)
{
$tags = array(

'#(.*?)PageParser.php?file=(.*?)&(.*?)#si' => '?\\3',
'#(.*?).php?(.*?)#si' => '\\2',
);

foreach ($tags AS $search => $replace)
{
$text = preg_replace($search, $replace, $text);
}
return $text;
}

function ShootNumbers($text, $xhtml = true)
{
$tags = array(

'#&12(.*?)#si' => '&somefunnyrandomnumbersforajoke=',
);

foreach ($tags AS $search => $replace)
{
$text = preg_replace($search, $replace, $text);
}
return $text;
}

function FinalBitOfURL($text, $xhtml = true)
{
$tags = array(

'#http://www.(.*)/(.*)/(.*).php(.*)#si' => '\\2/',
);

foreach ($tags AS $search => $replace)
{
$text = preg_replace($search, $replace, $text);
}
return $text;
}

function LastDitchAttempt($text, $xhtml = true)
{
$tags = array(

'#http://(.*)//(.*)#si' => 'http://\\1/\\2',
);

foreach ($tags AS $search => $replace)
{
$text = preg_replace($search, $replace, $text);
}
return $text;
}




$Location = selfURL();
$AllVars = GetTrueVars($Location);
//echo $AllVars;
//echo "<br>";
$Combo = $file . $AllVars;
//echo $Combo;
//echo "<br>";
$NewCombo = ShootNumbers($Combo);
//echo $NewCombo;
//echo "<br>";
$GetFolders = FinalBitOfURL($Location);
//echo $GetFolders;
//echo "<br>";
$WebAddress = "http://".$_SERVER['SERVER_NAME']."/".$GetFolders."/".$NewCombo;
$StripTheCrap = LastDitchAttempt($WebAddress);
//echo $WebAddress;
//echo "<br>";
echo $StripTheCrap;
echo "<hr>";

// if(!$file == "" && (strstr($file,"?")))
// {

// $reg_ex = '?';
// $replace_word = "&";

// $OpenFile = ereg_replace($reg_ex, $replace_word, $file);

// }
// else { $OpenFile = $file;}

// echo $OpenFile;



ob_start();
include($StripTheCrap);
$string = ob_get_contents();
ob_end_clean();

echo $string;
echo '<iframe src="'.$StripTheCrap.'" scrolling="auto" style="height:80%; width:80%;">
Content
</iframe>';
===
What this is trying to do is get the full url of the page, then strip back the domian name and page, and the FILE variable if set, so just the other variables remain... and then build the URL of the file to include in the Buffer area. This is presently now in place for the example links in the above post - BUT for some reason the include now fails!

This bit of script is a bit messy, but the output page SHOULD first show the "real" URL of the page being shown, then display the contents of that page, followed by an iframe again containing that content. However, the only bit that works is displaying the real url.
- example <snip>
will display <snip> as the URL on the first line of the page... changing the & to a ? also changes it within the page. But the INCLUDE function should be working as the file is there.

This is really bugging me now.

[edited by: dreamcatcher at 6:46 am (utc) on Feb 6, 2010]
[edit reason] no urls please, see T.O.S [/edit]

mazeteam

4:23 am on Feb 7, 2010 (gmt 0)

10+ Year Member



I've got this a little bit further now.

The below example is when I have a preset file
====
ob_start();
$_GET = array();
$_GET['page'] = "SiteNews";
include("NewMTsite-MThomeCorePage.php");
$string = ob_get_contents();
ob_end_clean();
====
That works and will show the correct page

BUT when I have it so that the "file" variable and the "page" variable can be altered, it breaks and reverts to default again!

====
ob_start();
$_GET = array();
$_GET['page'] = $page;
include($file);
$string = ob_get_contents();
ob_end_clean();
=====

On paper, this is exactly the same as the top script and so SHOULD work, but doesn't.
the URL being PageParser.php?file=NewMTsite-MThomeCorePage.php?page=SiteNews - AND I have made script that turns the second ? into an & and seperates the variable string so that just ?page=SiteNews is a string in it's own right ($FullVars). if I run an include like this
===
include($file.$FullVars);
===
Then I get an error message:
Warning: include({I've removed this bit}/test-area/NewMTsite-MThomeCorePage.php?page=SiteNews) [function.include]: failed to open stream: No such file or directory in {I've removed this bit}/test-area/PageParser.php on line 925

the $FullVars string can have multiple variables in it, so could be "?page=SiteNews&variableone=something&variabletwo=ted", and Ideally I'd like some script so that all of that can automatically go into a variable like:
$_GET = array();
$_GET['page'] = 'SiteNews';
$_GET['variableone'] = 'something';
$_GET['variabletwo'] = 'ted';
- then followed by the include command.