Dynamic includes

A script you can use to get urls like index.php?phpfan instead of just phpfan.php. It protects against suspicious characters from being included so people won’t be able to do naughty things.

<?php
 
	$vincludepath	= '/home/username/public_html/phpfan/';
	// instead of main put the name (but not the .php) of the php file you want to specify to be the default
	$default		= 'main';
 
	$errori			= 'Use of restricted characters in variables.';
	$errorexist		= 'That page does not exist.';
 
	$getqs			= $_SERVER['REQUEST_URI'];
 
	if (preg_match("/&/", $getqs, $matchqs))
	{
 
		$file = basename($getqs);
 
		if (preg_match("/\?([-_=a-zA-Z0-9]+)&/",  $file, $matchqs))
		{
 
			$site = $matchqs[1];
 
			if(!$site)
			{
 
				include $vincludepath . $default .'.php';
 
			} else {
 
				if (!preg_match("/=/", $site, $matchqs))
				{
 
					if(@file_exists($vincludepath . $site .'.php'))
					{
 
						include($vincludepath . $site.'.php');
 
					} else {
 
						echo $errorexist;
 
					}
				} else {
 
					include $vincludepath . $default .'.php';
				}
			}
		} else {
 
			echo $errori;
 
		}
 
	} else {
 
		if (preg_match("/=/", $getqs, $matchqs))
		{
			$httpvars = $GLOBALS['_GET'];
 
			foreach($httpvars as $key => $value)
			{
				if ($value != '')
				{
 
					include $vincludepath . $default .'.php';
 
				}
			}
 
		} else {
 
			$site = basename($_SERVER['QUERY_STRING']);
 
			if(!$site)
			{
 
				include $vincludepath . $default .'.php';
 
			} else {
 
				if (preg_match("/^[=\-_a-zA-Z0-9]+$/",$site, $matchqs))
				{
					if(file_exists($vincludepath . $site .'.php'))
					{
 
						include($vincludepath . $site.'.php');
 
					} else {
 
						echo $errorexist;
 
					}
				} else {
 
					echo $errori;
 
				}
 
			}
		}
	}
 
?>

Leave a Reply