Untuk mengenal dan memahami lebih jauh tentang session pada php. buatlah project berikut ini
Langkah Pertama membuat file setup.php untuk membuat database:
setup.php
<html>
<head>
<title>Setup Database</title>
</head>
<body>
<?php
$cn = @mysql_connect("localhost", "root", "") or die ("Gagal konek ke MySQL.");
mysql_query("drop database if exists db_user", $cn) or die ("Gagal menghapus database.");
mysql_query("create database db_user", $cn) or die ("Gagal membuat database.");
mysql_select_db("db_user", $cn) or die ("Gagal membuka database.");
mysql_query("create table tb_user (
login_username varchar(32) not null,
login_password char (32),
login_rights varchar (32),
primary key(login_username)
)", $cn) or die("Gagal membuat table.");
mysql_query("insert into tb_user values('admin', md5('admin'), '[A] [U]')", $cn);
mysql_query("insert into tb_user values('user', md5('user'), '[U]'", $cn);
mysql_close($cn);
?>
<p>Sukses meng-setup database!<br/>
username: <b>admin</b> dan Password: <b>admin</b>
username: <b>user</b> dan Password: <b>user</b>
</p>
<p>Silakan menuju <a href="index.php">index.php</a></p>
</body>
</html>
<head>
<title>Setup Database</title>
</head>
<body>
<?php
$cn = @mysql_connect("localhost", "root", "") or die ("Gagal konek ke MySQL.");
mysql_query("drop database if exists db_user", $cn) or die ("Gagal menghapus database.");
mysql_query("create database db_user", $cn) or die ("Gagal membuat database.");
mysql_select_db("db_user", $cn) or die ("Gagal membuka database.");
mysql_query("create table tb_user (
login_username varchar(32) not null,
login_password char (32),
login_rights varchar (32),
primary key(login_username)
)", $cn) or die("Gagal membuat table.");
mysql_query("insert into tb_user values('admin', md5('admin'), '[A] [U]')", $cn);
mysql_query("insert into tb_user values('user', md5('user'), '[U]'", $cn);
mysql_close($cn);
?>
<p>Sukses meng-setup database!<br/>
username: <b>admin</b> dan Password: <b>admin</b>
username: <b>user</b> dan Password: <b>user</b>
</p>
<p>Silakan menuju <a href="index.php">index.php</a></p>
</body>
</html>
Kedua Membuat File Halaman Utama :
index.php
<html>
<head>
<title>Homepage</title>
</head>
<body>
<P>
<a href="index.php">Home</a> |
<a href="login.php">Login</a> |
<a href="member.php">Member</a> |
<a href="logout.php">Logout</a>
</P>
<p>Selamat datang. Di sini semua orang bisa mengakses.</p>
</body>
</html>
<head>
<title>Homepage</title>
</head>
<body>
<P>
<a href="index.php">Home</a> |
<a href="login.php">Login</a> |
<a href="member.php">Member</a> |
<a href="logout.php">Logout</a>
</P>
<p>Selamat datang. Di sini semua orang bisa mengakses.</p>
</body>
</html>
Ketiga membuat file yang hanya dapat diakses untuk member saja :
member.php
<?php
session_start();
$username = "";
$password = "";
if (isset($_SESSION['username']) && isset($_SESSION['password'])) {
$un = $_SESSION['username'];
$pw = md5($_SESSION['password']);
$cn = @mysql_connect("localhost", "root", "") or die ("Gagal konek ke MySQL.");
mysql_select_db("db_user") or die ("Gagal memilih database.");
$query = "select * from tb_user where
login_username ='$un' and login_password = '$pw'";
$hasil = mysql_query($query) or die ("Gagal melakukan query.");
if ($hasil !=false) {
if (mysql_num_rows($hasil) ==1) {
$username = $_SESSION['username'];
$password = $_SESSION['password'];
};
};
};
?>
<html>
<head>
<title>Members Only</title>
</head>
<body>
<p>
<a href="index.php">Home</a> |
<a href="member.php">login</a> |
<a href="member.php">member</a> |
<a href="logout.php">logout</a>
</p>
<?php
if (empty($username) || empty($password)) {
echo "<p>Maaf, hanya members yang boleh mengakses halaman ini.</p>";
}
else {
?>
<p>
Selamat datang <b><?php echo $username; ?></b>
</p>
<?php
};
?>
</body>
</html>
session_start();
$username = "";
$password = "";
if (isset($_SESSION['username']) && isset($_SESSION['password'])) {
$un = $_SESSION['username'];
$pw = md5($_SESSION['password']);
$cn = @mysql_connect("localhost", "root", "") or die ("Gagal konek ke MySQL.");
mysql_select_db("db_user") or die ("Gagal memilih database.");
$query = "select * from tb_user where
login_username ='$un' and login_password = '$pw'";
$hasil = mysql_query($query) or die ("Gagal melakukan query.");
if ($hasil !=false) {
if (mysql_num_rows($hasil) ==1) {
$username = $_SESSION['username'];
$password = $_SESSION['password'];
};
};
};
?>
<html>
<head>
<title>Members Only</title>
</head>
<body>
<p>
<a href="index.php">Home</a> |
<a href="member.php">login</a> |
<a href="member.php">member</a> |
<a href="logout.php">logout</a>
</p>
<?php
if (empty($username) || empty($password)) {
echo "<p>Maaf, hanya members yang boleh mengakses halaman ini.</p>";
}
else {
?>
<p>
Selamat datang <b><?php echo $username; ?></b>
</p>
<?php
};
?>
</body>
</html>
Keempat membuat form login
login.php
<?php
session_start();
$username = "";
$password = "";
if (isset($_POST['username']) && isset($_POST['password'])) {
$un = $_POST['username'];
$pw = md5($_POST['password']);
$cn = @mysql_connect("localhost", "root", "") or die ("Gagal konek ke mysql.");
mysql_select_db("db_user") or die ("Gagal memilih database.");
$query = "select * from tb_user where
login_username = '$un' and login_password = '$pw'";
$hasil = mysql_query($query) or die ("Gagal melakukan query.");
if ($hasil !=false) {
if (mysql_num_rows($hasil) ==1) {
$username = $_POST['username'];
$password = $_POST['Password'];
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
};
};
};
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<p>
<a href="index.php">Home</a> |
<a href="login.php">Login</a> |
<a href="member.php">Member</a> |
<a href="logout.php">Logout</a>
</p>
<p>
<?php
if (empty($username)) {
if (isset($_POST['username']) || isset($_POST['password'])) {
echo "<p>Username dan pasaword yang anda ketik adalah salah!</p>";
};
?>
<form name="login_form" method="post" action="login.php">
username: <input name="username" size="32" maxleght="32" ><br/>
password: <input name="password" type="password" maxleght="32" ><br/>
<input type="submit" name="button" value="submit" >
</form>
<?php
}
else {
?>
<p>Anda telah berhasil melakukan login.</p>
<?php
};
?>
</p>
</body>
</html>
session_start();
$username = "";
$password = "";
if (isset($_POST['username']) && isset($_POST['password'])) {
$un = $_POST['username'];
$pw = md5($_POST['password']);
$cn = @mysql_connect("localhost", "root", "") or die ("Gagal konek ke mysql.");
mysql_select_db("db_user") or die ("Gagal memilih database.");
$query = "select * from tb_user where
login_username = '$un' and login_password = '$pw'";
$hasil = mysql_query($query) or die ("Gagal melakukan query.");
if ($hasil !=false) {
if (mysql_num_rows($hasil) ==1) {
$username = $_POST['username'];
$password = $_POST['Password'];
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
};
};
};
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<p>
<a href="index.php">Home</a> |
<a href="login.php">Login</a> |
<a href="member.php">Member</a> |
<a href="logout.php">Logout</a>
</p>
<p>
<?php
if (empty($username)) {
if (isset($_POST['username']) || isset($_POST['password'])) {
echo "<p>Username dan pasaword yang anda ketik adalah salah!</p>";
};
?>
<form name="login_form" method="post" action="login.php">
username: <input name="username" size="32" maxleght="32" ><br/>
password: <input name="password" type="password" maxleght="32" ><br/>
<input type="submit" name="button" value="submit" >
</form>
<?php
}
else {
?>
<p>Anda telah berhasil melakukan login.</p>
<?php
};
?>
</p>
</body>
</html>
Kelima atau yang terakhir membuat file logout
logout.php
<?php
session_start();
unset($_SESSION['username']);
unset($_SESSION['password']);
?>
<html>
<head>
<title>logout</title>
</head>
<body>
<p>
<a href="index.php">Home</a> |
<a href="login.php">Login</a> |
<a href="memeber.php">Member</a> |
<a href="logout.php">Logout</a>
</p>
<p>
okee...Anda sudah logout.
</p>
</body>
</html>
session_start();
unset($_SESSION['username']);
unset($_SESSION['password']);
?>
<html>
<head>
<title>logout</title>
</head>
<body>
<p>
<a href="index.php">Home</a> |
<a href="login.php">Login</a> |
<a href="memeber.php">Member</a> |
<a href="logout.php">Logout</a>
</p>
<p>
okee...Anda sudah logout.
</p>
</body>
</html>
Semoga Bermanfaat!
Source : http://wwdq.blogspot.com/2011/05/fungsi-session-pada-php-dan-cara.html
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :