Membuat form login dengan php mysql,
Berikut ini saya akan membuat sebuah form login dengan php mysql. Langkah pertama kita harus sudah mempunyai tabel user di database lihat
cara membuat tabel user di phpmyadmin
Setelah mempunyai tabel user langkah selanjutnya untuk membuat form login dengan php
1 .Membuat file koneksi.php
<?php
//variabel database
$nama_host="localhost";
$user_db="root";
$password_db="root";
$nama_db="loginuser";
//koneksi database
$koneksi=mysql_connect($nama_host,$user_db,$password_db);
//bila terkoneksi
if($koneksi){
//pilih database
mysql_select_db($nama_db);
}else{
echo "Database tidak terkoneksi";
}
?>
2.
Membuat form login.php
<?php
// memulai session
session_start();
include "koneksi.php";
if(isset($_POST['Submit'])){
$username = $_POST['username'];
$password = md5($_POST['pass']);
if(empty($username)){
echo "<script type='text/javascript'>
onload =function(){
alert('Username belum diisi');
}
</script>";
}
elseif(empty($password)){
echo "<script type='text/javascript'>
onload =function(){
alert('Password belum diisi');
}
</script>";
} else {
// query untuk mendapatkan record dari username
$query = "SELECT * FROM user WHERE username = '$username'";
$hasil = mysql_query($query);
$data = mysql_fetch_array($hasil);
// cek kesesuaian password
if ($password == $data['password'])
{
header("location:inputuser.php");
// menyimpan username dan level ke dalam session
$_SESSION['level'] = $data['level'];
$_SESSION['username'] = $data['username'];
$_SESSION['nama_lengkap'] = $data['nama_lengkap'];
$_SESSION['email'] = $data['email'];
}
echo "<script type='text/javascript'>
onload =function(){
alert('Username atau password salah!! Ulangi kembali');
}
</script>";
}
}
?>
<html>
<head>
<title>Login Administrator</title>
<link rel="stylesheet" type="text/css" href="HTML/style.css">
<style type="text/css">
<!--
.style13 { color: #0066CC;
font-weight: bold;
font-size: 16px;
font-family: Arial, Helvetica, sans-serif;
}
.style7 {color: #CC0000;
font-weight: bold;
}
.style7 {color: #333333}
.style9 {color: #003399; font-weight: bold; font-size: 14px; }
-->
</style>
</head>
<body leftmargin=0 topmargin=0 marginheight="0" marginwidth="0" bgcolor="#ffffff">
<table border="0" cellspacing="0" cellpadding="0" width="100%" height="83%">
<tr>
<td><table width="780" border="0" cellspacing="0" cellpadding="0">
<tr>
</tr>
<tr>
<td background="../gambar/fon02.gif" height="34"><img src="../gambar/separator.gif" width="2" height="34"></td>
</tr>
</table>
<p class="px">
<table width="644" border="0" align="center" cellpadding="1" cellspacing="0">
<tr>
<td width="549"><p> </p>
<table width="413" border="1" align="center" cellpadding="1" cellspacing="0" bordercolor="#CCCCCC">
<tr>
<td valign="middle"><span class="style13">Login Administrator </span></td>
</tr>
<tr>
<td background="../gambar/back_right.jpg"><table width="350" border="0" align="center" cellspacing="0" bordercolor="#999999">
<tr>
<td><form action="" method="post" name="form1" id="form1">
<p align="center" class="style9">Masukkan Username dan Password anda !! </p>
<table width="244" height="87" border="0" align="center">
<tr>
<td width="92"><strong>Username</strong></td>
<td width="210"><label>
<input name="username" type="text" id="username" size="0" style="background:#FFFF66" />
</label></td>
</tr>
<tr>
<td><strong>Password</strong></td>
<td><label>
<input name="pass" type="password" id="pass" size="0" style="background:#FFFF66" />
</label></td>
</tr>
<tr>
<td> </td>
<td><label>
<input name="Submit" type="submit" id="Submit" style="background-color:#CC9933" value="Masuk"/>
</label></td>
</tr>
</table>
</form></td>
</tr>
</table>
</tr>
</table>
<p align="center">
</p>
</p>
<div align="center"><a href="../index.php">Back to Home</a> </div></td>
</tr>
</table>
</center>
</td>
</tr>
</table>
</body>
</html>
maksud tulisan yang berwarna merah adalah sesudah login akan diarahkan ke-inputuser.php
form login php
membuat file
inputuser.php
<?php
session_start();
include "koneksi.php";
if (isset($_SESSION['level']) && isset($_SESSION['username']))
{
if ($_SESSION['level'] == "admin")
{
if(isset($_POST['simpan'])){
$username=$_POST['username'];
$password=md5($_POST['password']);
$password2=md5($_POST['password2']);
$nama_lengkap=$_POST['nama_lengkap'];
$email=$_POST['email'];
$telp_hp=$_POST['telp_hp'];
$level=$_POST['level'];
if(empty($username)||empty($password)||empty($password2)||empty($nama_lengkap)||empty($email)||empty($telp_hp)||empty($level))
{
echo "<script type='text/javascript'>
onload =function(){
alert('Semua data harus diisi!');
}
</script>";
}else{
$a="insert into user(username,password,nama_lengkap,email,telp_hp,level)values('$username','$password','$nama_lengkap','$email','$telp_hp','$level')";
$b=mysql_query($a);
if($b){
echo "<script>alert('Data berhasil disimpan!');javascript:history.go(-1);</script>";
}else{
echo "<script type='text/javascript'>
onload =function(){
alert('Data gagal disimpan');
}
</script>";
}
}
}
?>
<head>
<title>Administrator Area </title>
</head>
<td><?php include "menu.php"; ?></td>
<body leftmargin=0 topmargin=0 marginheight="0" marginwidth="0" bgcolor="#ffffff">
<table border="0" cellspacing="0" cellpadding="0" width="100%" height="83%">
<tr>
<td width="50%" background="../gambar/bg.gif"><img src="../gambar/px1.gif" width="1" height="1" alt="" border="0"></td>
<td valign="bottom" background="../gambar/bg_left.gif"><img src="../gambar/bg_left.gif" alt="" width="17" height="16" border="0"></td>
<td><table width="780" border="0" cellspacing="0" cellpadding="0">
<tr>
</tr>
<tr>
<td background="../gambar/fon02.gif" height="34"><div align="center"></div></td>
</tr>
</table>
<p align="center" class="px">
\
<table width="644" border="0" align="center" cellpadding="1" cellspacing="0">
<tr>
<td width="549"><table width="628" border="0" align="center">
<tr>
<td width="622"><div align="center">
<h2><strong>input user</strong></h2>
</div>
<form action="" method="post" name="form1" id="form1">
<table width="553" border="0" align="center" cellpadding="1" cellspacing="0">
<tr>
<td width="149">Username</td>
<td width="400"><label><?php if(!$_GET['username']){
echo "<input name='username' type='text' id='username' size='20'>";
}else{
echo "<b>".$baris['username']."</b>";
}
?>
</label></td>
</tr>
<tr>
<td>Password</td>
<td><label>
<input name="password" type="password" id="password" size="25" />
</label>
<?php
if($_GET['username']){
echo "<br><font color='red'>Apabila password tidak diubah, silahkan dikosongkan saja</font>";
}
?> </td>
</tr>
<tr>
<td>Nama Lengkap </td>
<td><label>
<input name="nama_lengkap" type="text" id="nama_lengkap" size="35" value="<?php echo $baris['nama_lengkap'];?>"/>
</label></td>
</tr>
<tr>
<td>Email</td>
<td><label>
<input name="email" type="text" id="email" size="35" value="<?php echo $baris['email'];?>"/>
</label></td>
</tr>
<tr>
<td>Telpon/HP</td>
<td><label>
<input name="telp_hp" type="text" id="telp_hp" size="15" value="<?php echo $baris['telp_hp'];?>"/>
</label></td>
</tr>
<tr>
<td>Level User </td>
<td><label>
<select name="level" id="level">
<option value="admin" <?php if($baris['level']=="admin"){ echo "selected";}?>>Administrator</option>
<option value="user" <?php if($baris['level']=="user"){ echo "selected";}?>>User</option>
</select>
</label></td>
</tr>
<tr>
</tr>
<td> </td>
<td><label>
<input name="simpan" type="submit" id="simpan" value="simpan" />
</label> <label>
<input type="submit" name="Submit2" value="Batal" />
</label>
</tr>
</table>
</form>
<p align="center"> </p>
<p align="center"> </p></td>
</tr>
</table>
<h3 align="center"> </h3>
<h3 align="center">Tabel User </h3>
<table width="690" border="1" align="center" cellspacing="0">
<tr bgcolor="#FFFF99">
<td width="50"><div align="center"><strong>No</strong></div></td>
<td width="100"><div align="center"><strong>Username</strong></div></td>
<td width="150"><div align="center"><strong>Nama Lengkap</strong></div></td>
<td width="150"><div align="center"><strong>Email</strong></div></td>
<td width="100"><div align="center"><strong>Telephon/HP</strong></div></td>
<td width="50"><div align="center"><strong>Level</strong></div></td>
</tr>
<?php
$sql="select * from user order by nama_lengkap ASC";
$query=mysql_query($sql);
$no=1;
while($data=mysql_fetch_array($query)){
?>
<tr>
<td><div align="center"><?php echo $no; ?></div></td>
<td><?php echo $data['username']; ?></td>
<td><?php echo $data['nama_lengkap']; ?></td>
<td><?php echo $data['email']; ?></td>
<td><?php echo $data['telp_hp']; ?></td>
<td><?php echo $data['level']; ?></td>
<td><?php echo $data['status']; ?></td>
</tr>
<?php
$no++;
}?>
</table>
<p align="center"> </p>
<p align="center"><a href="logout.php"></a></p>
<p> </p>
</td>
</tr>
</table>
<div class="px" align="center"><img src="../gambar/bot01.jpg" width="780" height="9" alt="" border="0"></div>
<table border="0" cellspacing="0" cellpadding="0" width="780" align="center">
<tr>
<td height="50">
<p class="bot"> </p>
</td>
</tr>
</table>
</td>
<td valign="bottom" background="../gambar/bg_right.gif"><img src="../gambar/bg_right.gif" alt="" width="17" height="16" border="0"></td>
<td width="50%" background="../gambar/bg.gif"><img src="../gambar/px1.gif" width="1" height="1" alt="" border="0"></td>
</tr>
</table>
</body>
</html>
<?php
}
else
{
// jika levelnya bukan admin, tampilkan pesan
echo "<script>alert('Sorry ya Coy.. Anda bukan admin');javascript:history.go(-1);</script>";
}
}
else
{
echo "<script>alert('. Login dulu dong!!');javascript:history.go(-1);</script>";
}
?>
membuat
menu.php
<?php
session_start();
if ($_SESSION['level'] == "admin")
{
echo "
<table width='760' border='0' align='center' cellpadding='1' cellspacing='0'>
<tr>
<td><div align='center'><a href='home.php'><img src='../gambar/Home.png' width='48' height='48' border='0' /></a></div></td>
<td><div align='center'><a href='setting.php'><img src='../gambar/Setting.png' width='48' height='48' border='0' /></a></div></td>
<td><div align='center'><a href='user.php'><img src='../gambar/User.png' width='48' height='48' border='0' /></a></div></td>
<td><div align='center'><a href='kategori.php'><img src='../gambar/Books.png' width='48' height='48' border='0' /></a></div></td>
<td><div align='center'><a href='berita.php'><img src='../gambar/Notepad.png' width='48' height='48' border='0' /></a></div></td>
<td><div align='center'><a href='download.php'><img src='../gambar/Globe.png' width='48' height='48' border='0' /></a></div></td>
<td><div align='center'><a href='kategori_produk.php'><img src='../gambar/katproduk.png' width='48' height='48' border='0' /></a></div></td>
<td><div align='center'><a href='produk.php'><img src='../gambar/produk.png' width='48' height='40' border='0' /></a></div></td>
<td><div align='center'><a href='order.php'><img src='../gambar/order.png' width='48' height='48' border='0' /></a></div></td>
<td><div align='center'><a href='uploadphoto.php'><img src='../gambar/order.png' width='48' height='48' border='0' /></a></div></td>
<td><div align='center'><a href='logout.php'><img src='../gambar/Minus.png' width='48' height='48' border='0' /></a></div></td>
</tr>
<tr>
<td><div align='center'><span class='px style1'><a href='home.php' STYLE='TEXT-DECORATION:NONE'>Home</a></span></div></td>
<td><div align='center'><span class='px style1'><a href='setting.php' STYLE='TEXT-DECORATION:NONE'>Setting</a></span></div></td>
<td><div align='center'><span class='px style1'><a href='user.php' STYLE='TEXT-DECORATION:NONE'>User</a></span></div></td>
<td><div align='center'><span class='px style1'><a href='kategori.php' STYLE='TEXT-DECORATION:NONE'>Kategori Berita</a></span></div></td>
<td><div align='center'><a href='berita.php' STYLE='TEXT-DECORATION:NONE'>Berita</a></div></td>
<td><div align='center'><a href='download.php' STYLE='TEXT-DECORATION:NONE'>Download</a></div></td>
<td><div align='center'><span class='px style1'><a href='kategori_produk.php' STYLE='TEXT-DECORATION:NONE'>Kategori Produk</a></span></div></td>
<td><div align='center'><a href='produk.php' STYLE='TEXT-DECORATION:NONE'>Produk</a></div></td>
<td><div align='center'><a href='order.php' STYLE='TEXT-DECORATION:NONE'>Order</a></div></td>
<td><div align='center'><a href='uploadphoto.php' STYLE='TEXT-DECORATION:NONE'>uploadphoto</a></div></td>
<td><div align='center'><a href='logout.php' STYLE='TEXT-DECORATION:NONE'>LogOut</a></div></td>
</tr>
<tr><td colspan=10><hr></td></tr>
</table>
<p> </p>";
}
else if ($_SESSION['level'] == "user")
{
echo "
<table width='432' border='0' align='center' cellpadding='1' cellspacing='0'>
<tr>
<td><div align='center'><a href='home.php'><img src='../gambar/Home.png' width='48' height='48' border='0' /></a></div></td>
<td><div align='center'><a href='berita_usr.php'><img src='../gambar/Notepad.png' width='48' height='48' border='0' /></a></div></td>
<td><div align='center'><a href='logout_usr.php'><img src='../gambar/Minus.png' width='48' height='48' border='0' /></a></div></td>
</tr>
<tr>
<td><div align='center'><span class='px style1'><a href='home.php' STYLE='TEXT-DECORATION:NONE'>Home</a></span></div></td>
<td><div align='center'><a href='berita_usr.php' STYLE='TEXT-DECORATION:NONE'>Berita</a></div></td>
<td><div align='center'><a href='logout_usr.php' STYLE='TEXT-DECORATION:NONE'>LogOut</a></div></td>
</tr>
</table>
<p> </p>";
}
?>
Setelah membuat form login kita belum bisa masuk dikarenakan tabel user masih kosong
sedangkan bila ingin memasukan data user melalui inputuser.php belum bisa dikarenakan harus login terlebih dahulu.
berikutnya kita buat form
inputusersementara.php
<?php
include "koneksi.php";
if(isset($_POST['simpan'])){
$username=$_POST['username'];
$password=md5($_POST['password']);
$password2=md5($_POST['password2']);
$nama_lengkap=$_POST['nama_lengkap'];
$email=$_POST['email'];
$telp_hp=$_POST['telp_hp'];
$level=$_POST['level'];
if(empty($username)||empty($password)||empty($password2)||empty($nama_lengkap)||empty($email)||empty($telp_hp)||empty($level))
{
echo "<script type='text/javascript'>
onload =function(){
alert('Semua data harus diisi!');
}
</script>";
}else{
$a="insert into user(username,password,nama_lengkap,email,telp_hp,level)values('$username','$password','$nama_lengkap','$email','$telp_hp','$level')";
$b=mysql_query($a);
if($b){
echo "<script>alert('Data berhasil disimpan!');javascript:history.go(-1);</script>";
}else{
echo "<script type='text/javascript'>
onload =function(){
alert('Data gagal disimpan');
}
</script>";
}
}
}
?>
<head>
<title>Administrator Area </title>
</head>
<body leftmargin=0 topmargin=0 marginheight="0" marginwidth="0" bgcolor="#ffffff">
<table border="0" cellspacing="0" cellpadding="0" width="100%" height="83%">
<tr>
<td width="50%" background="../gambar/bg.gif"><img src="../gambar/px1.gif" width="1" height="1" alt="" border="0"></td>
<td valign="bottom" background="../gambar/bg_left.gif"><img src="../gambar/bg_left.gif" alt="" width="17" height="16" border="0"></td>
<td><table width="780" border="0" cellspacing="0" cellpadding="0">
<tr>
</tr>
<tr>
<td background="../gambar/fon02.gif" height="34"><div align="center"></div></td>
</tr>
</table>
<p align="center" class="px">
\
<table width="644" border="0" align="center" cellpadding="1" cellspacing="0">
<tr>
<td width="549"><table width="628" border="0" align="center">
<tr>
<td width="622"><div align="center">
<h2><strong>input user</strong></h2>
</div>
<form action="" method="post" name="form1" id="form1">
<table width="553" border="0" align="center" cellpadding="1" cellspacing="0">
<tr>
<td width="149">Username</td>
<td width="400"><label><?php if(!$_GET['username']){
echo "<input name='username' type='text' id='username' size='20'>";
}else{
echo "<b>".$baris['username']."</b>";
}
?>
</label></td>
</tr>
<tr>
<td>Password</td>
<td><label>
<input name="password" type="password" id="password" size="25" />
</label>
<?php
if($_GET['username']){
echo "<br><font color='red'>Apabila password tidak diubah, silahkan dikosongkan saja</font>";
}
?> </td>
</tr>
<tr>
<td>Nama Lengkap </td>
<td><label>
<input name="nama_lengkap" type="text" id="nama_lengkap" size="35" value="<?php echo $baris['nama_lengkap'];?>"/>
</label></td>
</tr>
<tr>
<td>Email</td>
<td><label>
<input name="email" type="text" id="email" size="35" value="<?php echo $baris['email'];?>"/>
</label></td>
</tr>
<tr>
<td>Telpon/HP</td>
<td><label>
<input name="telp_hp" type="text" id="telp_hp" size="15" value="<?php echo $baris['telp_hp'];?>"/>
</label></td>
</tr>
<tr>
<td>Level User </td>
<td><label>
<select name="level" id="level">
<option value="admin" <?php if($baris['level']=="admin"){ echo "selected";}?>>Administrator</option>
<option value="user" <?php if($baris['level']=="user"){ echo "selected";}?>>User</option>
</select>
</label></td>
</tr>
<tr>
</tr>
<td> </td>
<td><label>
<input name="simpan" type="submit" id="simpan" value="simpan" />
</label> <label>
<input type="submit" name="Submit2" value="Batal" />
</label>
</tr>
</table>
</form>
<p align="center"> </p>
<p align="center"> </p></td>
</tr>
</table>
<h3 align="center"> </h3>
<h3 align="center">Tabel User </h3>
<table width="690" border="1" align="center" cellspacing="0">
<tr bgcolor="#FFFF99">
<td width="50"><div align="center"><strong>No</strong></div></td>
<td width="100"><div align="center"><strong>Username</strong></div></td>
<td width="150"><div align="center"><strong>Nama Lengkap</strong></div></td>
<td width="150"><div align="center"><strong>Email</strong></div></td>
<td width="100"><div align="center"><strong>Telephon/HP</strong></div></td>
<td width="50"><div align="center"><strong>Level</strong></div></td>
</tr>
<?php
$sql="select * from user order by nama_lengkap ASC";
$query=mysql_query($sql);
$no=1;
while($data=mysql_fetch_array($query)){
?>
<tr>
<td><div align="center"><?php echo $no; ?></div></td>
<td><?php echo $data['username']; ?></td>
<td><?php echo $data['nama_lengkap']; ?></td>
<td><?php echo $data['email']; ?></td>
<td><?php echo $data['telp_hp']; ?></td>
<td><?php echo $data['level']; ?></td>
<td><?php echo $data['status']; ?></td>
</tr>
<?php
$no++;
}?>
</table>
<p align="center"> </p>
<p align="center"><a href="logout.php"></a></p>
<p> </p>
</td>
</tr>
</table>
</body>
</html>
Lalu masukan data user melalui localhost
/namafolder/inputusersementara.php
setelah tabel user terisi data ketikan di jendela browser localhost/namafolder/login.php
Demikian
Cara Membuat form login dengan php mysql
Title : Membuat Form Login dengan Php Mysql
Description : Membuat form login dengan php mysql , Berikut ini saya akan membuat sebuah form login dengan php mysql. Langkah pertama kita harus sudah m...