Membuat Form Login dan Logout dengan PHP

Standard

Untuk Username : Admin dan Password: 123

Code untuk Form Login, simpan dengan nama login.php :

<?php
session_start();
session_destroy();
$user = isset($_REQUEST['user']) ? $_REQUEST['user'] : '';
$pass = isset($_REQUEST['pass']) ? $_REQUEST['pass'] : '';
if ($user=="" || $pass=="")
{
$err="1";
}
else
{
$err="2";
}
if ($err=="1")
$pesan="<script>alert('Silahkan Masukkan Username dan Password Anda');</script>";
if ($err=="2")
{
//$nm="admin"; $pw="123";

if ($user=="admin" && $pass=="123")
{
session_start();
$_SESSION['user']="admin";
$_SESSION['pass']="123";
header("location:index.php");
}
else
{
$err="3";
}
}
if ($err=="3")
$pesan="<script>alert('Silahkan Masukkan Username dan Password Anda');</script>";
?>
<html>
<head>
<title>Login Form</title>
<style type="text/css">
<!--
.style3 {font-family: Geneva, Arial, Helvetica, sans-serif; font-weight: bold; }
.style5 {font-family: Geneva, Arial, Helvetica, sans-serif; font-weight: bold; color: #FFFFFF; }
-->
</style>
</head>
<body>
<form id="form1" name="form1" action="">
<table width="40%" border="0" align="center">
<tr>
<td colspan="2" align="center" bgcolor="#000000"><span>Login</span></td>
</tr>
<tr>
<td width="35%"><span>Username</span></td>
<td width="65%"><input name="user" type="text" id="user" value="<?php echo $user; ?>" /></td>
</tr>
<tr>
<td><span>Password</span></td>
<td><input name="pass" type="password" id="pass" value="<?php echo $pass; ?>" /></td>
</tr>
<tr>
<td> </td>
<td><input name="Submit" type="submit" value="Submit" /><?php echo $pesan; ?></td>
</tr>
</table>
</form>
</body>
</html>

Code untuk Form Logout, simpan dengan nama index.php :

<?php
session_start();
if (empty($_SESSION["user"]))
{
header("location:login.php");
}
else
{
?>

<html>
<head>
<title>index</title>
</head>
<body>
Welcome
<a href="login.php">Logout</a>
</body>
</html>
<?php
}
?>

Selamat Mencoba, semoga bermanfaat :)

About these ads

3 responses »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s