Dynamische Webseiten mit PHP Oder: LAMP - The open way
Verbreitung Neben ASP verbreitetste Web-Scripting-Umgebung LAMP (Linux, Apache, mysql, PHP): freie Alternative zu M$
Überblick Dynamische Webseiten Entwicklung von PHP PHP an Beispielen Anbindung an mysql Projekt Literatur: Beighly, PHP&MYSQL von Kopf bis Fuß, O'Reilly, 2009 Lerdorf, Tatroe, Programming PHP, O Reilly, 2013
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 PHP Version 5 2004
dynamische Webseiten Programm erzeugt HTML HTML enthält Programm! 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>! 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 Stil! echo Hallo";! %>! <script language= php" echo Hallo";! </script>! </HTML>!!// korrektes 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 <?php! if (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 Arrays! foreach($namen as $name)! echo $name;!
Funktionen <?php! function tuwas($i) {! echo $i;! $i++;! $wert = 10;! tuwas($wert);! echo $wert!!!// 10, call by value! function tuwas(&$i) {...! tuwas($wert);! echo $wert;!!// 11, call by reference! function x($y=1) {!!// default Werte für Parameter! //Werte zurückgeben mit return $...!
Variablenfunktionen Wie andere Scriptsprachen auch ist PHP ziemlich flexibel, ein Beispiel: <?php! function minus($i) {! return $i-1;! function plus($i) {! return $i+1;! $op = plus ;! echo $op(3);!
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>! DoHelloForm.php <HTML>! <body>! <?! for($i=0;$i<$wieoft;$i++) // $WieOft enthält Eingabe!!echo "Hello world<br>";! </body>! </HTML>!
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ählen! mysql_select_db("haenisch",$connection);! // Abfrage ausführen! $result = mysql_query("select nick,url,email! from BENUTZER",$connection);! // Resultset durchgehen! while ($row = mysql_fetch_row($result))! {! // $row ist ein Array mit den Inhalten der Attribute! echo $row[0];! echo "<br>";! // Verbindung wieder schließen! mysql_close($connection);!
alternativ...! $result = mysql_query("select nick,url,email! from BENUTZER",$connection);! // Resultset durchgehen! while ($row = mysql_fetch_array($result))! {! // $row ist ein Array mit den Inhalten der Attribute! echo $row["nick"];! echo "<br>";! }...! // Insert geht übrigens genauso! mysql_query( INSERT INTO data(text) values('hallo');! sieht schon ziemlich ähnlich wie ASP aus, oder?
mysqli <?! // das ganze mit der neueren mysqli-schnittstelle! $db = mysqli_connect("localhost","till","haenisch","haenisch");! // Abfrage ausführen! $result = mysqli_query($db,"select nick,url,email from BENUTZER );! // Resultset durchgehen! while ($row = mysqli_fetch_array($result))! {! // $row ist ein Array mit den Inhalten der Attribute! echo $row["nick"];! echo "<br>";! // Verbindung wieder schließen! mysqli_close($db);!
mysqli-oo <?! // PHP kann auch Objekte...! $db = new mysqli("localhost","till","haenisch","haenisch");! // Abfrage ausführen! $result = $db->query("select nick,url,email from BENUTZER );! // Resultset durchgehen! while ($row = $result->fetch_assoc())! {! // $row ist ein Array mit den Inhalten der Attribute! echo $row["nick"];! echo "<br>";! // Verbindung wieder schließen! db->close($db);!
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