KUmPulan PHP Scrip

31
Untuk Memebuat Form Akses Sintaknya adalah <html> <head> <title>Quick Direct!!!</title> </head> <body bgcolor="white"> <center>Free Add Links </center> <hr> <FORM METHOD="POST" ACTION="/cgi-bin/links.cgi"> <PRE> Website URL <INPUT type=text size=50 maxlength=70 name="url"> Website Title <INPUT type=text size=50 maxlength=70 name="name"> <br></br> <INPUT type="submit" value="Add URL"> <INPUT type="reset" value="clear form"> </PRE> </FORM> <hr> <!--Begin Directory--> br><li><a href="http://webit.ml.org/scripts/">WEB-IT CGI Scripts Archive</a> <hr> <center><a href="http://webit.ml.org/scripts/">WEB-IT CGI Scripts Archive</a></center> </body> </html> Untuk Memebuat Form Login <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859- 1"> <META NAME="GENERATOR" CONTENT="Mozilla/4.01 [en] (Win95; I) [Netscape]"> <TITLE>Document Doctor</TITLE> </HEAD> <FORM method=post action="login_setup.cgi"> <center><h1>Database Doctor</h1> This page should probably be somewhere password protected...either that or change the name and hide it... <table border=0> <tr><td>

Transcript of KUmPulan PHP Scrip

Page 1: KUmPulan PHP Scrip

Untuk Memebuat Form Akses Sintaknya adalah

<html> <head> <title>Quick Direct!!!</title> </head> <body bgcolor="white"> <center>Free Add Links </center> <hr> <FORM METHOD="POST" ACTION="/cgi-bin/links.cgi"> <PRE> Website URL <INPUT type=text size=50 maxlength=70 name="url"> Website Title <INPUT type=text size=50 maxlength=70 name="name"> <br></br> <INPUT type="submit" value="Add URL"> <INPUT type="reset" value="clear form"> </PRE> </FORM> <hr> <!--Begin Directory--> br><li><a href="http://webit.ml.org/scripts/">WEB-IT CGI Scripts Archive</a> <hr> <center><a href="http://webit.ml.org/scripts/">WEB-IT CGI Scripts Archive</a></center> </body> </html>

Untuk Memebuat Form Login

<HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <META NAME="GENERATOR" CONTENT="Mozilla/4.01 [en] (Win95; I) [Netscape]"> <TITLE>Document Doctor</TITLE> </HEAD> <FORM method=post action="login_setup.cgi"> <center><h1>Database Doctor</h1> This page should probably be somewhere password protected...either that or change the name and hide it... <table border=0> <tr><td>

Page 2: KUmPulan PHP Scrip

New Users Name</td><td><INPUT type=text name="new_user" size=20></td></tr> <tr><td> Password</td><td><INPUT type="text" name="new_pass" size=20></td></tr></table> <p> <INPUT type="Submit" name="pass" Value=" Set the New Users Login "> </FORM> </BODY> </HTML>

Membuat Form Pemesanan Barang <! Sample Order Form Template > <! Comments start with a bracket and exclamation point > <! Please read all the comments in the form before making any > <! Modifications to the form. And backup the form before making > <! any changes so you will always have a working copy!! > <! Modifications are not difficult, but when working with html > <! strange things happen when you delete tags that should not > <! be deleted. So please follow the instructions carefully. > <! Change Merchant's Order Form to your business name below. > <HTML> <HEAD> <TITLE>Order Form</TITLE> </HEAD> <BODY BGCOLOR=FFFFFF TEXT=000000 LINK=0000FF VLINK=800080> <CENTER> <TABLE WIDTH="600"> <TR> <TD ALIGN="CENTER"><FONT FACE="Arial" size=5><B>Merchant's Order Form</FONT></TD> </TR> </TABLE><BR> <! This block of code gives instructions for your customers who > <! want to pay by check or money order. Replace the Merchant Name > <! with your company name and replace the address lines 1 and 2. > <! If you do not accept checks or money orders you can delete the > <! whole block of code. > <! Start Check/Money order code > <TABLE WIDTH="500"> <TR> <TD> If you are paying by check or money order mail to:</FONT><BR><BR> <FONT FACE="Arial" SIZE="-1"></FONT><FONT FACE="Arial" SIZE="-1">Merchant Name

Page 3: KUmPulan PHP Scrip

