งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Chapter 4 Server-side Technologies

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Chapter 4 Server-side Technologies"— ใบสำเนางานนำเสนอ:

1 Chapter 4 Server-side Technologies
Details 4.1 PHP 4.2 ASP 4.3 JSP 4.4 Perl and CGI

2 4.1 PHP PHP :Hypertext Preprocessor
- abbreviated from Personal Home Pages - one of the most popular server-side scripting languages - was created in 1994 by Rasmas Lerdorf - an open-source technology - platform independent - supports a large number of databases more : PHP: Hypertext Preprocessor เดิมทีเรียกว่า “Personal Home Page Tools” เป็นภาษาแบบ server-side scripting ทำงานบนฝั่ง server พัฒนาจากภาษา C แปลภาษาแบบ Interpretation คือแปลคำสั่งแล้ว execute ทีละบรรทัด แต่ในเวอร์ชั่น ตั้งแต่ Version 4 ขึ้นมาทำการ compile คำสั่งแล้วเก็บไว้ใน Memory เพื่อทำการ Execute เมื่อมีการเรียกซ้ำ เป็น Open-source ผู้สนใจสามารถดู แก้ไข เสนอไฟล์โค้ดของตนเองเป็นการเผยแพร่ได้ มีกลุ่มผู้สนใจ PHP คอยสนับสนุนให้คำแนะนำ นำไปใช้ได้กับหลายๆ Platform (Platform independent) เช่น Unix, Windows, Linux, etc. ถูกคิดค้นขึ้นในปีค.ศ.1994 (พ.ศ.2537) โดย Mr.Rasmus Lerdorf เพื่อใช้ตรวจสอบสถิติการเข้าชมเว็บของตนเอง ปีค.ศ.1995 มีการเผยแพร่ และพัฒนาเป็น PHP/FI หรือ PHP รุ่น 2 ให้มีความสามารถมากขึ้น กลางปีค.ศ.1996 เริ่มมีทีมพัฒนา โดยมี Mr.Zeev Suraski และ Mr.Andi Gutmans ร่วมทีม และได้ PHP รุ่น 3

3 การเตรียมการเพื่อเขียนโปรแกรมภาษา PHP
เครื่องคอมพิวเตอร์อย่างน้อย 1 เครื่อง โดยปกติการพัฒนาระบบงานบนเว็บจะต้องมีเครื่องคอมพิวเตอร์ที่ทำเป็น Web Server และ Web browser แต่ถ้ามีงบประมาณจำกัดในการจัดหาเครื่องคอมพิวเตอร์ก็สามารถใช้เครื่องคอมพิวเตอร์เพียงเครื่องเดียวก็ได้ โดยให้เครื่องคอมพิวเตอร์นี้ทำหน้าที่เป็นทั้ง Web Server และ Web browser ระบบปฏิบัติการ Windows หรือ Unix หรือ Linux โปรแกรม Web Server ซึ่งสามารถเลือกใช้โปรแกรมอะไรก็ได้ เช่น Apache, Internet Information Server (IIS), OmniHTTPd เป็นต้น

4 การเตรียมการเพื่อเขียนโปรแกรมภาษา PHP
PHP Engine หรือตัวแปลภาษา PHP ซึ่งเป็นโปรแกรมที่จะทำหน้าที่แปลภาษา PHP เป็นภาษาเครื่องเพื่อให้คอมพิวเตอร์สามารถทำงานและประมวลผลได้ตามที่ต้องการ โปรแกรมจัดการฐานข้อมูล ซึ่งโปรแกรมภาษา PHP สามารถเลือกใช้ฐานข้อมูลได้หลายประเภท เช่น dBase, IMAP, MySql, MS Access, Oracle เป็นต้น หากเลือกใช้ฐานข้อมูล MySql ควรใช้ PhpMyadmin ซึ่งเป็นโปรแกรมที่ใช้จัดการฐานข้อมูล MySql โดยเฉพาะ โปรแกรม Web Authoring และ Editor คือโปรแกรมที่ใช้ในการสร้างเว็บเพจ เช่น Macromedia Dreamweaver, Microsoft Frontpage, Editplus, Notepad เป็นต้น

