Anda di halaman 1dari 3

Membuat Halaman Login Buat tabel admin

Tambahkan data ke tabel admin, misal usename=admin password =admin.

Selanjutya, buat 4 file berikut ini didalam folder admin 1. 2. 3. 4. 5. Koneksi.php (bisa memakai file di bab sebelumnya) Buat login_fom.php Buat login_action.php Buat admin.php Buat logout_action.php

Koneksi.php <?php define('db_host','localhost'); define('db_user','root'); define('db_pass',''); define('db_name','kursus'); mysql_connect(db_host,db_user,db_pass); mysql_select_db(db_name); ?>

Login_form.php <html> <head> <title> Buku tamu </title> <script src="../js/valid.js"></script> </head> <body> <h1 >Login</h1> <table> <form name='login' method="POST" action="login_action.php">

<tr><td><label>Username</label></td><td> <input type="text" name='username'/></td></tr> <tr><td><label>password</label></td><td> <input type="password" name='password' /></td></tr> <tr><td></td><td><input type='reset' value="clear" /> <input type='submit' value="login" /></td></tr> </form> <script language="javaScript" type="text/javascript" xml:space="preserve"> var f = new Validator("login"); f.EnableOnPageErrorDisplaySingleBox(); f.EnableMsgsTogether(); f.addValidation("username","req","nama harus di isi!"); f.addValidation("password","req","password harus di isi!"); </script> </table> <div id="login_errorloc" style="color:#FF0000;"> <?php if(isset($_GET['status'])){ echo "Login Gagal!"; } ?> </div> </body> </html>

Login_action.php <?php session_start(); include ('../koneksi.php'); $username = $_POST['username']; $password = md5(trim($_POST['password'])); $sql = "select * from admin where username='$username' and password='$password' "; $sql_login = mysql_query($sql) or die(mysql_error()); $hasil = mysql_fetch_object($sql_login); if(mysql_num_rows($sql_login) == 1) { $_SESSION['username'] = $username; header("location:admin.php");

} else { header("location:login_form.php?status=1"); }?>

Admin.php <?php session_start(); $username=$_SESSION['username']; if(empty($username)){ header("location:login_form.php"); } ?> <html> <head> <title> Halaman admin</title> </head> <body> <h2> Halaman utama pengolahan Data <a href='logout_action.php'?>Logout</a> </body> </html>

Logout.php <?php session_start(); session_destroy(); header("location:login_form.php"); ?>