index.php
[php]<?php
// Alle Fehler ausgeben
error_reporting(E_ALL | E_STRICT);
// UTF-8 als Ausgabe-Encoding sicherstellen
header(‚Content-Type: text/html; charset=UTF-8‘);
// Nur Strings, die in dieser Liste auftauchen, sind als Eingaben für mögliche
// Unterseiten erlaubt
$allowedPages = array(
‚home‘,
‚mathe/fibonacci‘,
‚test2‘
);
// Falls URL-Parameter p nicht gesetzt ist, setze auf Startseite
$_GET[‚p‘] = (isset($_GET[‚p‘]))
? $_GET[‚p‘]
: ‚home‘;
// Stelle sicher, dass nachgefragte Seite unter den erlaubten Seiten ist, lade
// ansonsten eine Seite „error“
if (!in_array($_GET[‚p‘], $allowedPages)) {
$_GET[‚p‘] = ‚error‘;
}
$contentPath = ‚./pages/‘ . $_GET[‚p‘] . ‚.phtml‘;
$fileExists = file_exists($contentPath);
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="styles.css" media="screen" rel="stylesheet" type="text/css" />
<title>Testseite</title>
</head>
<body>
<div class="wrapper">
<div class="header">
header
</div>
<div class="sidebar">
<ul class="navigation">
<li><a href="./?p=home">Startseite</a></li>
<li><a href="./?p=mathe/fibonacci">Fibonacci-Reihe</a></li>
<li><a href="./?p=test2">Noch eine Unterseite</a></li>
</ul>
</div>
<div class="maincontent">
<?php if ($fileExists) :
include $contentPath;
else :
?>
<h1>Fehlende Seite</h1>
<p>
Die Seite "<?php echo htmlspecialchars($contentPath); ?>"
sollte vorliegen, wurde aber nicht gefunden.
</p>
<?php endif; ?>
</div>
<div class="footer">
footer
</div>
</div>
</body>
[/php]
styles.css
[code]body {
margin: 0;
padding: 0;
background: #fff;
color: #666;
}
.wrapper {
margin: 0 auto;
width: 900px;
background: #cff;
}
.header {
height: 100px;
padding: 10px;
background: #fcc;
}
.sidebar {
float: left;
width: 180px;
padding: 10px;
background: #cfc;
}
.maincontent {
margin-left: 200px;
padding: 10px;
background: #ffc;
}
.footer {
clear: both;
padding: 10px;
background: #ccf;
}[/code]
pages/home.phtml
[html]
Herzlich willkommen!
Willkommen auf meiner Seite zu den Themen...
[/html]
pages/error.phtml
[html]
Fehler
Die angeforderte Seite existiert nicht. Bitte überprüfen Sie Ihre Links.
[/html]
pages/mathe/fibonacci.phtml
[php]<?php
// Quelle: http://www.scriptol.com/programming/fibonacci.php
function fibonacci($length)
{
for( $l = array(1,1), $i = 2, $x = 0; $i < $length; $i++ )
{
$l = $l[$x++] + $l[$x];
}
return $l;
}
?>
Fibonacci-Zahlen
Die ersten 20 Zahlen der Fibonacci-Reihe lauten:
<?php echo implode("\n", fibonacci(20)); ?>
[/php]