5 Characteristic of PHP - file extension is .php
- PHP statements are terminated with a semicolon ; - one-line comment begins with // or # - multiline comment begins with /* and end with */

6 รูปแบบการฝัง PHP script ในเอกสาร
แบบที่ 1 แทรก script ด้วยเครื่องหมาย <?php และ ?> <?php echo “This is also simple, do like this <br>”; ?> แบบที่ 2 กำหนดด้วย tag <script></script> <script language = “php”> echo “Hello php<br>”; </script> แบบที่ 3 ครอบส่วนที่เป็น script ด้วย <? และ ?> เรียกว่า short style <? echo “This is the simplest way<br>”; ?>

7 รูปแบบการฝัง PHP script ในเอกสาร (ต่อ)
แบบที่ 4 ใช้ tag ของ ASP <% echo “you may use ASP-style tags”; %> หมายเหตุ จะต้องกำหนด asp_tags ในเอกสาร config ก่อน จึงจะใช้ได้ (ใช้ได้กับ php version เท่านั้น)

8 Basic PHP <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
" <!-- first.php --> <?php $name = "LunaTic"; // declaration ?> <html xmlns= <head> <title>A simple PHP document</title> </head> <body style = "font-size: 2em"> <p> <strong> Welcome to PHP, <?php print( "$name" ); ?>! </strong> </p> </body> </html>

9 Define PHP Variable names
use characters, number, underscore and $ $var_name - first character must not be number - don’t use name as same as built-in functions or any keyword

10 PHP Keywords and false or break for require case foreach return
class function static continue global switch default if this do include true else list var elseif new virtual extends not xor while

11 PHP data type Data type Description int, integer float, double string
bool, Boolean array object NULL Whole numbers without decimal point Real numbers Text enclosed in either single or double quotes True of false Group of the same type elements Group of associated data and methods No value

12 Integer เขียนอยู่ในรูปของเลขฐานได้ คือฐาน 10 ฐาน 8 : ใช้ 0 นำหน้า
และ ฐาน 16 : ใช้ 0x นำหน้า $i = 254; $j = -431; $k = 0123; # มีค่าเท่ากับ (123 ฐาน 8 มีค่าเท่ากับ 83) $m = 0x12; # มีค่าเท่ากับ (12 ฐาน 16 มีค่าเท่ากับ 18 )

13 Float String เขียนในรูปแบบของเลขทศนิยมและเลขยกกำลัง $i = 6.254;
$j = -2e12; # มีค่าเท่ากับ -2 x 1012 String ใช้จัดเก็บตัวอักษรและข้อความทั่วไป โดยจะต้องอยู่ ในเครื่องหมาย double quotes (“ ”)

14 String รหัสควบคุมพิเศษที่ใช้กับ string สัญลักษณ์ ความหมาย \n
ใช้ขึ้นบรรทัดใหม่ \f เลื่อนเคอร์เซอร์ไปต้นบรรทัด \t ใช้เลื่อน Tab \\ ใช้พิมพ์เครื่องหมาย \ (Backslash) \$ ใช้พิมพ์เครื่องหมาย $ (Dollar) \” ใช้พิมพ์เครื่องหมาย “ (Double Quote) \0 กำหนดจุดสิ้นสุดของ string

15 Define data type - we can define data type by assign value to
variables <? $num = 60-30; echo $num.“<br>”; $num = “test”; echo $num; ?> - type casting <? $x = 23.4; $y = 12.7; $z = $x+$y; echo “z = ”.$z.“<br>”; $x = (integer)$x; $y = (integer)$y; echo “z = ”.$z; ?> ผล 30 test ผล z = 36.1 z = 35 ประเภทของตัวแปรจะถูกกำหนดในขณะ run time และสามารถเปลี่ยนไปได้ขึ้นอยู่กับว่าเราต้องการให้ตัวแปรนั้นเก็บค่าอะไร

