PHP ProgrammingPHP Programming
Windows socket Interface
FTPServer
Telnet Server
WWW Server
DNSServer
UDPTCP
IP
Application
Host to Host
Internet
Network
TCP Port 20,21
TCP Port :80 TCP Port :53TCP Port :23
Application LayerApplication Layer
การทำ�างานของโปรโตคอลชั้��นน��จะเป�นการเข�าใชั้�การทำ�างานของโปรโตคอลชั้��นน��จะเป�นการเข�าใชั้�ทำร�พยากรทำร�พยากร
ระยะไกล ระยะไกล ((Remote AccessRemote Access ) ) และการแชั้ร�ใชั้�และการแชั้ร�ใชั้�ทำร�พยากร ทำร�พยากร
((Resource SharingResource Sharing ) ) โปรโตคอลทำ��อย !ใน โปรโตคอลทำ��อย !ใน Layer Layer น��ได้�แก!น��ได้�แก!
HTTP (Hyper Text Transfer Protocol)ใชั้�สำ�าหร�บการร�บสำ!งไฟล�เว็(บเพจ ระหว็!างเว็(บบราว็เซอร�และเว็(บเซ*ร�ฟเว็อร�
SMTP (Simple Mail Transfer Protocol)ใชั้�สำ�าหร�บการร�บสำ!งอ�เมล�ระหว็!างเมล�เซอร�เว็อร�
Application LayerApplication LayerFTP (File Transfer Protocol)FTP (File Transfer Protocol)
ใช้�สำ��หรับก�รัถ่ �ยโอนไฟล์�รัะหว่ �งโฮสำต์�ทำ��ได้�เฉพ�ะสำม�ช้!กเทำ �น"น
ก�รับรั!ก�รัอย# 2 ปรัะเภทำ คื(อ Private FTP / เฉพ�ะกล์) ม น!ยมใช้�ต์�มสำถ่�นศึ+กษ�แล์ะภ�ยในบรั!ษทำ ผู้#�ใช้�บรั!ก�รัจะต์�องม0รัหสำผู้ �นเฉพ�ะจ+งจะใช้�ง�นได้� ปรัะเภทำทำ02สำองคื(อ Anonymous FTP/FTP สำ�ธ�รัณะให�
บรั!ก�รัด้�ว่น�โหล์ด้ไฟล์�ข้�อม#ล์ฟรั0โด้ยไม ต์�องม0รัหสำผู้ �น ซึ่+2งป7จจ)บนม0บรั!ก�รัในล์กษณะน0"เป8นจ��นว่นม�ก โด้ยเฉพ�ะโปรัแกรัมซึ่อฟต์�แว่รั�ใหม ๆ ทำ02ต์�องก�รัเผู้ยแพรั ไปสำ# สำ�ธ�รัณช้น
Web ServerWeb Serverเว่:บเซึ่!รั�ฟเว่อรั� เว่:บเซึ่!รั�ฟเว่อรั� ((Web ServerWeb Server ) ) คื(ออะไรัคื(ออะไรั
เคร,�องบร*การเว็(บไซต� หร,อเว็(บเพจผ่!าน เคร,�องบร*การเว็(บไซต� หร,อเว็(บเพจผ่!าน httphttp ://.. .. .. ://.. .. .. เพ,�อให�ได้�ข�อม ลทำ��งเพ,�อให�ได้�ข�อม ลทำ��งภาพ เสำ�ยงจากภาพ เสำ�ยงจาก
ผ่ �ให�บร*การ เชั้!นบร*การ ผ่ �ให�บร*การ เชั้!นบร*การ httphttp://://wwwwww..googlegoogle..com com ทำ��เป/ด้บร*การเว็(บจากเคร,�องทำ��เป/ด้บร*การเว็(บจากเคร,�องให�บร*การทำ��ให�บร*การทำ��
เร�ยกว็!า เร�ยกว็!า Web Server Web Server สำ�าหร�บบทำคว็าม หร,อข�อม ลในหน�าน��ให�ข�อม ลเก��ยว็ก�บการสำ�าหร�บบทำคว็าม หร,อข�อม ลในหน�าน��ให�ข�อม ลเก��ยว็ก�บการทำ�า ทำ�า
Web Server Web Server ด้�ว็ย โปรแกรมต!าง ๆด้�ว็ย โปรแกรมต!าง ๆ Web Server Web Server ทำ��เป�นทำ��น*ยมได้�แก! ทำ��เป�นทำ��น*ยมได้�แก! PWS, PWS, IIS IIS และ และ Apache\Apache\
บร*การเว็(บเซ*ร�ฟเว็อร� จะม�บร*การเสำร*มต!าง ๆ สำ�าหร�บน�กพ�ฒนา ทำ��ทำ�าให�เว็(บไซต�บร*การเว็(บเซ*ร�ฟเว็อร� จะม�บร*การเสำร*มต!าง ๆ สำ�าหร�บน�กพ�ฒนา ทำ��ทำ�าให�เว็(บไซต�สำมบ รณ์� เชั้!นบร*การภาษา หร,อระบบฐานข�อม ล ซ5�งแต!ละโปรแกรมม�คว็ามแตกต!างสำมบ รณ์� เชั้!นบร*การภาษา หร,อระบบฐานข�อม ล ซ5�งแต!ละโปรแกรมม�คว็ามแตกต!าง
ก�นไปก�นไปเชั้!น ภาษา เชั้!น ภาษา HTML, PERL, PHP, ASPHTML, PERL, PHP, ASP หร,อ หร,อ JSPJSP เป�นต�น สำ!ว็นฐานข�อม ลเป�นต�น สำ!ว็นฐานข�อม ล
อาจใชั้� อาจใชั้� MSAccess, Mysql, MSSQLMSAccess, Mysql, MSSQL หร,อ หร,อ OracleOracle เป�นต�นเป�นต�น
Apache Apache คื(ออะไรัคื(ออะไรั Apache Apache ค,อ โปรแกรมค,อ โปรแกรม ทำ��ทำ�าหน�าทำ��เป�น ทำ��ทำ�าหน�าทำ��เป�น web web server server ทำ��ม�ผ่ �ใชั้�ทำ��ว็โลกมากกว็!า ทำ��ม�ผ่ �ใชั้�ทำ��ว็โลกมากกว็!า 6060 % % ม�หน�าทำ��ในการม�หน�าทำ��ในการจ�ด้เก(บ จ�ด้เก(บ Homepage Homepage และสำ!ง และสำ!ง Homepage Homepage ไปย�ง ไปย�ง Browser Browser ทำ��ม�การเร�ยกเข�า ย�ง ทำ��ม�การเร�ยกเข�า ย�ง Web server Web server ทำ��เก(บ ทำ��เก(บ HomePage HomePage น��นอย !ซ5�งป6จจ7บ�นจ�ด้ได้�ว็!าเป�น น��นอย !ซ5�งป6จจ7บ�นจ�ด้ได้�ว็!าเป�น web web server server ทำ��ม� คว็ามน!าเชั้,�อถื,อมาก ซ5�งสำามารถืหามา ทำ��ม� คว็ามน!าเชั้,�อถื,อมาก ซ5�งสำามารถืหามา Download Download ได้�จาก ได้�จาก website website www.apache.orgwww.apache.org
Web ArchitectureWeb Architecture
Data base MySQL
ApacheWeb Server
TCP/IP Port:3306
InternetInternet
TCP/IP Port:80
TCP/IP Port:80Via Web Brower
Web ArchitectureWeb Architecture (3)(3)
HTMLDocument
ApacheWeb Server
includePhp engine
PHPDocument
Get / Post
MySQL Database
Php Configuration
withMySQL Library
Grant Allow forConnect via
Apache and PHP
TCP/IP Port:3306
WebBrower
Apache + PHP + MysqlApache + PHP + Mysqlการจะต*ด้ต��ง การจะต*ด้ต��ง Apache webserver Apache webserver และจะร �สำ5กว็!าเป�นภาระหน�กย*�งข5�น หากเราและจะร �สำ5กว็!าเป�นภาระหน�กย*�งข5�น หากเราต�องการเพ*�ม ต�องการเพ*�ม
MySQL, PHP MySQL, PHP และ และ Perl Perl เข�าไปด้�ว็ย ป6จจ7บ�นเราไม!ต�องลงแรงขนาด้น��น เน,�องจากเข�าไปด้�ว็ย ป6จจ7บ�นเราไม!ต�องลงแรงขนาด้น��น เน,�องจากในโลกของในโลกของ
ชั้7มชั้นน�กพ�ฒนาจะม�โปรแกรมจ�าลองเคร,�องพ�ซ�ให�เป�นเว็บเซอร�ฟเว็อร�ได้�อย!างชั้7มชั้นน�กพ�ฒนาจะม�โปรแกรมจ�าลองเคร,�องพ�ซ�ให�เป�นเว็บเซอร�ฟเว็อร�ได้�อย!างง!ายด้าย ง!ายด้าย
โปรแกรม โปรแกรม XAMPP XAMPP เป�นหน5�งในโปรแกรมยอด้น*ยมทำ��น�กพ�ฒนาน�ามาใชั้�ก�นเป�นหน5�งในโปรแกรมยอด้น*ยมทำ��น�กพ�ฒนาน�ามาใชั้�ก�นXAMPP XAMPP ได้�รว็บรว็ม ได้�รว็บรว็ม package package ต!าง ๆ ไว็� เพ,�อลด้คว็ามย7!งยากในการต*ด้ต��ง ต!าง ๆ ไว็� เพ,�อลด้คว็ามย7!งยากในการต*ด้ต��ง package package ต!าง ๆ ทำ��ต!าง ๆ ทำ��
ใชั้�ในการทำ�า ใชั้�ในการทำ�า webserver webserver ซ5�งภายในโปรแกรมจะประกอบไปด้�ว็ยซ5�งภายในโปรแกรมจะประกอบไปด้�ว็ยApache, MySQL, PHP Apache, MySQL, PHP + + PEAR, Perl, mod_php, PEAR, Perl, mod_php, mod_perl, mod_ssl, OpenSSL, phpMyAdmin, mod_perl, mod_ssl, OpenSSL, phpMyAdmin, Webalizer, Mercury Mail Transport System for Webalizer, Mercury Mail Transport System for Win32 and NetWare Systems v3.32, JpGraph, Win32 and NetWare Systems v3.32, JpGraph, FileZilla FTP Server, mcrypt, eAccelerator, FileZilla FTP Server, mcrypt, eAccelerator, SQLite, and WEBSQLite, and WEB--DAV DAV + + mod_auth_mysqlmod_auth_mysql..
xamppxampp--win32-1.4.15-win32-1.4.15-installerinstaller
xamppxampp--win32-1.4.15-win32-1.4.15-installerinstaller
xamppxampp--win32-1.4.15-win32-1.4.15-installerinstaller
xamppxampp--win32-1.4.15-win32-1.4.15-installerinstaller
xamppxampp--win32-1.4.15-win32-1.4.15-installerinstaller
xamppxampp--win32-1.4.15-win32-1.4.15-installerinstaller
xamppxampp--win32-1.4.15-win32-1.4.15-installerinstaller
xamppxampp--win32-1.4.15-win32-1.4.15-installerinstaller
http://localhost/http://localhost/
Getting StartGetting Start
Web RootWeb Root
Introduction for PHPIntroduction for PHP
ใช้�ในก�รัเป;ด้แล์ะป;ด้ ใช้�ในก�รัเป;ด้แล์ะป;ด้ Script Script ทำ02เข้0ยนด้�ว่ย ทำ02เข้0ยนด้�ว่ย PHPPHP<?PHP<?PHP
……....
?>?>
หรั(อหรั(อ<?<?
……....
?>?>
Introduction for PHPIntroduction for PHP
PHPPHP : : Hypertext PreprocessorHypertext Preprocessor
<html> <head> <title>Example</title> </head> <body> Say by HTML <br>
<?php
echo " Hi, I'm a PHP script!";
?> </body></html>
<html> <head> <title>Example</title> </head> <body> Say by HTML <br>
<?php
echo " Hi, I'm a PHP script!";
?> </body></html>
Introduction for PHPIntroduction for PHP
ทำ�าการแสำด้งผ่ลเป�นเอกสำารร ปแบบ ทำ�าการแสำด้งผ่ลเป�นเอกสำารร ปแบบ HTML HTML DocumentDocument<html> <head> <title>Example</title> </head> <body> Say by HTML <br>
Hi, I'm a PHP script! </body></html>
<html> <head> <title>Example</title> </head> <body> Say by HTML <br>
Hi, I'm a PHP script! </body></html>
Introduction for PHPIntroduction for PHP
Function print() and echo() Function print() and echo()
การแสำด้งผ่ลข�อม ล/ ข�อม ล
print “Hello”;echo “Hello”;
หรั(อ
print (“Hello”);echo (“Hello”);
แบบบรรทำ�ด้เด้�ยว็ // ข�อคว็าม # ข�อคว็าม
แบบหล์�ยบรัรัทำด้ /* ข�อคว็ามบรรทำ�ด้ทำ�� 1 ข�อคว็ามบรรทำ�ด้ทำ�� 2 */
ก�รัเข้0ยนหม�ยเหต์)ให�กบโปรัแกรัม ก�รัเข้0ยนหม�ยเหต์)ให�กบโปรัแกรัม (Comment)(Comment)
Show all predefined Show all predefined
variables with variables with phpinfophpinfo()() <?php phpinfo<?php phpinfo (); (); ?>?> pphpinfohpinfo.php.php
Workshop Workshop ทำ02 ทำ02 11 ftp://sec3@riderftp://sec3@rider สำร�าง สำร�าง folder folder ด้�ว็ยรห�สำน�กศึ5กษาด้�ว็ยรห�สำน�กศึ5กษา สำร�าง สำร�าง file file ชั้,�อ ชั้,�อ index.phpindex.php
<HTML><HEAD><TITLE> 48111111111</TITLE></HEAD><BODY><h1> 48111111111</h1> Hello form HTML<br><? for ($i = 0 ;$i < 5 ; $i++) { echo “Hi, PHP Script. <br>”; }?></BODY></HTML>
Workshop Workshop ทำ02 ทำ02 22 ftp://sec3@riderftp://sec3@rider สำร�าง สำร�าง folder folder ด้�ว็ยรห�สำน�กศึ5กษาด้�ว็ยรห�สำน�กศึ5กษา สำร�าง สำร�าง file file ชั้,�อ ชั้,�อ info.phpinfo.php
<HTML><HEAD><TITLE> 48111111111</TITLE></HEAD><BODY><? phpinfo();?></BODY></HTML>
Dealing with FormsDealing with Forms A simple HTML formA simple HTML form <form action="action.php " method="post">
Your name : <input type="text " name="name " /> <br> Your age : <input type="text " name="age " /> <br>
<input type="submit " /> <br>
</form>
Hi <?php echo $name; ?>.You are <?php echo $age; ?> years old.
File name :test.php
File name :action.php
Hi Joe . You are 22 years old.
Out put from this script
Workshop Workshop ทำ02 ทำ02 33 ftp://sec3@riderftp://sec3@rider สำร�าง สำร�าง folder folder ด้�ว็ยรห�สำน�กศึ5กษาด้�ว็ยรห�สำน�กศึ5กษา สำร�าง สำร�าง file file ชั้,�อ ชั้,�อ input.php, print.phpinput.php, print.php
<HTML><HEAD><TITLE> 48111111111</TITLE></HEAD><BODY><h1> 48111111111</h1><FORM METHOD=POST ACTION=“print.php"> Input your name <INPUT TYPE="text" NAME=“yourname"></FORM></BODY></HTML> Input.php
My name is My name is
<? echo $yourname ?> .<? echo $yourname ?> .
print.phpprint.php
Web ArchitectureWeb Architecture
HTMLDocument
ApacheWeb Server
includePhp engine
PHPDocument
Get / Post
Php Configuration
withMySQL Library
WebBrower
โครงสำร�างภาษา โครงสำร�างภาษา PHPPHP
PHP จะคล�ายภาษาคอมพ*ว็เตอร�ทำ��ว็ไป เชั้!น C, Perl, Java
ร ปแบบม�ด้�งน��– ช้น!ด้ข้�อม#ล์ (Data Type)– ต์ว่แปรั (Variable)– โอเปอเรเตอร� (Operator)– ค!าคงทำ�� (Constants)– เง,�อนไขเพ,�อต�ด้สำ*นใจ (If…elseif…else, swich…case)– การทำ�างานว็นรอบ (For, while, Do..while, Foreach)
ต์ว่แปรั ต์ว่แปรั ((VariablesVariables))1. ชั้,�อต�ว็แปรต�องข5�นต�นด้�ว็ย $ (Dollar sign) จากน��นตามด้�ว็ยต�ว็อ�กษร ต�ว็เลข หร,อสำ�ญล�กษณ์�2. สำามารถืน�าต�ว็อ�กษรก�บต�ว็เลขผ่สำมก�นได้� $name_1, $Salary1, $phone3. ชั้,�อต�ว็แปรไม!สำามารถืเว็�นว็!างหร,อเคาะเว็�นว็รรคได้�4. คว็รสำ,�อคว็ามหมายในต�ว็ม�นเอง5. ชั้,�อต�ว็แปร อ�กษรต�ว็เล(ก ต�ว็ใหญ!น��นม�คว็ามสำ�าค�ญมาก (case Sensitive) เชั้!น $address, $Address, $ADDRESS ถื,อว็!าเป�นต�ว็แปรคนละต�ว็ก�น
PHP supports 8 primitive PHP supports 8 primitive typestypes
4 scalar types4 scalar types – booleanboolean – integerinteger – floatfloat - (floating point number, aka ' - (floating point number, aka 'doubledouble ') ')
– stringstring
2 compound types2 compound types– arrayarray – objectobject
2 2 special typesspecial types– resourceresource – NULLNULL
booleanboolean
keyword TRUE or FALSE . Both are case-insensitive
เป�นของม ลทำ��ม�ค!า 2 ค!าค,อ true (ถื ก) หร,อ false (ผ่*ด้)
integerintegerใช้�สำ��หรับเก:บข้�อม#ล์ต์ว่เล์ข้ จ��นว่นเต์:ม เล์ข้
ฐ�นต์ �งๆ
Floating point numbersFloating point numbers ใช้�เก:บข้�อม#ล์ต์ว่เล์ข้จ��นว่นจรั!ง +,- ทำ02ม0ทำศึน!ยมแล์ะไม ม0ทำศึน!ยม
$a = 1.23; //จ�านว็นเต(มบว็ก$a = -1.23; //จ�านว็นเต(มลบ
$a = 1.2e3; // จ�านว็นเต(มบว็กในร ปยกก�าล�ง // 1.2*10^3 =1200
$a = -1.2e3; // จ�านว็นเต(มลบในร ปยกก�าล�ง // - 1.2*10^3 = -1200
StringsStrings ใช้�เก:บข้�อม#ล์ต์ว่อกษรัหรั(อข้�อคืว่�ม
“ข้�อคืว่�ม” single quotedsingle quoted
$str = ‘$str = ‘Example of stringExample of string’;’; double quoted double quoted
$str = “$str = “Example of stringExample of string”;”; heredoc syntaxheredoc syntax
$str $str = <<<EOD = <<<EODExample of stringExample of stringspanning multiple linesspanning multiple linesusing heredoc syntaxusing heredoc syntax..EOD;EOD;
How to assign How to assign to string to string variablevariable
Single quotedSingle quoted
Double quotedDouble quoted
HeredocHeredoc
ArraysArraysใช้�เก:บข้�อม#ล์เป8นช้)ด้ หรั(ออ�รั�เรัย�ก�รัก��หนด้ต์ว่แปรัจะอย# ในเคืรั(2องหม�ย [….]
ArraysArrays
unset() unset() ใชั้�ในการยกเล*กค!าของ ใชั้�ในการยกเล*กค!าของ arrayarray
การแสำด้งค!าใน การแสำด้งค!าใน arrayarray
Objectใช้�เก:บข้�อม#ล์เพ(2อเรั0ยกก�รัใช้� class object แล์ะ Functions
Type JugglingType Juggling
ใช้�เก:บข้�อม#ล์ในล์กษณะเฉพ�ะหรั(อผู้#�ใช้�เพ!2มเต์!มเข้��ม�
PHP Work Shop 3.PHP Work Shop 3.
ให�ร�บข�อม ลจาก ให�ร�บข�อม ลจาก form html form html แล�ว็ทำ�าการแสำด้งแล�ว็ทำ�าการแสำด้งผ่ลด้�งน��ผ่ลด้�งน��
PHP Work Shop 3.PHP Work Shop 3.
แสำด้ง แสำด้ง output output ด้�งน��ด้�งน��
ว็�ธี�การทำ�า ว็�ธี�การทำ�า PHP Work Shop 3.PHP Work Shop 3. ทำ�าการสำร�าง ทำ�าการสำร�าง file file ใน ใน Document rootDocument root
ให�สำรั��ง folder ด้�ว่ยรัหสำนกศึ+กษ�เช้ น 48111111111
ว็�ธี�การทำ�า ว็�ธี�การทำ�า PHP Work Shop 3.PHP Work Shop 3.WorkShop1.php
ว็�ธี�การทำ�า ว็�ธี�การทำ�า PHP Work Shop 3.PHP Work Shop 3.
acction.php