PHP Hypertext Preprocessor (PHP)

PHP-Logo


Einleitung

PHP Überblick

PHP Architektur

Hello World

Einbettung:

<html>
<head>
<title>Test</title>
</head><body>
Test auf PHP:
<?php echo "<p>Hallo von PHP</p>"; ?>
<?php echo "<p>$HTTP_USER_AGENT</p>"; ?>
<?php show_source("hallo.phtml"); ?>
</body>

Ausgabe:

Test auf PHP:
Hallo von PHP
Mozilla/4.5 [en] (X11; I; Linux 2.0.35 i586)
...

PHP Beispiele


PHP Sprache

Die Syntax ist von C, Java und Perl abgeleitet.

Werte, Variablen, Typen

   $foo = "0";
   $foo++;
   $foo += 1;

   $bar = (string) $foo;
   $foo = (int) $bar;

   $a = "b";
   $$a = "c";
   echo $b;

Kontrollstrukturen

Funktionen.

function tuwas($a) {
  echo "Eingabe = ", $a;
}

tuwas("mit einem Text");
function tuwas($a) {
  return "Eingabe = " . $a;
}

echo tuwas(´mit einem Text´);

PHP Pakete

dbm Datenbank

Ursprünglich von Berkeley, db, gdbm.

Auf (fast) allen Unix Systemen ohne Installation verfügbar.

nur (Key, Value)-Paare

Beispiel: Zugriffszähler

Einbettung mittels auto_prepend, oder direkt in die Seite

Verwendung

<?php 
  /* $counter_start="9999"; */
  echo "<h3>" . counter(). " Zugriffe,";
  echo " " . modified("de") . "</h3>"; 
?>

Counter Funktion

$filename=$SCRIPT_FILENAME;
$counter_start="1";
function counter() {
        global $filename, $counter_start;
        $counter_dir="/tmp/";
        $counter_db=$counter_dir . "zaehler.dbm";
        if (file_exists("$counter_db")) {
           $db=dbmopen($counter_db,"w");
           if ( dbmexists($db,$filename) ) {
               $cnt=dbmfetch($db,$filename);
               if ($counter_start=="1") { $cnt++; } 
               else { $cnt=$counter_start; }
               dbmreplace($db,$filename,$cnt);
           }
           else {
               $cnt=$counter_start;
               dbminsert($db,$filename,$cnt);
           }
           dbmclose($db);
           return "$cnt";
        }
        else {
           echo "Attempt to create file: " . $counter_db;
           $cnt=$counter_start;
           $db=dbmopen($counter_db,"n");
           dbminsert($db,$filename,$cnt);
           dbmclose($db);
           return "$cnt";
        }
}

Modified Funktion

$filename=$SCRIPT_FILENAME;
function modified($lang) {
        global $filename;
        $lm=date("d M Y H:i:s",filectime($filename));
        if ($lang=="de") {
           return "Geändert am $lm";
        } else {
           return "Last modified: $lm";
        }
}

PHP Beispiele

Installation, Konfiguration

Auf jedem Web-Server as CGI Programm

zum Beispiel bei Apache:

AddType application/x-php3-script .phtml
Action  application/x-php3-script /cgi-bin/php/

bei Apache als ladbares Modul

Aktivierung einzelner Datenbanken und Pakete

Weitere Direktiven z.B. in php3.ini oder in Apache Konfigurationsfiles mit Prefix php3_ oder im Code

Beispiel: php3.ini

Sicherheitsprobleme

sollten mit neuen Versionen behoben sein


Ausblick


© Universität Mannheim, Rechenzentrum, 1998-2001.

Heinz Kredel
Last modified: Wed Dec 20 22:19:34 MET 2000