16 Define data type - variable variables <? $num = “y”; $$num = 6;
echo $y. “<br>”; echo $num; ?> - constants int define (string name, mixed value) <? define (“value”, “constant data”); define (“x”, 12); define (“y”,3); $z = $x+$y; echo “value = ”.$value. “<br>”; echo “z = ”.$z; ?> ผล 6 y ผล value = constant data z = 15

17 Variable function gettype - use for show type of variable
string gettype(mixed var) settype - use for define data type of variable int settype(string var, string type) Function gettype จะ return string รับ parameter เป็นตัวแปรประเภทอะไรก็ได้ settype จะ return int รับ parameter เป็น string กับ string

18 Example <? $value = 26.425; echo “value = ”;
echo gettype($value). “<br>”; settype($value, “integer”); echo “type of variable<br>”; echo gettype($value); ?> ผล value = double type of variable value = integer

19 Operator Arithmetic operator สัญลักษณ์ ชื่อ ตัวอย่าง + Addition $x+$y
- Subtraction $x-$y * Multiplication $x*$y / Division $x/$y % Modulus $x%$y

20 Operator String operator
การนำข้อมูลมารวมกันเป็นข้อความเดียวกันใช้เครื่องหมายจุด (.) Assignment operator เป็นการกำหนดค่าให้กับตัวแปร ใช้เครื่องหมายเท่ากับ (=) - Combination Assignment operators ได้แก่ +=, -=, *=, /=, %=, .= เช่น $x+= 2 - Pre/Post Increment and Decrement ได้แก่ $x++, ++$x, $y--, --$y

21 Operator Comparison operator
การนำเปรียบเทียบค่า 2 ค่า เพื่อคืนค่าเป็น true หรือ false sym name example == equals $y == $x != not equal $y != $x <> $y <> $x < less than $y < $x > greater than $y > $x <= less than or equal to $y <= $x >= greater than or equal to $y => $x

22 Operator Logical operator คำสั่งเงื่อนไขทางตรรกะ sym name example !
NOT !$x && AND $y && $x || OR $y || $x

23 Control structure - if statement รูปแบบ if (condition)
ตัวอย่าง if ($height > 0 && $width > 0) { $area = $height*$width; echo “Area of square = ” .$area; }

24 Control structure - if…else statement รูปแบบ if (condition)
ตัวอย่าง if ($height > 0 && $width > 0) { $area = $height*$width; echo “Area of square = ” .$area; } { echo “Please enter new value”;}

25 Control structure - if…elseif statement (nested if statement)
รูปแบบ if (condition1) { statement; ……} elseif (condition2) ตัวอย่าง if ($score < 0) echo “Enter new score\n”; elseif ($score >= 80.0) $grade = “G”; elseif ($score >= 60.0) $grade = “P”;

26 Control structure - switch statement รูปแบบ switch (variable)
{ case value1 : statement; break; case value2 : statement; default : statement; }

27 Loop structure - for statement
รูปแบบ for (initial value; condition; change value) { statement; …… } - while statement รูปแบบ while (condition) - do…while statement รูปแบบ do { statement; …… } while (condition);

28 Array ที่มี index เป็นตัวเลข
array array([mixed….]); $province = array(“Bangkok”, “Trad”, “Chiang Mai”); for ($i=0;$i<3;$i++) { echo “province[”.$i. “] = ”; echo $province[$i]; echo “<br>”; }

29 array range(int ค่าเริ่มต้น, int ค่าสูงสุด);
$arr = range(4,9); for ($i=0;$i<count($arr);$i++) { echo "arr[".$i."] = "; echo $arr[$i]; echo "<br>"; }

