Setiap website pasti memiliki halaman Admin’a sendiri-sendiri. Halam admin ini sangat berguna bagi admin itu sendiri dalam hal memanipulasi data, seperti menambah data, memperbaiki data, atau menghapus data. Kali ini kita akan mempelajari bagaimana membuat halaman admin sederhana menggunakan PHP.
1.Persiapan Membuat Database terlebih dahulu
CREATE TABLE admin (
id int(10) unsigned NOT NULL auto_increment,
username varchar(80),
password varchar(10),
PRIMARY KEY (id)
);
2. Setelah itu buat Koneksi Database
<?php
function open_connection() {
$host=”localhost”;
$username=”root”;
$password=”";
$databasename=”gbadvance”;
$link=mysql_connect($host,$username,$password) or die (mysql_error());
mysql_select_db($databasename,$link);
return $link;
}
?>
<?phpfunction open_connection() {$host=”localhost”;$username=”root”;$password=”";
$databasename=”gbadvance”;$link=mysql_connect($host,$username,$password) or die (mysql_error());mysql_select_db($databasename,$link);return $link;}?>
3. Halaman Login Admin
<html><head><title>Admin</title></head>
<body bgcolor=”#CCFF66?>
<h2 align=left>Login Admin</h2>
<hr>
<form action=”admin2.php” method=POST>
<pre>
Username : <input type=text name=user size=20>
Password : <input type=password name=pass size=20>
</pre>
<input type=submit value=”Login”> <input type=reset value=”Reset”>
</form></body></html>
4. Session
<?php
session_start();
require(“conn.php”);
$link=open_connection();
$sqla=”SELECT * FROM admin WHERE username=’{$_POST['user']}’ AND
password=’{$_POST['pass']}’ “;
$res=mysql_query($sqla);
if (mysql_num_rows($res)==0)
{
echo(“Invalid username or password<br>Please Login again !”);
session_destroy();
} else {
$_SESSION['user'] = $_POST['user'];
echo “<center>Selamat datang, {$_SESSION['user']} !</center>”;
echo “Silakan <a href=admin3.php>Klik</a> untuk admin guestbook”;
}
?>
5. Halaman Tampil
<?php
session_start();
require(“conn.php”);
$link=open_connection();
if(isset($_SESSION['user']))
{
$sqlstr=”SELECT * FROM guestbook ORDER BY id DESC”;
$result = mysql_query ($sqlstr) or
die(mysql_error());
while ($row = mysql_fetch_assoc($result))
{
$id=$row['id'];
$name=$row['name'];
$posted=$row['posted'];
$email=$row['email'];
$address=$row['address'];
$city=$row['city'];
$msg=stripslashes($row['msg']);
echo(“Posted : $posted<br>”);
echo(“Name : $name<br>”);
echo(“E-Mail : <a href=mailto:$email>$email</a><br>”);
echo(“Address : $address<br>”);
echo(“City : $city<br>”);
echo(“Message :<br>”);
echo(“$msg<br>”);
echo(“<a href=edit.php?id=$id>Edit</a> | “.”<a href=delete.php?id=$id>Delete</a> | “.”<a href=logout.php>Logout</a>”);
echo(“<hr size=1 width=100%>”);
}
} else {
echo(“Anda bukan admin atau belum login”);
echo(“<a href=admin1.php>Login</a>”);
}
?>
terimaksih semoga bermanfaat :)