nav-left cat-right
cat-right
Home » Web » Simple PHP based Website

Simple PHP based Website

You want to have a Website with as less code as possible, a CSS based design, but should work template based? This is my solution I worked out three years ago. It was based on articles like these:

The idea is relatively simple. It makes use of mod_rewrite, so that the URI does not need to match a real file. This URI is then evaluated by the PHP script which then includes the correct files. To make it really simple it does not even need a database, and because I have absolutely no knowledge about databases.

Lets start with the .htacces file to direct the URI to our script
RewriteEngine on
RewriteRule   ^download/.*$      -             [L]
RewriteRule   ^bbclone/.*$      -             [L]
RewriteRule !\.(gif|jpg|png|css|php|pdf|zip)$ index.php

This means that everything exept URIs with the folder /download/ and /bbclone/ and all files with ending gif, jpg, png, css, php, pdf, and zip are directed to index.php. This file could also have a differend name. Its content is shown below. This file does most of the magic is pretty simple:

< ?php
 
// Show all Errors
// error_reporting(E_ALL);
 
$secureREQUEST_URI = $HTTP_SERVER_VARS['REQUEST_URI'];
$secureDOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
 
// initilize the variables
 
define("PageRootDir","");
$thisPage="";
$thisSubPage="";
$thisSubSubPage="";
$page_URI_content=PageRootDir."includes/";
 
//Beware of hackers
 
if(strlen($secureREQUEST_URI)>100){
	header("Status: 404 Not Found");
	echo "Fehler 404\nDatei oder Verzeichnis nicht gefunden";
	exit();
}
 
// 1. check to see if a "real" file exists
// - Test if this is not index.php, that would mean and endless loop
// - URI must not be "/" because that does exist, 
//   but means to load index.php 
// If a request passes all these checks, we load the file using 
// include() and stop the execution of index.php using exit().
 
if(file_exists($secureDOCUMENT_ROOT.$secureREQUEST_URI)
	and ($SCRIPT_FILENAME!=$secureDOCUMENT_ROOT.$secureREQUEST_URI)
	and ($secureREQUEST_URI!="/")){
		$url=$secureREQUEST_URI;
		include($secureDOCUMENT_ROOT.$url);
		exit();
}
 
 
// 2. if not, go ahead and check for dynamic content.
$url=strip_tags($secureREQUEST_URI);
$page_URI = $url;
$url_array=explode("/",$url);
array_shift($url_array); //the first one is empty anyway
 
/* First, we transform the $secureREQUEST_URI to an array which 
   is easier to handle:
   We use strip_tags() to remove HTML or JavaScript tags from the 
   Query String (basic hack protection), and then use explode() 
   to split the $secureREQUEST_URI at the slashes ("/"). Finally, 
   using array_shift(), we remove the first array entry because it's
   always empty. (Why? Because $secureREQUEST_URI always 
   starts with a "/"). */
 
//we got a request for the index
if(empty($url_array) or $url=="/"){ 
	$page_URI_content=PageRootDir."includes/".'startseite.php'; 
	include(PageRootDir."includes/inc_index2.php");
	exit();
}
 
/* What if that $url_array is empty? You may have realized 
   that this corresponds to the case of the $secureREQUEST_URI
   containing only a slash ("/"), which I mentioned above. */
 
$link_depth=count($url_array);
$thisPage=$url_array[0];
if ($link_depth>1) {$thisSubPage=$url_array[1];} else {$thisSubPage="";}
if ($link_depth>2) {$thisSubSubPage=$url_array[2];} else {$thisSubPage="";}
if ($link_depth>3) {$thisSubSubSubPage=$url_array[3];} else {$thisSubSubPage="";}
 
 
/* Auf PHPMyAdmin Bereich testen - diesen laden, ansonsten
   anderen Möglichkeiten (normale Index) testen*/
if ($thisPage=="phpmyadmin") {
	include("phpmyadmin/index.php");
	exit();
}
 
 
// Aus dem übergebenem Pfad auf die zu ladende Datei schließen:
// /abc/123/def --> /includes/abc/123/def/def.php
 
for($a=0;$a< $link_depth-1;$a++) {
	$page_URI_content=$page_URI_content.$url_array[$a]."/";
}
$page_URI_content=$page_URI_content.$url_array[$link_depth-2].'.php';
$temp_documentcheck=$secureDOCUMENT_ROOT."/".$page_URI_content;
 
 
if(file_exists($temp_documentcheck)){  
	include(PageRootDir."includes/inc_index2.php");
	exit();
}
else{
	header("Status: 404 Not Found");
	echo "Fehler 404\nDatei oder Verzeichnis nicht gefunden";
	exit();
}
 
?>
You should have seen that the script converts the URI into several variables:
  • $page_URI contains the URI as inserted by the visitor
  • $page_URI_content contains the link to the file on the webserver
  • $thisPage contains the name of the first link
  • $thisSubPage contains the name of the sublink
  • $thisSubSubPage contains the name of the sublink of the sublink, od so forth
after these have been set, the real index.php site in loaded:
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
< ?php
  require_once 'php/various.php';
  require_once 'php/breadcrumb.php';
  require_once 'php/convert_names.php';
  require_once 'php/check_currentnavigation.php';
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
 
<head>
	< ?php  include 'includes/header.php';?>
	<link rel="stylesheet" href="/css/main2.css" type="text/css" >
	</link><link rel="stylesheet" href="/css/layout21.css" type="text/css" >
</link></head>
<body>
<a name="top"></a>
<div id="pagewidth">
<div id="header">
	< ?php  include 'includes/pagehead2.php';?>
</div>
 
<div id="navmenu">
	< ?php require_once ("includes/navmenu2.php");?>	
	<div style="display: block;clear: both;"></div>
</div>
 
<div id="leftside_wrapper">
	<div id="rightside_wrapper">
		<div id="container">
			<div id="adjust_right">		
 
				<div id="left_sidebar">					
					<div style="padding:0.5em">
						< ?php require_once ("includes/sidebar.php");?>
						< ?php require_once ("includes/sidebar_right.php");?>					
					</div>
				</div>
				<div id="adjust_left">
					<div id="youarehere">
						<span class="bread left ">< ?php breadCrumb($page_URI); ?></span>
						<span class="date right">< ?php include 'php/date_printout.php';?></span>				
						<span style="clear:right">&nbsp;</span>
					</div>
 
					<div id="main_content">
						< ?php require_once ("$page_URI_content");?>
						<span style="clear:right">&nbsp;</span>
					</div>
				</div>
 
			</div>
		</div>
 
		<!--<div id="right_sidebar">
		</div>-->
 
		<div class="clearing">&nbsp;</div>
	</div>
 
</div>
 
<div id="footer">
	<div style="padding: 5px">
		<a href="/impressum/">Impressum</a>&nbsp;::&nbsp;<a href="/disclaimer/">Disclaimer</a><br />
		Content Copyright © 2000 - < ?php   $d = time();  echo strftime('%Y', $d);?>, Matthias Pospiech
	</div>
</div>
<!-- End of PAGEWIDHT -->
 
</body>
</html>
article needs to be finished …
Print Page