30 Array ที่มี index เป็นตัวอักษร
- ฟังก์ชัน each() เป็นฟังก์ชันที่คืนค่า 2 ค่าคือ index และค่าของข้อมูล โดยที่ค่า index จะคืนด้วยฟิลด์ key และ ค่าของข้อมูลจะ คืนด้วยฟิลด์ value - ฟังก์ชัน list ทำงานเช่นเดียวกับ each() แต่จะแยก array ออกจาก กันและเก็บในตัวแปรที่กำหนด - ฟังก์ชัน reset ใช้สำหรับย้ายพอยน์เตอร์ ไปชี้ที่ค่าแรกของ array

31 Array ที่มี index เป็นตัวอักษร
การสร้าง array ใช้ฟังก์ชัน array() $arr_list=array("Bangkok"=>36,"Tak"=>10,"Trad"=>15); while ($element = each($arr_list)) { echo "province ["; echo $element["key"]; echo "] = "; echo $element["value"]; echo "<br>"; }

32 Example $arr_list=array("Bangkok"=>36,"Tak"=>10,"Trad"=>15);
while (list($province,$amphur) = each($arr_list)) { echo "province [".$province."] have "; echo $amphur." amphurs"; echo "<br>"; }

33 Array หลายมิติที่มี index เป็นตัวเลข
$arr_list = array(array("BKK","Bangkok",36), array("TK","Tak",10), array("TR","Trad",15)); for($row=0;$row<3;$row++) { for($col=0;$col<3;$col++) echo $arr_list[$row][$col]." "; echo "<br>"; }

34 Array หลายมิติที่มี index เป็นตัวอักษร
$arr_list=array(array("Code"=>"BKK", "Des"=>"Bangkok","amphur"=>36), array("Code"=>"TK","Des"=>"Tak","amphur"=>10), array("Code"=>"TR","Des"=>"Trad","amphur"=>15)); for($row=0;$row<3;$row++){ echo $arr_list[$row]["Code"]." "; echo $arr_list[$row]["Des"]." "; echo $arr_list[$row]["amphur"]."<br>"; }

35 การใช้งาน string การตัดช่องว่างในสตริง - trim()
สำหรับตัดช่องว่างทั้งหมดที่อยู่ด้านหน้าและด้านหลัง ของสตริงทิ้งไป - ltrim() ตัดช่องว่างที่อยู่ด้านหน้า(ทางซ้าย) ทิ้งไป - chop() ตัดช่องว่างที่อยู่ด้านหลัง (ทางขวา) ทิ้งไป

36 trim() <? $str1 =" Computer Science\n\n";
print "String1 = ".$str1."<br>"; echo "Length of String1 = ".strlen($str1)."<br>"; $str2 = trim($str1); echo "Length of String2 = ".strlen($str2)."<br>"; ?>

37 ltrim() <? $str1 =" Computer Science\n\n";
print "String1 = ".$str1."<br>"; echo "Length of String1 = ".strlen($str1)."<br>"; $str2 = ltrim($str1); echo "Length of String2 = ".strlen($str2)."<br>"; ?>

38 chop() <? $str1 =" Computer Science\n\n ";
print "String1 = ".$str1."<br>"; echo "Length of String1 = ".strlen($str1)."<br>"; $str2 = chop($str1); echo "Length of String2 = ".strlen($str2)."<br>"; ?>

39 nl2br() เปลี่ยน \n ให้เป็น <br>
<? $str =" Computer\n Science\nChiang Mai\n\n"; print "String = ".$str."<br>"; echo “use nl2br() <br>"; echo "String = ".nl2br($str); ?>

40 การแสดงข้อมูลทางจอภาพ
การแสดงตัวเลขโดยใช้คำสั่ง echo นั้น จะไม่ สามารถจัดรูปแบบได้ แต่ถ้าใช้ฟังก์ชัน printf() และ sprintf() จะสามารถจัดรูปแบบได้ <? $str =" Computer\n Science\nChiang Mai\n\n"; printf("String = %s<br>",$str); print “<em>use nl2br() </em><br>"; echo "String = ".nl2br($str); ?>

41 ค่า args สำหรับการแสดงรูปแบบ
ชนิด ความหมาย b c d f o s x X แสดงเป็นเลขฐานสอง แสดงเป็นตัวอักขระตามรหัสแอสกีครั้งละ 1 ตัว แสดงเป็นเลขฐานสิบ แสดงเป็นเลขจำนวนจริง แสดงเป็นเลขฐานแปด แสดงเป็นตัวอักษร (String) แสดงเป็นเลขฐานสิบหกและเป็นตัวอักษรตัวเล็ก a-f แสดงเป็นเลขฐานสิบหกและเป็นตัวอักษรตัวใหญ่ A-F

42 ตัวอย่างการแสดงผล $val = 109.2536859;
printf("binary = %b<br>",$val); printf("decimal = %d<br>",$val); printf("character = %c<br>",$val); printf("floating = %.4f<br>",$val); printf("octal = %o<br>",$val); printf("string = %0.5s<br>",$val); printf("hexalower = %x<br>",$val); printf("hexaupper = %X<br>",$val); $val1 = sprintf("%b",$val); $val2 = sprintf("%d",$val); $val3 = sprintf("%c",$val); echo "binary = ".$val1."<br>"; echo "decimal = ".$val2."<br>"; echo "character = ".$val3."<br>";

43 การเปลี่ยนรูปแบบของตัวอักษร
ฟังก์ชัน คำอธิบาย strtoupper() strtolower() ucfirst() ucwords() แปลงประโยคให้เป็นตัวพิมพ์ใหญ่ แปลงประโยคให้เป็นตัวพิมพ์เล็ก แปลงตัวอักษรตัวแรกของประโยคให้เป็น ตัวพิมพ์ใหญ่ แปลงตัวอักษรตัวแรกของคำให้เป็นตัวพิมพ์ ใหญ่ ตัวอย่างคำสั่ง $str = “Please ENTER your name”; strtoupper($str); strtolower($str); ucfirst($str); ucwords($str);

44 การใช้ Regular Expression
แทนที่ข้อมูลแบบสตริง ซึ่งคล้ายกับการใช้ฟังก์ชัน เปรียบเทียบและแทนที่ของสตริง แต่ regular expression จะสามารถกำหนดรูปแบบของสตริงได้มากกว่า เพื่อใช้ใน การค้นหาสตริงที่ใกล้เคียงกันได้

45 ฟังก์ชันสำหรับตรวจสอบค่าสตริง
ฟังก์ชันการค้นหาสตริงย่อย - ereg() ใช้สำหรับตรวจสอบข้อความและตัวอักษร ฟังก์ชันจะตี ความตัวอักษรพิมพ์ใหญ่และตัวอักษรพิมพ์เล็กแตกต่างกัน รูปแบบ int ereg(string pattern, string string) - eregi() ใช้สำหรับการตรวจสอบข้อความและตัวอักษร โดยจะ ตีความตัวอักษรพิมพ์ใหญ่และตัวพิมพ์เล็กเหมือนกัน รูปแบบ int eregi(string pattern, string string)

46 ตัวอย่าง <? $str1 = "Computer Science, Chiang mai";
$com = "Chiang mai"; echo "Comparision string1<br>"; if (ereg($com,$str1)) echo "Found String<br>"; else echo "Not found <br>"; echo "Comparision string2<br>"; if (ereg($com,$str2)) echo "Comparision string2 with eregi<br>"; if (eregi($com,$str2)) ?>

47 ฟังก์ชันการแทนที่สตริงย่อย - ereg_replace()
ใช้สำหรับแทนที่ข้อความและตัวอักษร ฟังก์ชันจะตี ความตัวอักษรพิมพ์ใหญ่และตัวอักษรพิมพ์เล็กแตกต่างกัน รูปแบบ string ereg_replace(string pattern, string replace,string string) - eregi_replace() ใช้สำหรับแทนที่ข้อความและตัวอักษร โดยจะตีความตัว อักษรพิมพ์ใหญ่และตัวพิมพ์เล็กเหมือนกัน string eregi_replace(string pattern, string replace,string string)

