Kamis, 08 Desember 2011

STUDI KASUS PHP (Shoutbox, Hitcounter, Gallery, UserOnline)+Validasi Form

AGAR Shoutbox, dan Useronline di perlukan sebuah koneksi database, Scripnya sebagai berikut :
Kebetulan nama koneksi yang saya gunakan adalah "buka.php", apabila temen2 memakai nama lain maka sesuaikan apabila ingin include database, sbg contoh :  include "buka.php";  jika file koneksi yang di buat "connect.php" maka include "connect.php";  , Selanjutnya untuk "localhost,root, dan labkom6" sesuaikan dengan setingan yang ada di komputer masing2 atau sesuaikan pada database yang telah di buat pada webhosting


Membuat shoutbox,
=======================
Sebelum menuliskan scriptnya, buat dulu tabel dengan nama "shoutbox"
Untuk id jadikan primary key dengan meng-klik gambar kunci pada action


Untuk script shoutbox.php :

</html>
<head>
    <title> :: shoutbox :: </title>
<script language="javascript">
function validasi(form1){
   if(document.form1.nama.value==""){
    alert("Isi nama dulu bro ....!!");
    form1.nama.focus();
    return false;
    }
   if(document.form1.pesan.value==""){
    alert("Tinggalkan pesan anda ....!!");
    form1.pesan.focus();
    return false;
    }
   return true;
}  
</script>
</head>
<body>
<table border="1">
<tr><td>
<div align="left" style="height:300px; margin-left:2px; overflow:auto; padding:3px text-align:left; width:99%;">
<!-- untuk menampilkan isi shoutbox -->
<?php
include "buka.php";
$sl="select * from shoutbox order by id desc";
$h=mysql_query($sl);
$i=0;
while($d=mysql_fetch_row($h)){
$i++;
echo "No : $d[0]<br />";
echo "Waktu : $d[3]<br />";
echo "Nama : $d[1]<br />";
echo "Pesan : $d[2]<br />";
echo "================<br />";
}
?>
</div>
<!-- untuk menyimpan data ke tabel -->
<?php
include "buka.php";
if(isset($_POST[submit])){
$sl="insert into shoutbox values('', '$_POST[nama]', '$_POST[pesan]', '$_POST[waktu]')";
     $query=mysql_query($sl);
   if($query){
    echo "Thank You .. :D";
} else {
    echo "Data gagal disimpan";
}
}
?>
<div>
<form name="form1" method="POST" action="shoutbox.php" onSubmit="return validasi(this);">
Nama : <input type="text" name="nama" id="nama"><br />
Pesan : <textarea name="pesan" id="pesan"></textarea><br />
<input type="hidden" name="waktu" id="waktu" value="<?php echo date('Y-m-j'); ?>"><br />
<input type="submit" name="submit" value="simpan">
<input type="reset" name="reset" value="Reset">
</form>
</div>
</td></tr>
</table>
</body>
</html>

Script diatas sudah terdapat validasi form input.

Membuat Gallery
=================
Untuk membuat gallery, siapkan dulu folder dengan nama "gambar" dan masukkan beberapa images kedalam folder tersebut.

Untuk script gallery.php
<html>
<head>
    <title> Galerry </title>
</head>
<body>
<div align="center">
<?php
$i = 0;
$namadir = "gambar";
$dir = dir($namadir);
echo"<teble border'0' cellpadding='3' cellspacing='3'>";
while($endir=$dir->read()) {
if($endir=="."||$endir=="..")
{
   continue;
}
   $fp=@fopen("$namadir/$endir","r");
if($i=='0')
{
    echo "<tr>";
}
if($i=='5')
{
    echo "<tr>";
}
if($i=='10')
{
    echo "<tr>";
}
if($i=='20')
{
    echo "<tr>";
}
?>
<td width="100">
<a href="<?php echo "$namadir/$endir" ?>">
<img src="<?php echo "$namadir/$endir" ?>" alt="<?php echo $endir ?>" width="60" height="50">
</a>
</td>
<?php
$i=$i+1;
}
?>
</tr></table>
</div>
</body>
</html>

Hasilnya :
Jika script yang dituliskan benar maka hasinya akan keluar seperti diatas

Membuat Hitcounter
==================
Script hitcounter.php
<html>
<head>
    <title> counter </title>
</head>
<body>
<?php
if(file_exists("counter.dat")) {
    $exist_file = fopen("counter.dat","r");
    $new_count=fgets($exist_file,255);
    $new_count++;
    fclose($exist_file);
    echo ("$new_count pengunjung");
    $exist_count=fopen("counter.dat","w");
    fputs($exist_count,$new_count);
    fclose($exist_count);
} else {
    $new_file=fopen("counter.dat","w");
    fputs($new_file,"1");
    echo("1 pengunjung");
    fclose($new_file);
}

?>
</body>
</html>

Untuk menyimpan jumlah hit-nya, buat file "counter.dat" dengan notepad dan isikan angka 0 didalamnya kemudian save. Untuk hasil nya, coba sendiri ea ...... hehe ......

Membuat User Online
======================
Pertama buat dulu tabel dengan nama "useronline" dengan field :
Untuk yang ini, tidak perlu primary key
Untuk script useronline.php

<?php
include "buka.php";
$detikout=300;

$waktu=time();
$timeout=$waktu-$detikout;

$insert=mysql_query("insert into useronline values ('$waktu','$REMOTE_ADDR','$PHP_SELF')");
if(isset($_POST['insert'])){
    echo "Error";
}
$delete=mysql_query("delete from useronline where waktu < $timeout");
if(isset($_POST['delete'])){
    echo "Error";
}
$hasil=mysql_query("select distinct ip from useronline where file='$PHP_SELF'");
if(isset($_POST['hasil'])){
    echo "Error";
}
$user=mysql_num_rows($hasil);
echo("$user user sedang online\n");
?>

Hasilnya coba sendiri..... semangat ......

1 komentar: