Upload
odo-stiehl
View
107
Download
2
Embed Size (px)
Citation preview
Dynamische Webseiten mit PHP
Oder: LAMP - The open way
Überblick
• Dynamische Webseiten• Entwicklung von PHP• PHP an Beispielen• Anbindung an mySQL• Projekt• Literatur:
– Williams, Lane, Web database Applications with PHP and MySQL, O‘Reilly, 2002
– Lerdorf, Tatroe, Programieren mit PHP, O‘Reilly, 2003
PHP
• Personal Homepage Tools, Rasmus Lerdorf• Erste Version 1994
– CGI Tools– keine Scriptsprache
• PHP/FI 1996– Scriptsprache, Anbindung an Datenbanken
• PHP Version 3 1998– Open Source, Suraski, Gutmans
• PHP Version 4 2000– Redesign, Zend
Verbreitung
Neben ASP verbreitetste Web-Scripting-UmgebungLAMP (Linux, Apache, mySQL, PHP): freie Alternative zu M$
dynamische Webseiten
public class myServlet extends HttpServlet {
protected void doGet(...) {
response.setContentType( "text/html"); java.io.PrintWriter out = response.getWriter(); out.println("<HTML>“); ... out.println(“Hallo"); out.println("</HTML>“); } }
<HTML>...<?php echo “Hallo"; ?></HTML>
Programm erzeugt HTML HTML enthält Programm
verschiedene Implementierungen:PHP (C-ähnlich)JSP (JAVA)ASP (Visual Basic (und andere)usw.
Einbetten von Code
<HTML>...<?php // XML-Stil echo “Hallo"; ?>
<? /* SGML (PI) Stil */ echo “Hallo"; ?>
<% # ASP Stilecho “Hallo"; %>
<script language=“php" // „korrektes“ HTMLecho “Hallo"; </script></HTML>
Syntax<?php $i = 0; // Variablen werden nicht deklariert echo $i; // Name beginnt mit $ echo $I; // undefiniert (Schreibweise), aber Echo $i; // funktioniert. if ($I == null) // undef. Variablen haben Wert null if (isset($I)) // oder so $i= “Hallo"; // Datentyp implizit (Zuweisung) $i= ‘Hallo‘; // String durch ‘ oder “ terminiert $i=0.3E-4; // Integer, Fließkomma, String, $i=true; // Boolean $namen[0]=“max“; // Array $namen[1]=“hugo“; // Größe nach Bedarf $namen[]=“gerd“; // „hinten“ anhängen $namen=array(“max“,“hugo“,“gerd“); // Operatoren wie in JAVA, . verknuepft Strings $text =“Hello“ . “world“;?>
Kontrollstrukturen<?phpif (1 == 1) // Syntax wie in JAVA//Schleifen: for, while, do ... while, foreach$namen=array(“max“,“hugo“,“gerd“); for($i=0;$i<count($namen);$i++) echo $namen[$i];while ($i < 10) { echo $i; $i++;}
// Durchlaufen von Arraysforeach($namen as $name) echo $name;?>
Funktionen<?phpfunction tuwas($i) { echo $i; $i++;}
$wert = 10;tuwas($wert);echo $wert // 10, call by value
function tuwas(&$i) { // 11, call by reference...tuwas($wert);echo $wert;
function x($y=1) { // default Werte für Parameter//Werte zurückgeben mit return $...?>
Variablenfunktionen
<?phpfunction minus($i) { return $i-1;}
function plus($i) { return $i+1;}
$op = “plus“;echo $op(3);?>
Wie andere Scriptsprachen auch ist PHP ziemlich flexibel,ein Beispiel:
Ein Beispiel
<HTML><body><? for($i=0;$i<10;$i++)
echo "Hello world<br>";?></body></HTML>
HTML-Formulare<HTML><body><form action="DoHelloForm.php" method="post">Wie oft ? <input type="text" name="WieOft"><input type="submit"></form></body></HTML>
<HTML><body><? for($i=0;$i<$WieOft;$i++) // $WieOft enthält Eingabe
echo "Hello world<br>";?></body></HTML>
DoHelloForm.php
All in one
<HTML><body><? if (isset($WieOft)) { for($i=0;$i<$WieOft;$i++) echo "Hello world<br>";}else { ?> <form action="CompactHelloForm.php" method="post"> Wie oft ? <input type="text" name="WieOft"> <input type="submit"> </form> <?}?></body></HTML>
besser
<HTML><body><form action="RealHelloForm.php" method="post">Wie oft ? <input type="text" name="WieOft"<? if (isset($WieOft)) echo ' value="'.$WieOft.'"'?>><input type="submit"></form><? if (isset($WieOft)){for($i=0;$i<$WieOft;$i++)
echo "Hello world<br>";}?></body></HTML>
Zugriff auf mySQL
<? // Verbindung mit mysql_connect aufbauen$connection = mysql_connect("localhost","till","haenisch");// Datenbank auswählenmysql_select_db("ba",$connection);// Abfrage ausführen$result = mysql_query("SELECT text from data",$connection);// Resultset durchgehenwhile ($row = mysql_fetch_row($result)){
// $row ist ein Array mit den Inhalten der Attribute echo $row[0];echo "<br>";
}// Verbindung wieder schließenmysql_close($connection);?>
alternativ
...$result = mysql_query("SELECT text from data",$connection);// Resultset durchgehenwhile ($row = mysql_fetch_array($result)){
// $row ist jetzt ein assoziatives Arrayecho $row["text"];echo "<br>";
}...// Insert geht übrigens genausomysql_query(„INSERT INTO data(text) values('Hallo');
sieht schon ziemlich ähnlich wie ASP aus, oder ?
Cookies
<? if (!isset($count)) // unser Cookie {
$count=0; } else $count++; // Cookies müssen gesetzt werden, bevor html ausgegeben wird setcookie("count",$count);?><html><body>Hallo zum <?=$count?> ten Mal.</body></html>
Dazu braucht man also Cookies ...(eins pro Variable --> umständlich, Typ != String ??)
Sessions<? session_start(); if (!session_is_registered("count")) {
session_register("count"); $count=0;
} else $count++;?><html><body>Hallo zum <?=$count?> ten Mal.</body></html>
Ein Cookie pro Session (alternativ URL rewriting)Beenden mit session_destroy(), z.B. beim logout