48 ตัวอย่าง <? $str1 = "Computer Science, Chiang mai";
echo "before replace string = $str1<br>"; echo "after replace string = "; echo ereg_replace("Chiang mai","CMU",$str1)."<br>"; echo eregi_replace("computer scienCE","Cs",$str1); ?>

49 ฟังก์ชันการแบ่งสตริง - split()
ใช้สำหรับแบ่งสตริงแล้วไปเก็บไว้ในอาร์เรย์ รูปแบบ array split(string pattern, string search, int [max]) - spliti() ใช้สำหรับแบ่งสตริง โดยจะตีความตัวอักษรพิมพ์ใหญ่และ ตัวพิมพ์เล็กเหมือนกัน array spliti(string pattern, string search, int [max])

50 ตัวอย่าง <? $str1 = "Computer Science, Chiang mai";
echo "after substring<br>"; $array1 = split(" ",$str1); for ($i=0;$i<count($array1);$i++) echo "array1[$i]=$array1[$i]<br>"; echo "after substring with c<br>"; $array3 = spliti("c",$str1); for ($i=0;$i<count($array3);$i++) echo "array3[$i]=$array3[$i]<br>"; ?>

51 User-Defined Function
ฟังก์ชันที่ไม่มีการส่งค่ากลับ รูปแบบ function function_name (argument_list ) { statement; ..... } ฟังก์ชันที่มีการส่งค่ากลับ return ค่าที่จะส่งกลับ;

52 User-Defined Function
ตัวอย่าง <?php function check_length($data) { if (strlen($data) < 6) return "The data was too small"; else return "That data was fine"; } ?> การเรียกใช้ สามารถเรียกใช้ที่จากที่ใดก็ได้ในไฟล์ php $example = "qwertyuiop"; echo check_length($example);

53 ตัวอย่าง Pass by value function one ( $parameter ) { $parameter++; } $a = 10 ; one( $a ) ; echo “ a = $a <BR>” ; ผลลัพธ์ของโปรแกรมคือ a = 10

54 ตัวอย่าง Pass by reference
function one ( &$parameter ) { $parameter++; } $a = 10 ; one( $a ) ; echo “ a = $a <BR>” ; ผลลัพธ์ของโปรแกรมคือ a = 11

55 ตัวอย่าง PHP with Form <html> <body> <form action="formprocess.php" method="POST" name="fmProcess"> ชื่อ: <input name="firstname" type="text" size="32" maxlength="30"> <br> นามสกุล: <input name="lastname" type="text" size="32" <input name="btnSubmit" type="submit" value="บันทึก"> <input name="btnReset" type="reset" value="ยกเลิก"> </form> </body> </html> form.html

56 ตัวอย่าง PHP with Form <html> <body> <?php echo "ชื่อ: " . $_POST['firstname'] . "<br>"; echo "นามสกุล: " . $_POST['lastname'] . "<br>"; ?> </body> </html> formprocess.php

57 การทำงานของ Method : Post , Get
- POST สงขอมูลโดยสงขอมูลสงไปกับ HTTP Request - GET สงขอมูลโดยการแปะคาเปน URL Parameter (Query String) ไปกับ URL (ขอมูลที่สงจะแสดงอยูบน URL ของเว็บเบราเซอร) NOTE: วิธีการสงขอมูลแบบ GET ไมควรใชกับฟอรมที่มีการสงขอมูล จํานวนมาก รวมทั้งไมควรใชในการสงขอมูลที่เปนความลับ เชน username, password หรือเลขที่บัตรเครดิต เปนตน เนื่องจากวิธีการสงแบบ GET นี้ ขอมูลที่เราสงจะถูกแสดงบน URL เชน


ดาวน์โหลด ppt Chapter 4 Server-side Technologies

งานนำเสนอที่คล้ายกัน


Ads by Google