</FONT><BR> <FONT FACE="Arial" SIZE="-1">Address 1</FONT><BR> <FONT FACE="Arial" SIZE="-1">Address 2</FONT><BR> </TR></TD></TABLE><BR> <! End Check/Money order code > <! Modify the action statement in the form tag below > <! to point to eaccountant.cgi on your system. Do not > <! forget to leave ?process_order on the end. > <! Change [email protected] to your email address. > <! Change the subject of the email. > <! Do NOT remove the HIDDEN form field. > <TABLE WIDTH="500" BGCOLOR="#EBEBEB"> <TR> <TD> <form name="order" action="/cgi-bin/yourdirectory/eaccountant.cgi?process_order" METHOD="POST"> <INPUT TYPE=HIDDEN name="recipient" value="[email protected]"> <INPUT TYPE="HIDDEN" name="subject" value="Subject of Email"> <INPUT TYPE="HIDDEN" name="customerorder" value="yes"> <! This is a nested table for the grey section of the form > <TABLE WIDTH="100%"> <TR> <TD WIDTH="494" COLSPAN="2" ALIGN="CENTER" BGCOLOR="#FCCA03"> <FONT FACE="Arial" COLOR="#FF0000"><B>CustomerInformation</B></FONT> </TD> </TR> <! A blank row > <TR> <TD><BR><BR> </TD></TR> <! Row for first name with 2 columns, one for the description of > <! the item, the other for the input box. > <! You shouldn't have to modify anything in this section. > <TR> <TD WIDTH="178" ALIGN="RIGHT"><FONT FACE="Arial" SIZE="-1">First Name:</FONT></TD> <TD WIDTH="316"> <P><INPUT TYPE="TEXT" NAME="fname" SIZE="30"></P></TD> </TR> <! Row for last name with 2 columns >

Page 4: KUmPulan PHP Scrip

<TR> <TD WIDTH="178" ALIGN="RIGHT"><FONT FACE="Arial" SIZE="-1">Last Name:</FONT></TD> <TD WIDTH="316"> <P><INPUT TYPE="TEXT" NAME="name" SIZE="30"></P></TD> </TR> <! Row for address with 2 columns > <TR> <TD WIDTH="178" ALIGN="RIGHT"><FONT FACE="Arial" SIZE="-1">Address:</FONT></TD> <TD WIDTH="316"> <P><INPUT TYPE="TEXT" NAME="address1" SIZE="30"></P></TD> </TR> <! Row for 2nd address box with 2 columns > <TR> <TD WIDTH="178" ALIGN="RIGHT"><FONT FACE="Arial" SIZE="-1">Address:</FONT></TD> <TD WIDTH="316"> <P><INPUT TYPE="TEXT" NAME="address2" SIZE="30"></P></TD> </TR> <! Row for city with 2 columns > <TR> <TD WIDTH="178" ALIGN="RIGHT"><FONT FACE="Arial" SIZE="-1">City:</FONT></TD> <TD WIDTH="316"> <P><INPUT TYPE="TEXT" NAME="city" SIZE="30"></P></TD> </TR> <! Row for state with 2 columns > <TR> <TD WIDTH="178" ALIGN="RIGHT"><FONT FACE="Arial" SIZE="-1">State/Province:</FONT></TD> <TD WIDTH="316"> <P><INPUT TYPE="TEXT" NAME="state" SIZE="30"></P></TD> </TR> <! Row for country with 2 columns > <TR> <TD WIDTH="178" ALIGN="RIGHT"><FONT FACE="Arial" SIZE="-1">Country:</FONT></TD> <TD WIDTH="316"> <P><INPUT TYPE="TEXT" NAME="country" SIZE="30"></P></TD> </TR> <! Row for zip with 2 columns > <TR> <TD WIDTH="178" ALIGN="RIGHT"><FONT FACE="Arial" SIZE="-1">Zip Code:</FONT></TD> <TD WIDTH="316">

Page 5: KUmPulan PHP Scrip

<P><INPUT TYPE="TEXT" NAME="zip" SIZE="30"></P></TD> </TR> <! Row for phone number with 2 columns > <TR> <TD WIDTH="178" ALIGN="RIGHT"><FONT FACE="Arial" SIZE="-1">Telephone:</FONT></TD> <TD WIDTH="316"> <P><INPUT TYPE="TEXT" NAME="phone" SIZE="30"></P></TD> </TR> <! Row for email address with 2 columns > <TR> <TD WIDTH="178" ALIGN="RIGHT"><FONT FACE="Arial" SIZE="-1">Email:</FONT></TD> <TD WIDTH="316"> <P><INPUT TYPE="TEXT" NAME="email" SIZE="30"></P></TD> </TR> <! Blank row > <TR><TD><BR><BR></TD></TR> <! Start of Order Information section. > <! Here's where you will need to make changes. > <TR> <TD WIDTH="700" COLSPAN="2" ALIGN="CENTER" BGCOLOR="#FCCA03"> <FONT FACE="Arial" COLOR="#FF0000"><B>Order Information</B></FONT></TD> </TR> <TR><TD><BR><BR></TD></TR> <TR> <TD WIDTH="700" COLSPAN="2"><CENTER> <! Nested table for the item radio boxes and quantity input boxes. > <! To modify an entry replace the name=" " with the name of your > <! product. Make sure it matches a product you have entered into > <! e-Accountant exactly. Also change the value=" " to the same > <! as the name=" ", and add your description of the product. > <! To delete a product, delete the entire block of code. > <! To add an entry just copy and paste one of the existing items > <! being careful to copy the entire block of code as above, > <! including the TRs and TDs. > <! > <! Be sure that when you are finished adding or deleting entries > <! that your quanities are still in consecutive order, with qty1 > <! the first and so on. >

Page 6: KUmPulan PHP Scrip

