Selasa, 24 Juli 2012

MEMBUAT FORM LOGIN MENGGUNAKAN PHP DAN MYSQL

Buat databasenya terlebih dahulu di MySQL , kita akan memBuat nama databasenya  “admin” dan Tabelnya juga “admin”
 lihat pada gambar
Yaitu terdiri dari 6 file yaitu: id, nama, email, kelamin, user, dan password. Primary key nya  “id”
Cara input data klik pada insert atau sisipkan
Password jangan lupa buat fungsi” MD5”, lalu Klik GO
Selanjutnya kita akan membuat file koneksi, login, keluar dan home. Simpan filenya kedalam folder “htdocs  dan buat folder satu lagi dengan nama “login”, ingat , ini hanyalah contoh...

Selanjutnya kita membuat koneksi
Script Koneksi:
html>
<head>
</head>
<body>
<?
ini_set('display_errors',FALSE);
$host="localhost";
$user="root";
$pass="";
$db="admin";


$koneksi=mysql_connect($host,$user,$pass);
$tanggal=date("Y-m-d H:i:s");

if ($koneksi)
{
                //echo "berhasil : )";
}else{
                ?><script language="javascript">alert("Gagal Koneksi Database MySql !!")</script><?
}

?>

</body>
</html>

Simpan dengan nama “conn.php”
Selanjutnya membuat menu “login”
<? session_start();
if (isset($_POST['admin']))
{
                include ("conn.php");
                $user_name=htmlentities((trim($_POST['admin'])));
                $password=htmlentities(md5($_POST['kunci']));
               
                $login=mysql_db_query($db,"select * from admin where user='$user_name' and password='$password'",$koneksi);
               
                $cek_login=mysql_num_rows($login);
                                if (empty($cek_login))
                                {
                                                ?><script language="javascript">
                                                alert("Maaf, Password Anda salah!!");
                                                document.location="login.php";
                                                </script><?
                                }
                                else
                                {
                                                //daftarkan ID jika user dan password BENAR
                                                while ($row=mysql_fetch_array($login))
                                                {
                                                                $id=$row[0];
                                                                session_register('id');
                                                                session_register('user_name');
                                                }
                                                echo "<script> document.location.href='home.php'; </script>";
                                }
}
?>


<html>
<title>Login</title>
<head>
<script language="javascript">
function cek(){
                var user= document.getElementById('userid').value;
                var pass= document.getElementById('passwd').value;
                if(user.replace(/^\s+|\s+$/g, '')==''){
                                alert('Username Harus Diisi!');
                                return false;
                }
                if(pass.replace(/^\s+|\s+$/g, '')==''){
                                alert('Password Harus diisi!');
                                return false;
                }
                return true;
}
</script>
</head>

<form action="login.php" method="post">
  <p align="center"><? echo $status;?></p>
  <table width="251" height="101" border="0" align="center">
<tr valign="bottom">
                <td width="104" height="35"><font size="4" face="verdana">Username</font></td>
                <td width="137"><input type="text" name="admin" size="20" id="userid"></td>
</tr>

<tr valign="top">
                <td height="34"><font size="4" face="verdana">Password</font></td>
                <td><input type="password" name="kunci" size="20" id="passwd"></td>
</tr>

<tr>
                <td>&nbsp;</td>
                <td><input name="submit" type="submit" onClick="return cek()" value="LOGIN"></td>
</tr>
</table>
</form>

</body>
</html>

Selanjutnya Simpan dengan nama “login.php”
Selanjutnya kita akan membuat menu utama
<? session_start();
if (session_is_registered('id'))
{
                $_SESSION['id'];
                $_SESSION['user'];
               
                ?>
                <html>
                <head>
                                <title>[Admin]</title>
                                <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
                                <style type="text/css">
                                <!--
.style2 {
                font-family: "comic Sans MS";
                font-size: 14px;
}
.style3 {
                font-family: Arial, Helvetica, sans-serif;
                font-style: italic;
}
.style4 {font-family: Arial, Helvetica, sans-serif}
.style5 {
                color: #FF0000;
                font-weight: bold;
}
                                -->
                                </style>
                </head>
                <body background="./img/background.jpg">
                <p>&nbsp;</p>
                                <table border="0" align="center" bgcolor="#FFFFFF">
                                <tr>
                                                <td width="501">
                                               
                                               
                                               
                                                <table width="547" height="368" border="0" align="center">
                                                <tr>
                                                                <td width="444" height="32" align="center" valign="top" class="style4"><marquee behavior="scroll" scrollamount="3">Selamat Datang <? echo $_SESSION['user_name'];?>
                                                                </marquee>
                                                                </td>
                                                </tr>
                                                <tr>
                                                                <td height="21" align="center" bgcolor="#00CCFF"><a href="logout.php" class="style5" title="Keluar">Menu Logout</a></td>
                                                </tr>
                                                <tr>
                                                                <td height="260" align="center" bgcolor="#0099FF">
                                                                  <span class="style3">isi dari web anda</span></td>
                                                </tr>
                                                <tr>
                                                                <td height="45"><div align="center" class="style2">Copyright &copy; ismade.blogspot.com</div></td>
                                                </tr>
                                                </table>
                                               
                                               
                                  </td>
                                </tr>
                                </table>
                    <p>&nbsp;</p>
                </body>
                </html>
<?
}else{
                ?><script language="javascript">
                alert("Maaf, Anda tidak berhak mengakses halaman ini!!");
                document.location="login.php";
                </script>
                <?
}
?>
Simpan dengan nama “home.php”
Lalu kita membuat file logout
<? session_start();
if (session_is_registered('id'))
{
                session_unregister("id");
                session_unregister("user_name");
                //session_destroy();
                ?><script language="javascript">
                alert("Anda Berhasil logout.");
                document.location="login.php";
                </script><?
               
}else{
                ?><script language="javascript">
                alert("Maaf, Anda tidak berhak mengakses halaman ini!!");
                document.location="login.php";
                </script>
                <?
}
?>

Simpan dengan nama “logout.php”
Sampai disini dulu , apabila langkah ya benar maka akan tampil seperti berikut
selamat mencoba

4 komentar:

ululf01 mengatakan...

Makasih gan sudah berbagi ilmunya

moga bermanfaat dan ilmu bertambah

Dyan123 mengatakan...

sama2.......

jika pada Xampp 1.7.3
program ini tidak akan jalan,maka gantilah semua script yang bertanda <? gantilah dengan <?php .

Semoga bermanfaat

Lelly mengatakan...

saya ingin tanya setelah tampil menu loginnya, password dan username nya di ketikan apa supaya masuk ke menu utama???

Dyan123 mengatakan...

di SQL nya jangan lupa di isi Username : admin
passwordnya : admin

jangan lupa untuk passwordnya silahkan pilih MD5, untuk lebih jelasnya lihat gambar yang ke-2

Posting Komentar

Terbit Lapak

Lapak

 
Design by Free WordPress Themes | Bloggerized by dyan123 - Premium Blogger Themes | cheap international calls