DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

PHP Internal Sub Request

11.17.2008
| 4561 views |
  • submit to reddit
        This code provides a way to make a kind of sub request without opening http connections to the server, just provide a valid php filename as input (you can use get variables too, ex: "blabla.php?id=12&a=2").

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com
class Common{
	public static function getPage($path){
		static $stack;
		$stack[] = '';
		$i = count($stack) - 1;

		if(strlen($s = ob_get_contents())){
			$i ? $stack[$i - 1] .= $s : @ob_flush();
			ob_end_clean();
		}
		$get = null; $pt = '';
		if(($qp = strpos($path, '?')) !== false){
			$get = $_GET; $pt = substr($path, 0, $qp);
			foreach(explode('&', substr($path, $qp + 1)) as $qv){
				$tv = explode('=', $qv);
				$_GET[$tv[0]] = count($tv) > 1 ? $tv[1] : '';
			}
		}
		else $pt = $path;
		ob_start();
		include $pt;
		$path = ob_get_contents();
		ob_end_clean();
		if(count($stack) > 1)
			ob_start();
		$get !== null && ($_GET = &$get);
		unset($get);
		return array_pop($stack) . $path;
	}
}