http://www.prcheckingtool.com

Minggu, 22 Mei 2011

Login dengan Batasan Akses (Otorisasi Admin-User)

Back again neh met with AaEzha.com. Gak bored right? : D
This time, we will discuss, is still on, log in again. The difference with the previous post, this time we will distinguish how the use the Administrator or a user authorization.

For its database and table, we still use the table that we created from a previous posting it:) stay plus a little bit, the field level.
About this log:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
mysql> alter table tbl_user add level varchar(15) not null;
Query OK, 1 row affected (1.26 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
mysql> desc tbl_user;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(2)      | NO   | PRI | NULL    | auto_increment |
| username | varchar(50) | NO   |     | NULL    |                |
| password | varchar(35) | NO   |     | NULL    |                |
| level    | varchar(15) | NO   |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.72 sec)
Then, we edit the levels aa username 'admin' and we created a new user with user-level again.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
mysql> update tbl_user set level='admin' where id=1;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
mysql> select * from tbl_user;
+----+----------+----------------------------------+-------+
| id | username | password                         | level |
+----+----------+----------------------------------+-------+
|  1 | aa       | 5f4dcc3b5aa765d61d8327deb882cf99 | admin |
+----+----------+----------------------------------+-------+
1 row in set (0.00 sec)
 
mysql> insert into tbl_user(username,password,level) values('bb', md5('password'),'user');
Query OK, 1 row affected (0.13 sec)
 
mysql> select * from tbl_user;
+----+----------+----------------------------------+-------+
| id | username | password                         | level |
+----+----------+----------------------------------+-------+
|  1 | aa       | 5f4dcc3b5aa765d61d8327deb882cf99 | admin |
|  2 | bb       | 5f4dcc3b5aa765d61d8327deb882cf99 | user  |
+----+----------+----------------------------------+-------+
2 rows in set (0.00 sec)
Okay, we just use the same table with the previous posting, but not to the files for example:)

Here are the files: D
this as index.php
01
02
03
04
05
06
07
08
09
10
11
<html>
<head><title>Login</title></head>
<body>
<h2>Halaman Login</h2>
<form action="log.php?op=in" method="post">
User ID : <input type="text" name="userid"><br>
Password : <input type="password" name="psw"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
then this log.php
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
session_start();
mysql_connect("localhost","root","12345") or die("Nggak bisa koneksi");
mysql_select_db("sekolah");//sesuaikan dengan nama database anda
 
$userid = $_POST['userid'];
$psw = $_POST['psw'];
$hash = md5($psw);
$op = $_GET['op'];
 
if($op=="in"){
    $cek = mysql_query("SELECT * FROM tbl_user WHERE username='$userid' AND password='$hash'");
    if(mysql_num_rows($cek)==1){//jika berhasil akan bernilai 1
        $c = mysql_fetch_array($cek);
        $_SESSION['userid'] = $c['userid'];
        $_SESSION['level'] = $c['level'];
        if($c['level']=="admin"){
            header("location:homeadmin.php");
        }else if($c['level']=="user"){
            header("location:homeuser.php");
        }
    }else{
         die("password salah <a href=\"javascript:history.back()\">kembali</a>");
    }
}else if($op=="out"){
    unset($_SESSION['userid']);
    unset($_SESSION['level']);
    header("location:index.php");
}
?>
ini homeuser.php
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
session_start();
 
//cek apakah user sudah login
if(!isset($_SESSION['userid'])){
    die("Anda belum login");//jika belum login jangan lanjut..
}
 
//cek level user
if($_SESSION['level']!="user"){
    die("Anda bukan user");//jika bukan user jangan lanjut
}
?>
 
<html>
<head><title>Halaman User</title></head>
<body>
<?php echo "<h3>Welcome ".$_SESSION['userid']."</h3>";?>
Menu :
<a href=halaman2.php>Halaman 2</a> |
<a href=halaman3.php>Halaman 3</a> |
<a href=log.php?op=out>Log Out</a>
 
</body>
</html>
this homeadmin.php
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
session_start();
 
//cek apakah user sudah login
if(!isset($_SESSION['userid'])){
    die("Anda belum login");//jika belum login jangan lanjut..
}
 
//cek level user
if($_SESSION['level']!="admin"){
    die("Anda bukan admin");//jika bukan admin jangan lanjut
}
?>
 
<html>
<head><title>Halaman Admin</title></head>
<body>
<?php echo "<h3>Welcome ".$_SESSION['userid']."</h3>";?>
Menu :
<a href=halaman1.php>Halaman 1</a> |
<a href=halaman2.php>Halaman 2</a> |
<a href=log.php?op=out>Log Out</a>
 
</body>
</html>
halaman1.php
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
session_start();
 
//cek apakah user sudah login
if(!isset($_SESSION['userid'])){
    die("Anda belum login");//jika belum login jangan lanjut..
}
 
//cek level user
if($_SESSION['level']!="admin"){
    die("Anda bukan admin");//jika bukan admin jangan lanjut
}
?>
 
<html>
<head><title>Halaman Admin</title></head>
<body>
<?php echo "<h3>Welcome ".$_SESSION['userid']."</h3>";?>
 
<h4>Ini Halaman 1</h4>
 
</body>
</html>
halaman2.php
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
session_start();
 
//cek apakah user sudah login
if(!isset($_SESSION['userid'])){
    die("Anda belum login");//jika belum login jangan lanjut..
}
 
//cek level user
if($_SESSION['level']!="admin" && $_SESSION['level']!="user"){
    die("Anda bukan admin");//jika bukan admin dan user jangan lanjut
}
?>
 
<html>
<head><title>Halaman Admin dan User</title></head>
<body>
<?php echo "<h3>Welcome ".$_SESSION['userid']."</h3>";?>
 
<h4>Ini Halaman 2</h4>
 
</body>
</html>
halaman3.php
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
session_start();
 
//cek apakah user sudah login
if(!isset($_SESSION['userid'])){
    die("Anda belum login");//jika belum login jangan lanjut..
}
 
//cek level user
if($_SESSION['level']!="user"){
    die("Anda bukan user");//jika bukan user jangan lanjut
}
?>
 
<html>
<head><title>Halaman User</title></head>
<body>
<?php echo "<h3>Welcome ".$_SESSION['userid']."</h3>";?>
 
<h4>Ini Halaman 3</h4>
 
</body>
</html>

Tidak ada komentar:

Posting Komentar