note 24401 deleted from function.ob-get-contents by didou
| From: | didou@php.net | Date: | Tue, 01 Jul 2003 13:30:54 +0000 |
| Subject: | note 24401 deleted from function.ob-get-contents by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-51274@lists.php.net to get a copy of this message | ||
Note Submitter: drtdiggersNOSPAM@MAPSONhotmail.com
----
Finally found a spiffy use for ob_x functions! Alright, I had to write a query script that returned
data in string form so that a Flash prog could easily query mySQL. Nothing special here. But, I
was thinking, it would be nice to abstract queries like this in other PHP code as well, so that you
could have some other existing script do the query, or whatever else you wanted it to do. So I came
up with this:
// These functions allow PHP scripts to abstract functionality onto other scripts, not necissarily
other PHP
// scripts thought...
// For example, abstracting database queries for simple information now adds an easily
// modifiable and maintainable security checkpoint. query.php can deal with
// checking for clearance and modules written that do queries can now call query.php
// to get the data, instead of having to manually execute a query statement
// in fact, will add functionality so that you can even flag it to just return the $result set if
// its a php script, or just let it return the query string and reassemble() it into whatever array
function executeExternal($script) {
list($script, $parameters) = explode('?', $script);
// Set up the parameters to the _GET string
$datasets = explode('&', $parameters);
foreach($datasets as $dataset) {
list($key, $value) = explode('=', $dataset);
$key = urldecode($key);
$value = urldecode($value);
$_GET[$key] = $value;
}
ob_start();
// Don't call it _directly_ from here, want all variables in this scope to be protected
$result = includethescript($script);
$value = ob_get_contents();
ob_end_clean();
// Depending on what was asked for, give it to them
if(isset($_GET['nostringreply']))
return $result;
else
return $value;
}
// Script CANNOT CANNOT CANNOT define any new functions and it will run in a protected scope
// basically doing exactly whats its saying its doing, executing the code
// in a clean scope, so don't mind if we trample these variables
function includethescript($__script) {
if(!substr_count($__script, '.php') && isset($_GET['nostringreply']))
die("can only request return of resource from a php script");
// $__resultset must be defined in the $__script if its been requested with nostringreply...
include($__script);
if(!isset($__resultset) && isset($_GET['nostringreply'])) {
// implicit flush
die("invalid php script for use with nostringreply option, did not return a resultset");
}
else
return $__resultset;
}
Note the restrictions placed on the foregin script if its php code: 1) the ability to straight
request a Result Resource must be implemented in the script being called (play with
$_GET['nostringreply'] to see if you should do this and set $__resultset if you should)
2) Cannot define functions within it. This is because in order to guarentee multiple calls,
you're basically gluing your other PHP script into whatever is calling this function, and an
include() is required, include_once() will only work for the first time. This provides a nice
common abstraction layer for both a script-based(php or whatever, Perl if you want to be l33t ;) )
interface through PHP to a database and a Flash based interface through PHP to a database.
Modifications to use $_POST would be trivial. Also note that nested output buffering works (I tried
it) with this scheme, so you can maintain whatever output buffering deal you currently have going
on.