Close it

Jumat, 01 April 2011

0 Membuat Proteksi Halaman Web►► URL : http://wwdq.blogspot.com/2011/04/membuat-proteksi-halaman-web.html.

Share
Membuat proteksi halaman web yang di maksud adalah,memproteksi halaman atau page pada web dengan password, sehingga apabila ada user yang mengakses halamaman/page web tersebut user di minta untuk memasukan user dan password. Mari sama-sama kita lihat script dibawah ini :

Buat File pass.php lalu masukan kode dibawah ini :

<?php

$LOGIN_INFORMATION = array(
'admin1' => 'root',
'admin' => 'adminpass'
);

define('USE_USERNAME', true);

define('LOGOUT_URL', 'http://www.example.com/');


define('TIMEOUT_MINUTES', 0);



///////////////////////////////////////////////////////
// jangan rubah code di bawah ini
///////////////////////////////////////////////////////

// show usage example
if(isset($_GET['help'])) {
die('Include following code into every page you would like to protect, at the very beginning (first line):<br>&lt;?php include("' . str_replace('\\','\\\\',__FILE__) . '"); ?&gt;');
}

// timeout in seconds
$timeout = (TIMEOUT_MINUTES == 0 ? 0 : time() + TIMEOUT_MINUTES * 60);

// logout?
if(isset($_GET['logout'])) {
setcookie("verify", '', $timeout, '/'); // clear password;
header('Location: ' . LOGOUT_URL);
exit();
}

if(!function_exists('showLoginPasswordProtect')) {

// show login form
function showLoginPasswordProtect($error_msg) {
?>
<html>
<head>
<title>Please enter password to access this page</title>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</head>
<body>
<style>
input { border: 1px solid black; }
</style>
<div style="width:500px; margin-left:auto; margin-right:auto; text-align:center">
<form method="post">
<h3>Please enter password to access this page</h3>
<font color="red"><?php echo $error_msg; ?></font><br />
<?php if (USE_USERNAME) echo 'Login:<br /><input type="input" name="access_login" /><br />Password:<br />'; ?>
<input type="password" name="access_password" /><p></p><input type="submit" name="Submit" value="Submit" />
</form>

</div>
</body>
</html>

<?php
// stop at this point
die();
}
}

// user provided password
if (isset($_POST['access_password'])) {

$login = isset($_POST['access_login']) ? $_POST['access_login'] : '';
$pass = $_POST['access_password'];
if (!USE_USERNAME && !in_array($pass, $LOGIN_INFORMATION)
|| (USE_USERNAME && ( !array_key_exists($login, $LOGIN_INFORMATION) || $LOGIN_INFORMATION[$login] != $pass ) )
) {
showLoginPasswordProtect("Incorrect password.");
}
else {
// set cookie if password was validated
setcookie("verify", md5($login.'%'.$pass), $timeout, '/');

// Some programs (like Form1 Bilder) check $_POST array to see if parameters passed
// So need to clear password protector variables
unset($_POST['access_login']);
unset($_POST['access_password']);
unset($_POST['Submit']);
}

}

else {

// check if password cookie is set
if (!isset($_COOKIE['verify'])) {
showLoginPasswordProtect("");
}

// check if cookie is good
$found = false;
foreach($LOGIN_INFORMATION as $key=>$val) {
$lp = (USE_USERNAME ? $key : '') .'%'.$val;
if ($_COOKIE['verify'] == md5($lp)) {
$found = true;
// prolong timeout
if (TIMEOUT_CHECK_ACTIVITY) {
setcookie("verify", md5($lp), $timeout, '/');
}
break;
}
}
if (!$found) {
showLoginPasswordProtect("");
}

}

?>

*Keterangan : kode yang berwarna biru, boleh diganti sesuai kebutuhan.
include file pass.php pada halaman page yang ingin di proteksi dengan script sebagai berikut :

<?php
include('pass.php)
?>

Post Info :
Judul = Membuat Proteksi Halaman Web

Url = http://wwdq.blogspot.com/2011/04/membuat-proteksi-halaman-web.html

Author = Rizal Loa Wanda. At : 09.03 Jumat, 01 April 2011 | Comment:0

Artikel Terkait

Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

0 Comment: on "Membuat Proteksi Halaman Web"

Posting Komentar

No Spam Please :)


Tambahkan Bookmark halaman ini untuk mempermudah Pencarian Anda [CTRL+D].