PHP & MySQL – Membuat Auto LogOut Website


autologout
Bagaimana caranya agar User Logout secara otomatis saat ia lupa keluar dari akun sebuah website? berikut caranya, langsung aja scriptnya:
* index.php
<table border=”0″ align=”left”>
<form action=”login.php” method=”post”>
<tr><td>Username</td><td><input type=”text” name=”user” size=”25″></td></tr>
<tr><td>Password</td><td><input type=”password” name=”pass” size=”25″></td></tr>
<tr><td></td><td><input type=”submit” name=”log” value=”Login” ></td></tr>
</form>
</table>

*login.php
<?php
session_start();
include “../connection.php”;
if(isset($_SESSION[‘admin’])){
header(“Location: index.php”);
}

if(isset($_POST[‘log’])) {
$user = $_POST[‘user’];
$pass = $_POST[‘pass’];

$sql = mysql_query(“SELECT * FROM administrator WHERE user=’$user’ && pass=’$pass'”);
$num = mysql_num_rows($sql);  if($num==1) {
$_SESSION[‘admin’] = $user;
$_SESSION[‘start’] = time(); // taking now logged in time
$_SESSION[‘expire’] = $_SESSION[‘start’] + (30 * 60) ;
header(‘location:index.php’);
} else {  ?>
<script language=”JavaScript”>alert(‘Login gagal, Cek lagi username atau password anda.’);
document.location=’login_admin.php’;</script>
<?php  } } ?>

*home.php
<?php session_start();
if(!isset($_SESSION[‘admin’])){
?><script language=”javascript”>
alert(“Maaf, Anda tidak berhak mengakses halaman ini!!”);
document.location=”login_admin.php”;
</script>
<?php
}else{
$now = time(); // checking the time now when home page starts

if($now > $_SESSION[‘expire’])
{
session_destroy();
unset($_SESSION[‘admin’]);
?><script language=”javascript”>
alert(“Expired, Anda Harus login lagi”);
document.location=”login_admin.php”;
</script>
<?php
}
else{
?>
<html>
<head>
<link rel=”Shortcut Icon” href=”images/logo.jpg” type=”image/png”>
<title>Page Admin</title>
</head>
<body>
<center><h2>Welcome to Page Administrator<br>Code Enterprise [C.E]</h2></center>
</body>
</html>
<?php
}
}
?>


One thought on “PHP & MySQL – Membuat Auto LogOut Website

  1. Hello, I think your site might be having browser compatibility issues.

    When I look at your website in Safari, it looks fine
    but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that,
    superb blog!

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s