Ketik kode berikut dan simpan dengan nama login.php pada root direktori web server.
<?php
session_start();
if ( isset ($_POST["login"] ) ) {
login();
}elseif ( isset ($_GET["action"]) and $_GET["action"] == "logout") {
logout();
}elseif ( isset ( $_GET["username"])) {
displayPage();
}else {
displayLoginForm();
}
function login() {
$username = "lyla"; //isi dengan nama
$passwd = "1008107020034"; //isi dengan nim
if (isset ( $_POST ["username"]) and isset ( $_POST["password"])) {
echo $_POST["username"]." ".$username;
if ( $_POST["username"] == $username and $_POST["password"] == $passwd) {
$_SESSION["username"] = $username;
session_write_close();
header ("Location: login.php");
}else {
displayLoginForm ("username/password Tidak cocok");
}
}
}
function logout() {
unset($_SESSION["username"]);
session_write_close();
header("Location: login.php");
}
function displayPage() {
displayPageHeader();
?>
<p>Welcome, <strong><?php echo $_SESSION["username"] ?> </strong>Anda Sudah Berhasil Login.</p>
<p><a href="login.php?action=logout">Logout</a></p>
</body>
</html>
<?php
}
function displayLoginForm($message="") {
displayPageHeader();
?>
<?php if ($message) echo '<p class="error">'. $message .'</p>' ?>
<form action="login.php" method="post">
<div style="width: 30em;">
<label for="username">Username</label>
<input type="text" name="username" id="username" value=""/>
<label for="password">Password</label>
<input type="password" name="password" id="password" value=""/>
<div style="clear: both;">
<input type="submit" name="login" value="Login"/>
</div>
</div>
</form>
</body>
</html>
<?php
}
function displaypageHeader() {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtmll//DTD/xhtmll-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>A Login Form</title>
<link rel="stylesheet" type="text/css" href="common.css" />
<style type="text/css">
.error { background: #d33; color: white; padding: 0.2em; }
</style>
</head>
<body>
<h1>Silahkan Login</h1>
<?php
}
?>
Ketik kode berikut dan simpan dengan nama common.css pada root direktori web server
/* page body */
body { font-family: Arial, Helvetica, sans-serif; }
/* definition list */
dl { width: 100%; margin: 2em 0; padding: 0; clear: both; overflow: auto; }
dt { width: 30%; float: left; margin: 0; padding: 5px 9.9% 5px 0; border-top: 1px solid #DDDDB7; font-weight: bold; overflow: auto; clear: left; }
dd { width: 60%; float: left; margin: 0; padding: 6px 0 5px 0; border-top: 1px solid #DDDDB7; overflow: auto; }
/* heading */
h1 { font-weight: bold; margin: 35px 0 14px; color: #666; font-size: 1.5em; }
h2 { font-weight: bold; margin: 30px 0 12px; color: #666; font-size: 1.2em; }
h3 { font-weight: normal; margin: 30px 0 12px; color: #666; font-size: 1.2em; }
h4 { font-weight: bold; margin: 25px 0 12px; color: #666; font-size: 1.0em; }
h5 { font-weight: bold; margin: 25px 0 12px; color: #666; font-size: 0.9em; }
/* form */
label { display: block; float: left; clear: both; text-align: right; margin: 0.6em 5px 0 0; width: 40%; }
input, select, textarea { float: right; margin: 1em 0 0 0; width: 57%; }
input { border: 1px solid #666; }
input [type=radio], input [type=checkbox], input [type=submit], input [type=reset], input [type=button], input [type=image], { width: auto; }
Jika berhasil login maka akan tampil :
Jika gagal maka akan tampil:
0 komentar: