PHP Template System semantischer Fehler

selberbauer

Captain
Registriert
Juni 2009
Beiträge
3.604
Hallo,
Ich übe gerade an einem Template System. Jetzt allerdings habe ich ein Problem, wenn ich auf unterseiten wechseln möchte, also der Aufruf über die Browserleiste mittels "localhost/index.php?section=contact" funktioniert, nur das wechseln auf der Seite selbst macht Probleme (Apache findet das Objekt nicht).

index.php
PHP:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

include('tmpl.php');
include('tmpl-out.php');
?>

tmpl.php
PHP:
<?php

$menu = array();
$menu[] = array('section' => 'home',		'title' => 'Home');
$menu[] = array('section' => 'about_us',	'title' => 'Über uns');
$menu[] = array('section' => 'services',	'title' => 'Dienstleistungen');
$menu[] = array('section' => 'reference',	'title' => 'Referenzen');
$menu[] = array('section' => 'tutorials',	'title' => 'Tutorials');
$menu[] = array('section' => 'downloads',	'title' => 'Downloads');
$menu[] = array('section' => 'contact',		'title' => 'Kontakt');

$current = $_GET['section'];

foreach ($menu as $m) {
	$class = '';
	if ($current == $m['section']) {
		$class = ' class="current"';
	}
	$slice = "\t\t<li" . $class . '><a href="index?section=' . $m['section'] . '">' . $m['title'] . "</a></li>\r\n";
	$all  .= $slice;
}

if (!isset($_GET['section'])) {
	$content = 'home.tpl';
} else {
	$content = $_GET['section'] . '.tpl';
}

?>

tmpl-out.php
PHP:
<!DOCTYPE html>
<html>
 <head>
  <title>Tartaros</title>
  <meta charset="utf-8" />
  <meta name="author" content="" />
  <meta name="keywords" content="" />
  <meta name="robots" content="index, follow" />
  <meta http-equiv="X-UA-Compatible" content="chrome-1" />
  <link rel="stylesheet" media="all" href="style.css" />
 </head>

 <body>
  <section>
   <header>
   	<h1></h1>
   	<p></p>
   </header>
   
   <nav>
    <ul>
<?php echo $all; ?>
    </ul>
   </nav>

   <article>
<?php include($content); ?>
   <article>

   <footer>
    <p>© 2011 <a href="index?section=contact">Impressum</a></p>
   </footer>
  </section>
 </body>
</html>
 
Mach doch mal aus den "index?..." jeweils "index.php?...", dann weiß er auch, dass er die index.php aufrufen soll.
 
:freak: das es immer die Kleinigkeiten sind :lol:

Danke für die Hilfe, gibt es noch weitere Sachen die sich verbessern ließen (z.B. Einsatz von Klassen?) oder ist das so ok?
 
Zuletzt bearbeitet:
Mit dem
Code:
<?php include($content); ?>
in der "tmpl-out.php" und dem
Code:
$content = $_GET['section'] . '.tpl';
in der "tmpl.php" sollte man vorsicht sein, da der Aufrufer in der Kombination darüber potentiell (fast) beliebige Dateien auf dem Server abrufen kann. Benutzereingaben am besten NIE direkt irgendwo benutzen!

Wenn du schon eine Liste mit gültigen Seiten hast, dann sollte man den Wert der Variable auch gegenchecken. Hier machen die das z.B. auch: http://tut.php-q.net/de/template.html
 
Zuletzt bearbeitet:
PHP:
<?php

$valide = false;

$menu = array();
$menu[] = array('section' => 'home',		'title' => 'Home');
$menu[] = array('section' => 'about_us',	'title' => 'Über uns');
$menu[] = array('section' => 'services',	'title' => 'Dienstleistungen');
$menu[] = array('section' => 'reference',	'title' => 'Referenzen');
$menu[] = array('section' => 'tutorials',	'title' => 'Tutorials');
$menu[] = array('section' => 'downloads',	'title' => 'Downloads');
$menu[] = array('section' => 'contact',		'title' => 'Kontakt');
 
$current = htmlentities($_GET['section']);
 
foreach ($menu as $m) {
	$class = '';
	if ($current == $m['section']) {
                $valide = true;
		$class = ' class="current"';
	}
	$slice = "\t\t<li" . $class . '><a href="index?section=' . $m['section'] . '">' . $m['title'] . "</a></li>\r\n";
	$all  .= $slice;
}
 
if (!isset($current) OR $valide === false) {
	$content = 'home.tpl';
} elseif($valide === true) {
	$content = $current . '.tpl';
}

if (isset($current) AND $valide === true) {
	$content = $current . '.tpl';
} elseif($valide === true) {
	$content = 'home.tpl';
}
 
?>

So in etwa könnte man es wohl auch machen, zumindest um das Ganze etwas sicherer zu machen.
 
Stimmt, daran habe ich nicht gedacht.
Habe tmpl.php jetzt ergänzt, sodass eine foreach Schleife prüfe, ob $_GET['section'] sich im $menu-array befindet, sollte dem so sein, kann $_GET['section'] weiterverwendet werden.
Der Code funktioniert, aber irgendwie kommt mir das ein bisschen "pfushig" vor - ist das begründet?

PHP:
$menu = array();
$menu[] = array('section' => 'home',		'title' => 'Home');
$menu[] = array('section' => 'about_us',	'title' => 'Über uns');
$menu[] = array('section' => 'services',	'title' => 'Dienstleistungen');
$menu[] = array('section' => 'reference',	'title' => 'Referenzen');
$menu[] = array('section' => 'tutorials',	'title' => 'Tutorials');
$menu[] = array('section' => 'downloads',	'title' => 'Downloads');
$menu[] = array('section' => 'contact',		'title' => 'Kontakt');

$current = $_GET['section'];

foreach ($menu as $m) {
	$class = '';
	if ($current == $m['section']) {
		$class = ' class="current"';
	}
	$slice = "     <li" . $class . '><a href="index.php?section=' . $m['section'] . '">' . $m['title'] . "</a></li>\r\n";
	$all  .= $slice;
}

if (!isset($_GET['section'])) {
	$content = 'home.tpl';
} else {
	foreach ($menu as $m) {
		if ($m['section'] = $_GET['section']) {
			$content = $_GET['section'] . '.tpl';
			break;
		}
	}
}
 
Und warum machst du das soooo kompliziert?
Bereits ab Zeile 12 durchläufst du doch eh alle Elemente - warum speicherst du dir dann nicht gleich dort, ob die aktuelle Seite "valide" ist oder nicht?

Davon abgesehen: if($a = $b) wird nicht funktionieren ....

Warum nutzt du eigentlich an manchen Stellen $current, an anderer Stelle $_GET['section'] und dann auch noch vollkommen unvalidiert? Was ist, wenn dir dort jemand Schadcode einschleußt?

Hast du mal den Quelltext den ich getippt hab angeschaut?
 
Sry, habe deinen Post übersehen, da der fast zeitgleich kam wie meiner.
Werde den mal durchlesen

Gruß
 

Ähnliche Themen

Zurück
Oben