<TABLE WIDTH=80% BORDER=3> <! Code for product 1 > <TR><CENTER> <TD WIDTH=95%><input type=radio name="AnsurSubmit" value="AnsurSubmit"><B>AnsurSubmit for $79</TD> <TD WIDTH=5% ALIGN=center><input type=text name="qty1" size=3><B>Qty </TD></TR> <! End code for product 1 > <! Code for product 2 > <TR> <TD WIDTH=95%><input type=radio name="AnsurDeep" value="AnsurDeep"><B>Ansur Deep Submit for $129</TD> <TD WIDTH=5% ALIGN=center><input type=text name="qty2" size=3><B>Qty </TD></TR> <! End code for product 2 > <! Code for product 3 > <TR> <TD WIDTH=95%><input type=radio name="Doorway Generator" value="Doorway Generator"><B>Doorway Generator for $29</TD> <TD WIDTH=5% ALIGN=center><input type=text name="qty3" size=3><B>Qty </TD></TR> <! End code for product 3 > <! Code for product 4 > <TR> <TD WIDTH=95%><input type=radio name="AnsurBlast" value="AnsurBlast"><B>AnsurBlast 6000 for $99</TD> <TD WIDTH=5% ALIGN=center><input type=text name="qty4" size=3><B>Qty </TD></TR> <! End code for product 4 > <! Code for product 5 > <TR> <TD WIDTH=95%><input type=radio name="AnsurSustain" value="AnsurSustain"><B>AnsurSustain monthly submission plan for $79 per month</TD> <TD WIDTH=5% ALIGN=center><input type=text name="qty5" size=3><B>Qty </TD></TR> <! End code for product 5 > <! If you do not collect sales tax you can delete this > <! block of code. If you collect sales tax just modify the state > <! and the tax amount. > <! Start sales tax code> <TR> <TD WIDTH=95% ALIGN="right"><B>Tax if in TX @ 7.25%</TD>

Page 7: KUmPulan PHP Scrip

<TD WIDTH=5% ALIGN=center>$<input type=text name="salestax" size=5><B>Tax</TD></TR> <! End sales tax code> <! If you do not ship products or do not charge for shipping you > <! can delete this block of code. If you charge for shipping, modify > <! the shipping methods and prices in the two sections below. > <! Remember that your shipping methods must match what you have > <! entered into e-Accountant exactly. > <! Start shipping code > <TR><TD WIDTH=75%><B>UPS Second Day $5<BR> <B>Airborne $10<BR> <B>Overnight $25<BR> <B>Federal Express $25<BR></TD> <TD> <SELECT NAME="shipping" SIZE=4> <OPTION><B>UPS Second Day <OPTION><B>Airborne <OPTION><B>Overnight <OPTION><B>Federal Express </SELECT> </TD></TR> <TR> <TD WIDTH=95% ALIGN="right"><B>Shipping Charges</TD> <TD WIDTH=5% ALIGN=center>$<input type=text name="shipcost" size=5></TR></TD> <! End Shipping code > </CENTER></TABLE></B> <TR><TD><BR><BR></TD></TR> <! Start of credit card information code. Modify the values as needed. > <! We have blocked off the code for each type of card so you can > <! delete code if necessary. > <TR> <TD WIDTH="700" COLSPAN="2" ALIGN="CENTER" BGCOLOR="#FCCA03"><FONT FACE="Arial" COLOR="#FF0000"><B>Credit Card Information</B></FONT></TD> </TR> <TR><TD><BR><BR></TD></TR> <TR> <TD WIDTH="178"><FONT FACE="Arial" SIZE="-1">Please choose one:</FONT></TD> <TD WIDTH="316">

Page 8: KUmPulan PHP Scrip

<TABLE WIDTH="100%"> <! Start Visa > <TR> <TD WIDTH="28"> <P><INPUT TYPE="RADIO" NAME="cardtype" VALUE="Visa" CHECKED="CHECKED"></P></TD> <TD WIDTH="46"><FONT FACE="Arial" SIZE="-1">Visa</FONT></TD> <TD WIDTH="25"> <! Start Mastercard > <P><INPUT TYPE="RADIO" NAME="cardtype" VALUE="Mastercard"></P></TD> <TD WIDTH="105"><FONT FACE="Arial" SIZE="-1">Master Card</FONT></TD> <TD WIDTH="26"> <! Start Amex > <P><INPUT TYPE="RADIO" NAME="cardtype" VALUE="American Express"></P></TD> <TD WIDTH="76"><FONT FACE="Arial" SIZE="-1">Amex</FONT></TD> <! End Amex > </TR> </TABLE></TD> </TR> <! Start collection of credit card information. You probably do > <! not need to modify this. > <TR> <TD WIDTH="178"><FONT FACE="Arial" SIZE="-1">Credit Card Number:</FONT></TD> <TD WIDTH="316"><FONT FACE="Arial" SIZE="-1"> Please leave a space between grouped numbers</FONT></TD> </TR> <TR> <TD WIDTH="178"></TD> <TD WIDTH="316"> <P><INPUT TYPE="TEXT" NAME="cardnumber" SIZE="40"></P></TD> </TR> <TR> <TD WIDTH="178"><FONT FACE="Arial" SIZE="-1">Exp. Date:</FONT></TD> <TD WIDTH="316"><FONT FACE="Arial" SIZE="-1">Example 0599 = May 1999</FONT>.</TD> </TR> <TR> <TD WIDTH="178"></TD> <TD WIDTH="316">

Page 9: KUmPulan PHP Scrip

<P><INPUT TYPE="TEXT" NAME="exp"></P></TD> </TR> <TR> <TD WIDTH="178"><FONT FACE="Arial" SIZE="-1">Cardholder:</FONT></TD> <TD WIDTH="316"><FONT FACE="Arial" SIZE="-1">Exact name on the Credit Card:</FONT></TD> </TR> <TR> <TD WIDTH="178"></TD> <TD WIDTH="316"> <P><INPUT TYPE="TEXT" NAME="fullname" SIZE="30"></P></TD> </TR> <TR> <TD WIDTH="178"></TD> <TD WIDTH="316"> <BR><BR> <P><CENTER><INPUT TYPE="SUBMIT" NAME="Submit1" VALUE="Submit"> <INPUT TYPE="RESET" NAME="Reset1"></P></CENTER></TD> </TR> <! Start display of your business information. Fill in your company > <! name, address and phone number here. > <TR> <TD WIDTH="178"></TD> <TD WIDTH="316"><HR WIDTH=450> <CENTER><FONT FACE="Arial" SIZE="-1"><B>Merchant Name<BR> <B>Address 1<BR> <B>Address 2<BR> <B>Phone Number</FONT></CENTER></TD> </TR> </TABLE></FORM></TD> </TR> </TABLE> </CENTER> </BODY> </HTML>

Prosesor.PHP <?php # FILE: processors.php # DATE: 11/27/02 # AUTHOR: ShaunC "Bulworth" # PROJECT: CC Processor Gateway Interface

Page 10: KUmPulan PHP Scrip

# COPYRIGHT: PHPLabs.Com # ---- # Description: This file contains the function used to post # transactions 2Checkout. ################################################################ # You MUST set the following variable before using the script # ################################################################ # $merchant2cuser is your merchant ID for 2Checkout. $merchant2cuser = "test"; ################################################################ # End of configuration - No need to edit below this line # ################################################################ #Process a transaction via 2Checkout #Returns: None. Transaction status will be posted to #2checkout_ipn.php by 2Checkout. function twocheckout($amount, $invoice=0){ global $_SERVER, $merchant2cuser; $target = "https://www.2checkout.com/cgi-bin/sbuyers/cartpurchase.2c?"; $amount = sprintf("%.02f", $amount); if(!$invoice) $invoice = sprintf("%06d", rand(111111, 999999)); $params = "sid=$merchant2cuser&total=$amount&cart_order_id=$invoice"; $params = str_replace(" ", "%20", $params); header("Location: $target$params"); } ?>

Page 11: KUmPulan PHP Scrip

Menangkap Variabel Post database create database db_gb ; use db_gb; create table guestbook (id int unsigned not null primary key auto_increment, nama varchar(50) not null, email varchar(100) not null, pesan text not null); file koneksi.php <?php $host="localhost"; $uname="root"; $pass=""; $db="db_gb"; $koneksi=mysql_connect($host,$uname,$pass); mysql_select_db($db,$koneksi); ?> file lihatgb.php <?php $queryGb = mysql_query("select * from guestbook"); $jmlGb = mysql_num_rows($queryGb); if($jmlGb==0) { echo"Data buku tamu masih kosong"; } else { $queryTampil=mysql_query("select * from guestbook order by id desc"); echo"<p>DATA BUKU TAMU<p>"; while($dataGb=mysql_fetch_row($queryTampil)) { echo"<b>$dataGb[1]</b><br>"; echo"".wordwrap($dataGb[3],85,"<br>",1)." "; echo"<hr width=50% align=left>"; } } ?> file gb.php <?php include'koneksi.php'; if(isset($_POST['kirim']) && ($_POST['kirim']=='Kirim')) { $nama=trim(strip_tags($_POST['nama'])); $email=trim(strip_tags($_POST['email'])); $pesan=trim(strip_tags($_POST['pesan']));

Page 12: KUmPulan PHP Scrip

//validasi form if($nama=='') $error1= "<font color=red>&nbsp;&nbsp;&laquo; Nama belum diisi</font><br>"; if($email=='') $error2= "<font color=red>&nbsp;&nbsp;&laquo; Email belum diisi</font><br>"; if($pesan=='') $error3= "<font color=red>&nbsp;&nbsp;&laquo; Pesan belum diisi</font><br>"; if($email!='') { $polaemail = "^.+@.+\..+$"; //nyontek tutorial validasi email yg ditulis b_scorpio :) $cek=eregi($polaemail,$email); if(!$cek) { $error4="<font color=red>&nbsp;&nbsp;&laquo; Pola email salah</font>"; } } //jika semua data valid if(($nama!='') && ($email!='') && $cek && ($pesan!='')) { //simpan data buku tamu $queryAddGb=mysql_query("insert into guestbook values(NULL,'$nama','$email','$pesan')"); if($queryAddGb) //jika query simpan buku tamu berhasil { //tampilkan pesan berhasil echo"<font color=red><b>Guestbook berhasil disimpan</b></font>"; echo"<meta http-equiv=\"refresh\" content=\"2; url=./gb.php\">"; exit; } else //jika query gagal { //tampilkan pesan gagal echo"<font color=red><b>Data gagal disimpan</b></font>"; echo"<meta http-equiv=\"refresh\" content=\"10; url=./gb.html\">"; exit; } } if(isset($error1) || isset($error2) || isset($error3) || isset($error4))

Page 13: KUmPulan PHP Scrip

{ //tangkap variabel post untuk menampilkan kembali data yang sudah diisi pada form $ex_nama=trim(strip_tags($_POST['nama'])); $ex_email=trim(strip_tags($_POST['email'])); $ex_pesan=trim(strip_tags($_POST['pesan'])); } } include'lihatgb.php'; //semua value variabel diset kosong jika belum diisi if(empty($ex_nama)) { $ex_nama=''; } if(empty($ex_email)) { $ex_email=''; } if(empty($ex_pesan)) { $ex_pesan=''; } ?> <p><br><br>FORM BUKU TAMU <form method="post" action=""> Nama *<br><input type="text" name="nama" size="34" value="<?php echo $ex_nama;?>"><?php if(isset($error1)) //jika data nama kosong { echo $error1; //tampilkan pesan di samping textbox nama } ?><br> Email *<br><input type="text" name="email" size="34" value="<?php echo $ex_email;?>"><?php if(isset($error2)) //jika data email kosong { echo $error2; //tampilkan pesan d isamping textbox email } if(isset($error4)) //jika data email tidak sesuai dengan pola email { echo $error4; //tampilkan pesan di samping textbox email } ?><br>

Page 14: KUmPulan PHP Scrip

Pesan *<br><textarea name="pesan" rows="8" cols="40"><?php echo $ex_pesan;?></textarea><?php if(isset($error3)) //jika data pesan kosong { echo $error3; //tampilkan pesan di samping textarea pesan } ?> <br>*) Wajib diisi <br> <input type="reset" value="Reset" name="kosongkan"> <input type="submit" value="Kirim" name="kirim"> </form> PHP Looping 26-09-2007 | 15:29:43 | Halaman Ini dibaca 7531 kali PHP Looping 1. While Loops syntax untuk while loop: while (expression) { code to execute; } contoh while loop yang menghitung sampai 10 script------------- <?php $num = 1; while ($num <= 10){ print "Number is $num<br />\n"; $num++; } print 'Done.'; ?> script------------- menghasilkan:

Page 15: KUmPulan PHP Scrip

Number is 1 Number is 2 Number is 3 Number is 4 Number is 5 Number is 6 Number is 7 Number is 8 Number is 9 Number is 10 Done. script nya akan me-loop sampai 10 dan script $num++ bertambah 1 2. For Loops syntax untuk For Loops adalah for (initialization expression; test expression; modification expression){ code that is executed; } contoh script untuk for loop: script------------- <?php for ($num = 1; $num <= 10; $num++) { print "Number is $num<br />\n"; } ?> script------------- menghasilkan: Number is 1 Number is 2 Number is 3 Number is 4 Number is 5 Number is 6 Number is 7 Number is 8

Page 16: KUmPulan PHP Scrip

Number is 9 Number is 10 3. Menghentikan sebuah Loop contoh Menghentikan sebuah Loop untuk menghindari Loop: script------------- <?php $counter = -3; for (; $counter < 10; $counter++){ // Check for division by zero if ($counter == 0){ echo "Stopping to avoid zero."; break; } echo "100/$counter<br />"; } ?> script------------- menghasilkan: 100/-3 100/-2 100/-1 Stopping to avoid division by zero. *dia akan loop sampai fungsi if dan karena ada script break, maka proses akan berhenti *script ini suatu saat akan berguna "Trust me" 4. continue Statements penggunaan continue selain break script------------- <?php

Page 17: KUmPulan PHP Scrip

$counter =- 3; for (; $counter < 10; $counter++){ // Check for division by zero if ($counter == 0){ echo "Skipping to avoid zero.<br />"; continue; } echo "100/$counter<br />"; } ?> script------------- menghasilkan: 100/-3 100/-2 100/-1 Skipping to avoid zero. 100/1 100/2 100/3 100/4 100/5 100/6 100/7 100/8 100/9 *dia akan loop sampai fungsi if dan karena ada script continue, maka proses akan berhenti mengganti angka 0 menjadi Skipping to avoid zero, lalu looping akan di lanjutkan kembali Membahas Fungsi include - include_once - require - include() - include_once() - require() Fugnsi di atas berguna untuk menggabungkan file PHP atau bukan PHP kedalam file PHP +++ include()

Page 18: KUmPulan PHP Scrip

sebagai contoh head.php ----- <?php echo "selamat datang di website ku<br>"; ?> ----- body.php ----- <?php include 'head.php'; echo "<br>Ini adalah contant atau isi dari website ku<br>"; ?> ----- hasilnya: selamat datang di website ku Ini adalah contant atau isi dari website ku +++ include_once() "include_once()" berfungsi untuk membatasi fungsi include cuma untuk satu kali saja contoh jika tidak menggunakan "include_once()": body.php ----- <?php include 'head.php'; include 'head.php'; include 'head.php'; echo "<br>Ini adalah content atau isi dari website ku<br>"; ?> ----- hasilnya: selamat datang di website ku selamat datang di website ku selamat datang di website ku Ini adalah content atau isi dari website ku contoh jika menggunakan "include_once()": body.php ----- <?php include_once 'head.php'; include_once 'head.php'; include_once 'head.php'; echo "<br>Ini adalah contant atau isi dari website ku<br>"; ?>

Page 19: KUmPulan PHP Scrip

----- hasilnya: selamat datang di website ku Ini adalah content atau isi dari website ku +++ require() fungsi ini tidak jauh beda dengan fungsi "include()" alias sama, jadi kita loncati saja fungsi ini Sekarang untuk sisi security/keamanan dari fungsi include() kenapa?? ya.. karena fungsi ini paling banyak di manfaatkan hacker .... 1. include hanya file yang berasal dari server anda saja... caranya: body.php ------ <?php include($_SERVER['DOCUMENT_ROOT'].'/head.php'); ?> ------ jika file head.php ada di root atau tidak dalam folder body.php ------ <?php include_once($_SERVER['DOCUMENT_ROOT'].'/head.php'); ?> ------ body.php ------ <?php require_once($_SERVER['DOCUMENT_ROOT'].'/head.php'); ?> ------ Untuk melihat dimana posisi file php anda, anda bisa gunakan: <?php echo $_SERVER['DOCUMENT_ROOT']; ?> !!! PENTING !!! Jika "$_SERVER['DOCUMENT_ROOT']" tidak bisa jalan di server anda dikarenakan

Page 20: KUmPulan PHP Scrip

restrict dari penyedia jasa hosting, anda tetap boleh menggunakan "include 'head.php';" !!! KESALAHAN YANG FATAL !!! JANGAN GUNAKAN "VARIABEL" DALAM FUNGSI INCLUDE / REQUIRE Contoh: body.php ----- <?php include $_GET['file']; echo "<br>Ini adalah contant atau isi dari website ku<br>"; ?> ----- BAHAYA !!! kenapa?? karena orang lain bisa menambahkan file dari luar ...!!! binggo..!!! server anda kena...!!! ------

Membahas Fungsi if - ifelse - else 24-03-2007 | 00:16:54 | Halaman Ini dibaca 7004 kali Di bagian ini kita akan membahas - Define - PHP Decision Making Ok kita mulai - Define di sini saya akan jelaskan sedikit tentang define: define mirip dengan, "string": --------------- <?php define("HELLO", "Hello world!"); echo HELLO; ?> --------------- Nanti hasilnya: "Hello world." - PHP Decision Making PHP Decision Making yang akan saya jelas kan kali ini adalah yang sering di pakai dalam programing website - if

Page 21: KUmPulan PHP Scrip

- else - elseif Penggunaan fungsi "if" Sebagai penalaran "if" anda samakan dengan "jika" Contoh: if (kondisi expresi) { kode anda; } Contoh Penerapan: <?php if ($user == b_scorpio) { echo "user anda b_scorpio"; } ?> Contoh Penerapan untuk user autentifikasi: <?php if ($user == b_scorpio && $password == 8_5c0rp10) { echo "anda berhasil login"; } ?> Penggunaan fungsi "else" Sebagai penalaran anda asumsikan "else" sebagai "jika tidak" Contoh: if (kondisi expresi) { kode anda; } else { kode anda; } Contoh Penerapan: <?php if ($user == b_scorpio) { echo "user anda b_scorpio"; } else

Page 22: KUmPulan PHP Scrip

{ echo "anda bukan b_scorpio"; } ?> Contoh Penerapan untuk user autentifikasi: <?php if ($user == b_scorpio && $password == 8_5c0rp10) { echo "anda berhasil login"; } else { echo "anda tidak bisa login"; } ?> Penggunaan fungsi "elseif" "elseif" di gunakan untuk, statment "if" lebih dari satu Contoh Penerapan untuk user autentifikasi: -------------- <?php if ($user == b_scorpio) { echo "anda login sebagai $user"; } elseif ($user == alk) { echo "anda login sebagai $user"; } elseif ($user == johan) { echo "anda login sebagai $user"; } else { echo "anda tidak bisa login"; } ?>

Membatasi Donload Dengan PHP <?php

Page 23: KUmPulan PHP Scrip

session_start(); if (ISSET($_SESSION['download']) && ($_SESSION['download'] == $_SESSION['cekdownload']) ) { // artibut file $fileName = 'sample.zip'; $mimeType = 'application/zip'; if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5') or strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7')) { $mimeType = 'application/x-download'; } // file apa yang mau didownload --> untuk browser header('content-disposition: attachment; filename=' . $fileName); header('content-type: ' . $mimeType); header('content-length: ' . filesize($fileName)); // Display the file readfile($fileName); } else { header("location: redirect"); } ?>

Membuat Cuplikan Artikel 1. File Koneksi.php <?php $host="localhost"; $uname="root"; $pass="phii"; $db="db_artikel"; $koneksi=mysql_connect($host,$uname,$pass); mysql_select_db($db,$koneksi); ?> 2. File fungsi.php

Page 24: KUmPulan PHP Scrip

<?php function cuplikArtikel($konten,$url) { $maxKata=20; //max kata dalam cuplikan artikel --> silahkan diganti sesuai kebutuhan $pecahArtikel=explode(' ',$konten); //pecah artikel menjadi array of string if(count($pecahArtikel) > $maxKata) { $cuplik=''; for($a=0;$a<$maxKata;$a++) { $cuplik.=$pecahArtikel[$a]." " ; } echo"$cuplik . . . .<br><a href=$url>Selengkapnya>> </a>"; } else { echo $konten; } }? > 3. File artikel_cuplik.php <?php include 'koneksi.php'; include 'fungsi.php'; if(isset($_GET['act']) && !empty($_GET['act']) && ($_GET['act']==='detail')) { file_exists('artikel_detail.php') ? include 'artikel_detail.php' : die('Halaman detail artikel tidak ditemukan'); }e lse { $sqlArtikel=mysql_query("select * from artikel order by id desc"); echo"<table width=50%>"; while($hasilSqlArtikel=mysql_fetch_row($sqlArtikel)) { $id=$hasilSqlArtikel[0]; $judul=$hasilSqlArtikel[1]; $konten=$hasilSqlArtikel[2]; $tgl=$hasilSqlArtikel[3]; $pengirim=$hasilSqlArtikel[4]; $url='?act=detail&idDet='.$id.''; echo"<tr><td>"; echo"<b>$judul</b><br>$pengirim | $tgl<br>"; cuplikArtikel($konten,$url); echo"<hr>";

Page 25: KUmPulan PHP Scrip

} }? > 5.File artikel_detail.php <?php $idDetail=$_GET['idDet']; $sqlDetail=mysql_query("select * from artikel where id='$idDetail'"); $hasilSqlDetail=mysql_fetch_array($sqlDetail); $judul=$hasilSqlDetail['judul']; $konten=$hasilSqlDetail['konten']; $tgl=$hasilSqlDetail['tgl']; $pengirim=$hasilSqlDetail['pengirim']; echo"<table width=50%><tr><td>"; echo"<b>$judul</b><br>$pengirim | $tgl<br><br>$konten"; echo"</td></tr> <tr><td align=right> <a href = artikel_cuplik.php> << back </a></td></tr> </table>"; ?>

Membuat komentar pada php config.php <?php $host = "localhost"; $username = "root"; $password = ""; $databasename = "cms_artikel"; $connection = mysql_connect($host, $username, $password); mysql_select_db($databasename, $connection) ; ?> Kemudian script untuk menampilkan judul artikelnya ... main.php <?php require_once "config.php"; $query = "select * from artikelnya"; $runquery = mysql_query($query); while($result = mysql_fetch_array($runquery)) { $id = $result['id']; $judul = $result['judul']; $isi_artikelnya = $result['isi_artikel']; $isi = substr($isi_artikelnya,0,400); $isi = substr($isi_artikelnya,0,strrpos($isi," ")); print "<b>$judul<br></b>$isi..."; print "<a href=view.php?page_detil=$id>Selengkapnya</a><br><br>"; }? >

Page 26: KUmPulan PHP Scrip

Script untuk menampilkan artikel secara keseluruhan ... view.php <?php require_once "config.php"; $id = $_GET['page_detil']; if (ISSET($_GET['page_detil'])) { $query = "select * from artikelnya where id=$id"; $runquery = mysql_query($query); $result = mysql_fetch_array($runquery); $isi_artikel = $result['isi_artikel']; print $isi_artikel ; print "<br><a href=main.php>Kembali ... </a><br><br>"; print "<b>Komentarnya : </b><hr>"; require_once "comment.php"; } else print ""; if ($_REQUEST['entry'] == "comment") { $id_artikel = $_POST['id_artikel'] ; $nama = $_POST['nama']; $komentar = $_POST['komentar']; $query2 = "insert into komentarnya values('','$nama','$komentar','$id_artikel')"; $runquery2 = mysql_query($query2); print "Berhasil di masukkan<br>"; print "<a href=view.php?page_detil=$id_artikel>Kembali...</a>"; } ?> Ini adalah script untuk menampilkan komentar, sekaligus form untuk memasukkan komentar comment.php <?php require_once "config.php"; $query = "select nama_komentar, isi_komentar from komentarnya where id_artikel=$id order by id desc"; $runquery = mysql_query($query); while ($result = mysql_fetch_array($runquery)) { $nama_komentar = $result['nama_komentar']; $isikomentar = $result['isi_komentar']; print "Nama : $nama_komentar <br> $isikomentar <hr>" ; }? > <html> <body><br>

Page 27: KUmPulan PHP Scrip

<b>Isi Komentar :</b> <form action="?entry=comment" method="post"> nama : <input type=text name=nama><br> komentar : <br><textarea name=komentar rows=6 width=200></textarea><br> <input type=submit name=submit value=submit> <input type=hidden name=id_artikel value="<?php print $id ;?>"> </form> </body> </html>

Kalkulator OnLine dg php <html> <head> <title>Kalkulator online created by Ivan wen piao</title> </head> <body> <form name="form1" method="post" action="hitung.php"> <p>Operator matematika </p> <p> <input name="angka" type="text" id="angka"> <select name="operator" id="operator"> <option>+</option> <option>-</option> <option>*</option> <option>/</option> </select> <input name="angka2" type="text" id="angka2"> <input type="submit" name="Submit" value="Hitung"> </p> </form> <form name="form2" method="post" action="hitung2.php"> <p>Konstanta matematika </p> <p> <input name="angka" type="text" id="angka"> <select name="operator" id="operator"> <option>abs</option> <option>floor</option> <option>round</option> <option>decbin</option> <option>bindec</option> <option>decoct</option> <option>octdec</option> <option>dechex</option> <option>hexdec</option> <option>sin</option> <option>cos</option> <option>tan</option> <option>log</option> <option>sqrt</option> </select> <input type="submit" name="Submit" value="Hitung">

Page 28: KUmPulan PHP Scrip

</p> <p>Keterangan :</p> <table width="100%" border="0"> <tr> <td>+</td> <td>=</td> <td>Tambah.</td> </tr> <tr> <td>-</td> <td>=</td> <td>Kurang.</td> </tr> <tr> <td>*</td> <td>=</td> <td>Kali.</td> </tr> <tr> <td>/</td> <td>=</td> <td>Bagi.</td> </tr> <tr> <td width="4%">abs</td> <td width="2%">=</td> <td width="94%">Mencari nilai absolut .</td> </tr> <tr> <td>floor</td> <td>=</td> <td>Pembulatan nilai. </td> </tr> <tr> <td>round</td> <td>=</td> <td>Membulatkan angka di belakang koma. </td> </tr> <tr> <td>decbin</td> <td>=</td> <td>Mengkonversi menjadi bilangan biner. </td> </tr> <tr> <td>bindec</td> <td>=</td> <td>Mengkonversi menjadi bilangan desimal. (kebalikan dari decbin) </td> </tr> <tr> <td>decoct</td> <td>=</td>

Page 29: KUmPulan PHP Scrip

<td>Mengkonversi menjadi bilangan oktal. </td> </tr> <tr> <td>octdec</td> <td>=</td> <td>Mengkonversi menjadi bilangan desimal (kebalikan dari decoct) </td> </tr> <tr> <td>dechex</td> <td>=</td> <td>Mengkonversi menjadi bilangan Hexadesimal. </td> </tr> <tr> <td>hexdec</td> <td>=</td> <td>Mengkonversi menjadi bilangan desimal (kebalikan dari dechex). </td> </tr> <tr> <td>sin</td> <td>=</td> <td>sine</td> </tr> <tr> <td>cos</td> <td>=</td> <td>cosine</td> </tr> <tr> <td>tan</td> <td>=</td> <td>tangent</td> </tr> <tr> <td>log</td> <td>=</td> <td>log</td> </tr> <tr> <td>sqrt</td> <td>=</td> <td>akar</td> </tr> </table> </form> <p>&nbsp;</p> </body> </html> Lalu untuk file yang kedua buat dengan nama “hitung.php” dan ketikkan script berikut : <?php $angka = $_POST['angka']; $angka2 = $_POST['angka2'];

Page 30: KUmPulan PHP Scrip

$operator = $_POST['operator']; if ($operator == '+') { $hasil = $angka+$angka2; }i f ($operator == '-') { $hasil = $angka-$angka2; }i f ($operator == '*') { $hasil = $angka*$angka2; }i f ($operator == '/') { $hasil = $angka/$angka2; } print "$hasil"; ?> Dan file yang terakhir, buat dengan nama “hitung2.php” lalu ketik script berikut : <?php $angka = $_POST['angka']; $operator = $_POST['operator']; if ($operator == 'abs') { $hasil = abs($angka); }i f ($operator == 'floor') { $hasil = floor($angka); }if ($operator == ' round') { $hasil = round($angka); }i f ($operator == 'decbin') }i f ($operator == 'hexdec') { $hasil = hexdec($angka); }i f ($operator == 'sin') { $hasil = sin($angka); }i f ($operator == 'cos') { $hasil = cos($angka); }i f ($operator == 'tan') {

Page 31: KUmPulan PHP Scrip

$hasil = tan($angka); }i f ($operator == 'log') { $hasil = log($angka); }i f ($operator == 'sqrt') { $hasil = sqrt($angka); } print "$hasil"; ?>