Dalam sebuah website terdapat fungsi fungsi dan definisi definisi yang akan selalu digunakan dalam website tersebut. Maka dibuatlah sebuah file yang berisi fungsi fungsi yang selalu dipanggil dalam website tersebut.fungsi dari file common ini bisa berupa file konfigurasi misal untuk mengganti theme website, mengganti bahasa website dan lain sebagainya.
Praktek:
- Buat sebuah folder bernama html di directory c:\apache\htdocs
- Buat file yang bernama about.txt, pasuruan.txt, surabaya.txt, contact.txt, depan.txt dan simpan file-file tersebut di directory c:\apache\htdocs\html
- Buat file common.php dan index.php menggunakan Crimson Editor atau notepad.
- Simpan kedua file tersebut di directory c:\apache\htdocs
- Jalankan program apache anda; Start – Programs – PHPTriad - Apache Console – Start Apache.
- Jalankan program mysql anda; Start – Programs – PHPTriad – MySQL – MySQL-D-NT.
- Buka Internet Explorer anda.
- Tulis di address http://localhost atau http://127.0.0.1 atau http://[IP Address Komputer
- anda] atau http://[Nama Komputer Anda]
Contoh file Common.php :
<?
//fungsi yang tidak ada variabelnya
function menu() {
echo "<font size=2 color=blue>Function menu ini berisikan menu yang harus dibuat sendiri</font>\n";
}
//definisi nama_perusahaan
define ("nama_perusahaan","PT Sepeda Surabaya");
?>
Kemudian Buat file index.php :
<html>
<head>
<title>Halaman Pertama</title>
<style type="text/css">
body{
font-family:arial;
font-size:1;
}
a:link{
text-decoration:none;
}
a:visited{
font-style:bold;
font-size:12;
text-decoration:none;
}
</style>
</head>
<body>
<?
require("common.php");
menu();
//perhatikan cara memanggilnya
?>
<H1><? echo(nama_perusahaan); ?></h1>
<!--- Perhatikan cara memanggilnya --->
<table width="100%" bgcolor="yellow">
<tr>
<td> Website Percobaan</td>
</tr>
<tr>
<td bgcolor="red"><a href="index.php">Halaman Depan</a> | <a href="index.php?sub=about">About Us</a> | <a href="index.php?sub=contact">Contact Us</a>
| <a href="index.php?sub=contact&bagian=pasuruan">contact 1 </a>| <a href="index.php?sub=contact&bagian=surabaya">contact 2</a></td>
</tr>
<tr>
<td bgcolor="#FFCCFF">
<?
if ($sub=="about"){
require("html/about.txt");
}
elseif ($sub=="contact"){
if($bagian=="pasuruan"){
require("html/pasuruan.txt");
}
elseif ($bagian=="surabaya"){
require("html/surabaya.txt");
}
else require("html/contact.txt");
}
else require("html/depan.txt");
?>
</td>
</tr>
</table>
</body>
</html>