Modul Proyek Pemrograman m01-m02
Modul Proyek Pemrograman m01-m02
Tujuan : 1. Dapat menjelaskan arsitektur aplikasi Web. 2. Dapat menjelaskan perbedaan static Web dan dynamic Web. 3. Dapat menjelaskan pengertian Client Side Technology dan mampu memberikan contohcontohnya. 4. Dapat menjelaskan pengertian Server Side Scripting dan mampu memberikan conothcontohnya. 5. Dapat memberikan contoh nyata aplikasi web.
URAIAN MATERI 1.1 Arsitektur Aplikasi Web Arsitektur Client-Server o Merupakan arsitektur yang menjelaskan pemisahan kerja antara penyedia informasi/layanan (server) dengan peminta informasi (client), o Seringkali client dan server beroperasi dalam jaringan komputer dengan client dan server ada di mesin/hardware yang berbeda, o Mesin server biasanya adalah host dengan performansi tinggi yang menjalankan satu atau lebih program server. server membagi informasi ke client, o Client tidak membagi informasi namun meminta informasi dari server, o Client menginisialisasi sesi komunikasi dengan server. server menunggu (await/listen) pada request yang datang. Web Server o Program komputer yang bertanggung jawab untuk menerima request dari client kemudian merespon request tersebut. request dari client dapat berupa dokumendokumen web (web resources) seperti dokumen html, image dll. o Komputer yang menjalankan program komputer seperti yang dijelaskan diatas. o Contoh web server: Apache, IIS, Xitami Web Client o program komputer yang meminta dokumen-dokumen web (web resource) ke web server. o web client seringkali disebut sebagai browser. o contoh web client: Internet Explorer (IE), Mozilla Firefox, Opera HTTP o kependekan dari Hypertext Transfer Protocol o protokol yang mengatur bentuk komunikasi antara web server dan web client
o o o
merupakan stateless protocol, protokol yang tidak me-maintain hubungan tiap request. tiap request tidak terkait dengan request-request sebelumnya memiliki beberapa request method, request yang paling sering dipergunakan dalam pembuatan aplikasi web adalah get dan post memiliki header yang menentukan bagaimana karakteristik data yang di-request ataupun yang dihasilkan. contoh header http yang pengembang aplikasi web sering pergunakan adalah Content-Type dan Location
Two Tier o jenis arsitektur client server dimana: user interface dijalankan di client database disimpan di server Application logic dapat dijalnkan baik di client ataupun di server Three Tier o jenis arsitektur client server yang terdiri dari: tier 1, user interface dijalankan di client tier 2 / middle tier, functional modules yang memproses data. jalan pada sebuah server. sering disebut juga sebagai application server. Tier 3 / data tier, database management system (DBMS) yang menyimpan data yang diperlukan middle tier. sering disebut juga sebagai database server.
1. 2. 3.
Cara Kerja : Browser merequest file hello.html ke web server, lalu server akan mencari file tersebut . Server akan mengirimkan file yang di-request Browser menampilkannya.
Web Server Web Client 3. Browser memunculkan Hello World ! 2. HTTP Respone <html> <head> <title>Hello World !</title> </head> <body> <h1>Hello World !</h1> </body> </html>
Kelebihan o Browser apa saja dapat memunculkannya dengan cukup baik. o Perangkat yang lain (contoh: hp) data memunculkannya dengan cukup baik. o Tiap request dengan cepat akan dipenuhi dan sedikit menggunakan resource system. o HTML mudah dipelajari. o Web developer dapat membuat perubahan kecil pada pag dengan cepat. Kekurangan o Sulit mengontrol desain dan layout. o Kurang mengakomodir page yang banyak. o Kurang interaktif. o Kurang mengakomodir content yang berubah dengan cepat atau personalisasi page.
1.3 Client Side Technologies teknologi yang dioperasikan/dijalankan disisi client. alasan diperlukannya teknologi yang dijalankan disisi client adalah : o client memerlukan akses informasi atau fungsi yang ada di client namun tidak diserver (inputan user) o server dapat kekurangan waktu proses untuk menjalankan seluruh request client o jika tanpa mengirimkan data ke server berarti memerlukan waktu yang lebih sedikit, mengurangi bandwidth dan mengurangi resiko keamanan. contoh: javascript, vbscript 1.4 Client-side HTML Extensions table
Client Side Technology Cascading Style Sheets, Dynamic HTML Client Side Scripting (Javascript, VbScript)
Kegunaan
Contoh Penerapan
Overlapping, different colored/sized fonts Layers, Exact Positioning Link that changes color on mouseover Calculator Short cartoon movie Flash Games
Flash animations
Animation
1.5 Server Side Scripting/Programming script dijalankan disisi server memungkinkan halaman web dimunculkan secara dinamis mampu dalam mengatur permintaan user, mengatur hak akses terhadap informasi tertentu contoh server side scripting: PHP
Web Server Web Client 2. Interpret/compile file hello.php, hasil compile akan berformat html
3. HTTP Respone
<html> <head> <title>Hello World !</title> </head> <body> <h1>Hello World !</h1> </body> </html>
Cara kerja mirip dengan HTML Static, namun dengan tambahan yaitu file yang direquest akan di compile terlebih dahulu baru kemudian dikirimkan ke server.
1.7 Contoh Aplikasi Content sites (both production and display) Community features (forums, bulletin boards, and so on) E-mail (Web mail, mail forwarding, and sending mail from a Web application) Customer-support and technical-support systems Advertising networks Web-delivered business applications Directories and membership rolls Surveys, polls, and tests Filling out and submitting forms online Personalization technologies Groupware Catalog, brochure, and informational sites Games (for example, chess) with lots of logic but simple/static graphics Any other application that needs to connect a backend server (database, LDAP, and so on) to a Web server Social Network
1.8 Kesimpulan Aplikasi berbasis web dapat bekerja dalam arsitektur jaringan client server baik 2-tier, 3tier ataupun n-tier. Aplikasi web dibangun dengan memanfaatkan client side technology ataupun server side scripting/programming. Server side scripting/programming memungkinkan halaman web berubah secara dinamis. Ada banyak sekali aplikasi yang dapat dibangun memanfatkan server side scripting/programming namun aplikasi yang dibangun masih terbatas pada aplikasi yang bukan realtime atau aplikasi yang memerlukan manipulasi image/grafis tinggi.
EVALUASI 1. 2. 3. 4. 5. Jelaskan pemahaman anda mengenai arsitektur aplikasi Web ! Jelaskan perbedaan antara static Web dan dynamic Web ditinjau dari cara kerjanya ! Jelaskan pengertian Client Side Technology dan berikan contoh-contohnya ! Jelaskan pengertian Server Side Scripting dan berikan contoh-contohnya ! Berikan contoh aplikasi web yang anda ketahui, ceritakan secara umum isi atau fungsi dari web tersebut !
URAIAN MATERI 2.1 Dasar Pembuatan Program Web dengan PHP Membuat halaman web Ada beberapa cara untuk membuat sebuah halaman web dengan php yaitu dengan menggunakan : Canonical php tags <?php ?> Short-open (SGML-style) tags <? ?> ASP-style tags <% %> HTML script tags <SCRIPT LANGUAGE=PHP> </SCRIPT> Instruction Separation Untuk memisahkan antara perintah yang satu dengan perintah lainnya PHP menggunakan tanda titik koma (;). Komentar Untuk menulis komentar di PHP dapat menggunakan beberapa cara yaitu : // komentar Komentar 1 baris bergaya C # komentar Komentar 1 baris bergaya unix shell /* komentar */ Komentar lebih dari 1 baris bergaya C Escaping Character
Escaping character merupakan karakter yang akan diperlakukan special oleh php. Dapat diperlakukan mirip seperti kontrol seperti control ganti baris, tab ataupun dapat juga seperti karakter biasa seperti kutip satu (), kutip dua () dll.
Escape sequence \ \ \\ \$ \n \r \t
Meaning
Kutip satu Kutip dua Backslash Dollar sign Newline Carriage Return Tab
PHP Variables o Gunakan karakter $ sebagai penanda nama variabel. o Variabel tidak perlu di deklarasikan atau tidak harus diinisialisasi terlebih dahulu. o Inisialisasi variabel dengan cara memasukkan nilai ke variabel tersebut. Contoh: $nama = Budi; Interacting with the user o Dapat menggunakan HTML form o Menggunakan method get atau post o Form element yang telah diinputkan user dapat diakses melalui super global variabel $_POST[] atau $_GET[] atau $_REQUEST[]; o Untuk memproses form dapat menggunakan php page yang berbeda.
2.2 Variabel, Konstanta dan Tipe Data Tipe Data integer, double, string, arrays and object,boolean integer 0 (zero) = false, non zero = true
double 0.0 = false, non zero = true string = false, non empty string = true True / False Identifiers Identifier case-sensitive, built-in functions and structures not case-sensitive. Contoh: $nama dan $Nama adalah variabel yang berbeda. Konstanta Gunakan fungsi define() Contoh: define(PRODI,Manajemen Informatika); define(PHI, 3.14); Gunakan fungsi defined() untuk mengecek sebuah konstanta ada atau tidak. Hasil fungsi 1 jika ada konstanta dan 0 jika tidak ada. Ada predefined konstanta dalam PHP, contohnya: __FILE__, __LINE__, PHP_VERSION, PHP_OS Type juggling akan dilakukan konversi tipe data secara otomatis. contoh: $a = 1; // variabel $a bertipe integer $a = 1.1; // sekarang variabel $a bertipe double $a = A; // sekarang variabel $a bertipe String String conversion melakukan operasi numerik terhadap string. contoh: $str = 5 jurus mudah belajar PHP; $x = 10 + $str; // variabel $x akan bernilai integer 15. Aturan string conversion: Hanya yang diawal yang akan dievaluasi sebagai angka. String 5th akan dievaluasi sebagai 5. String the 5th element akan dievaluasi sebagai 0. String akan dievaluasi sebagai double jika keseluruhan string mencerminkan double. String 10.2 akan dievaluasi sebagai double 10.2, namun 10.2 centimeter akan dievaluasi sebagai integer 10 bukan double 10.2. Type casting Konversi tipe data secara explisit. o (int), (integer) - cast to integer o (bool), (boolean) - cast to boolean o (float), (double), (real) - cast to float o (string) - cast to string
o (binary) - cast to binary string (PHP 6) o (array) - cast to array o (object) - cast to object Contoh: $a = 16.7; $b = (int) $a; // $b bernilai 16, integer $c = (double) %b; // $c bernilai 16.0, double $d = (string) $c; // $d bernilai 16.0, string Fungsi-fungsi untuk variabel. o gettype(), menentukan tipe data variabel $name = Budi; $tipe_nama = gettype($nama); // return string o settype(), megubah tipe data variabel $a = 5th; settype($a,integer); //$a akan bertipe integer dengan nilai 5 o isset(), mengecek variabel memiliki nilai atau tidak. o unset(), membebaskan memori yang dipakai variabel o empty(), mengecek variabel bernilai 0 atau empty string. o is_(), mengecek variabel apakah bertipe data tertentu. o is_int($a); o val(), mengubah tipe data variabel. $a = 5th; $x = intval($a);
2.3 Operator Aritmetic Operator (+, -, *, /, %) Unary Operator (-) Comparison Operator (==, <, >, <=, >=, !=, <>) Logical Operator (&&, ||, and, or, xor, !) String concatenation operator (.) Bitwise operator (&, |, ^, >>, <<, ~) Ternary operator ( test-expression ? yes-expression : no-expression) Variable Assignment shortcuts (+=, -=, *=, /=, %=, &=, |=, ^=, .=, >>=, <<=, ++, --)
for exit
2.5 Functions Definition function function-name ($argument-1, $argument-2, ..) { statement-1; statement-2; ... } Contoh: function printError($err) { echo <h4 style:\color:red\>$err</h4>; } function luasLingkaran($r) { $luas = 3.14 * r * r; return $luas; } Variabel scope and life time function print_x() { static $x=0; $x++; echo $x; } print_x(); print_x();
$x = 10; function print_x() { $x = 15; } print_x(); echo $x; ================================== $x = 10; function print_x() { global $x; $x = 15; } print_x();
echo $x;
Nested function and recursion function faktorial($x) { if ($x>1) { return $x * faktorial($x-1); } else { return 1; } }
function X($x) { function Y($x) { return $x * $x; } return Y($x); } echo X(2);
Including Files in PHP Pages Gunakan statement require, require_once, include, include_once; Contoh: //file common.php function printErr($errmsg) { echo "<h4 style=\"color:red\">Error: $errmsg</h4>"; } //file coba.php require common.php; if ($x==0) { printErr(\$x = 0);
2.6 Array Initializing Arrays // 3 elements, index beraturan $nim[] = 0320070001; $nim[] = 0320070002; $nim[] = 0320070003; ================================== $nim[0] = 0320070001; $nim[1] = 0320070002; $nim[2] = 0320070003; // 3 elements, index tak beraturan $nim[9] = 0320070001; $nim[14] = 0320070002; $nim[2] = 0320070003; // 4 elemnts, with last element have index 15 $nim[9] = 0320070001; $nim[14] = 0320070002; $nim[2] = 0320070003; $nim[] = 0320070004;
Initializing Arrays with array construct $nim = array(0320070001,0320070002,0320070003); $nim = array(1 => 0320070001,2 => 0320070002,3 => 0320070003); $nim = array( 0320070001,9 => 0320070002,0320070003);
Looping Through an Array // sequential indexed array $nim = array(0320070001,0320070002,0320070003); $jumlah_nim = count($nim); for ($i=0; $i < $jumlah_nim; $i++) { echo $nim[$i]; } //Atau foreach ($arr as $value) { echo "Value: $value<br />\n"; }
$nim = array(50 => 0320070001, 7=> 0320070002, 11 => 0320070003); reset($nim); while(list($key, $value) = each($nim)) { echo $value; } //Atau foreach ($arr as $key => $value) { echo $key => $value<br />\n"; } String indexed array // sequential indexed array $students = array(0320070031 => Indah Rahayu,0320070002 => Isnain Setiyanti,0320070003 => Fajar Pambudi); reset($students); $student = each($students); $student akan terdiri dari 4 element array. 1. Element pertama memiliki index 0 dan bernilai 0320070031 2. Element ke-2 memiliki index 1 dan bernilai Indah Rahayu 3. Element ke-3 memiliki index key dan bernilai 0320070031 4. Element ke-4 memiliki index value dan bernilai Indah Rahayu Array Multi Dimensi $students3B = array("student1" => array("0320070031" => "Indah Rahayu"), "student2" => array("0320070002" => "Isnain Setiyanti"), "student3" => array("0320070003" => "Fajar Pambudi")); reset($students3B); while (list($studentkey, $studentvalue) = each($students3B)) { while (list($nim, $nama) = each($students3B[$studentkey])) { echo $nim . " " . $nama . "<br>"; } }
2.7 File Handling Membuka file resource fopen ( string filename, string mode ) Note: If the fopen() function is unable to open the specified file, it returns 0 (false). Value r Decsription Read only. Starts at the beginning of the file
r+ w
Read/Write. Starts at the beginning of the file Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist Append. Opens and writes to the end of the file or creates a new file if it doesn't exist Read/Append. Preserves file content by writing to the end of the file Write only. Creates a new file. Returns FALSE and an error if file already exists Read/Write. Creates a new file. Returns FALSE and an error if file already exists
w+
a+
x+
Menutup file bool fclose ( resource handle ) Reading Files // read up to length bytes string fread ( resource handle, int length ) // character by character string fgetc ( resource handle ); //line by line string fgets ( resource handle [, int length] ) //identical to fgets except any HTML and PHP tags are stripped string fgetss ( resource handle [, int length [, string allowable_tags]] )
Writing to Files // writes the contents of string to the file int fwrite ( resource handle, string string [, int length] )
int fputs ( resource handle, string string [, int length] ) //line by line string fgets ( resource handle [, int length] ) //identical to fgets except any HTML and PHP tags are stripped string fgetss ( resource handle [, int length [, string allowable_tags]] )
Navigating Within Files // resets the current pointer to the beginning of the files bool fwrite ( resource handle ) // tells the file pointer read/write position int ftell ( resource handle ) // move to a spesific position int fseek ( resource handle, int offset [, int whence] ) whence values are: SEEK_SET - Set position equal to offset bytes. SEEK_CUR - Set position to current location plus offset. SEEK_END - Set position to end-of-file plus offset. //Tests for end-of-file on a file pointer. bool feof ( resource handle )
Copying, Deleting and Renaming Files // copy file bool copy(string source, string destination); // delete file bool unlink(string filename); // rename file bool rename (string oldname, string newname);
Other functions //Check file or directory exist or not bool file_exists ( string filename ); // get the size for given file int filesize ( string filename )
2.8 Kesimpulan Dasar pemrograman web menggunakaan PHP meliputi tag yang dipergunakan untuk membuat file PHP, bagaimana menuliskan perintah, dan cara bagaimana program dapat berinteraksi dengan user. Sama seperti bahasa pemrograman lainnya PHP juga mengenal tipe data, variable, konstanta, operasi percabangan dan operasi perulangan, selain itu juga ada fungsi yang biasa dipakai dalam pemrograman modular.
EVALUASI
1. 2. 3. 4. 5. 6. 7.
Terangkan apa saja yang harus diketahui agar dapat membuat program web dengan PHP ! Apa saja jenis variable yang ada di PHP ? Sebutkan operator-operator aritmatika yang ada di PHP ! Apa yang anda ketahui tentang nested if, jelaskan dengan bahasa sendiri ! Bagaimana cara untuk mendeklarasikan fungsi di PHP ? Buatlah sebuah array 2 dimensi ! Jelaskan kegunaan dari fungsi-fungsi berikut : - fopen - fwrite - freads - fgetss