Belajar Membuat SESSION Sederhana dengan PHP
1/27/2019
Add Comment
Untuk itu agar dapat mengetahui kegunaan session, berikut tutorial tentang session.
Tutorial ini dibuat dengan Bootstrap 3.0 sebagai template
Terdapat 4 File diantaranya ;
- Index.php
- Session1.php
- Session2.php
- Session3.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title>Belajar Session</title>
<!-- Bootstrap core CSS -->
<link href="dist/css/bootstrap.min.css" rel="stylesheet">
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="starter-template.css" rel="stylesheet">
<!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
<!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
<script src="assets/js/ie-emulation-modes-warning.js"></script>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Session</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="container">
<div class="starter-template">
<!—ISI sintak PHP Disini -->
</div>
</div><!-- /.container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
Untuk file pertama buat file copypaste code html dasar lalu isi kode dibawah ini kemudian save dengan nama index.php;
<form action="session1.php" method="post">
<h2>login disini</h2>
Username: <input type="text" name="user"><br>
Password: <input type="password" name="pass"><br>
<input type="submit" name="login" value="Log in">
Kemudian buat lagi file session1.php isi seperti file index.php tapi ganti sintak PHP dengan sintak berikut ini;
<?php
session_start();
if (isset($_POST['login']))
{
$user=$_POST['user'];
$pass=$_POST['pass'];
if($user =="admin" && $pass="password");
{
$_SESSION['login']=$user;
echo"<h1>Anda Berhasil LOGIN!!!</h1>";
echo"<h2>klik <a href='session2.php'>disni(session2.php)</a> untuk menuju ke halaman pemerikasaan session</h2>";
}
?>
Buat lagi file session2.php isi dengan html dasar kemudian tambahkan sintak PHP dibawah ini;
<?php
session_start();
if(isset($_SESSION['login']))
{
echo"<h1>Selamt Datang ".$_SESSION['login']."</h1>";
echo"<h2>Login Dahulu</h2>";
echo"<h2>klik <a href='session3.php'> disni (session3.php) </a> untuk LOGOUT</h2>";
}else{
die("Anda belum login! Anda tidak berhak masuk kehalaman ini. Silahkan Login<a href='index.php'> disni</a>");
}
?>
Terakhir buat file session3.php , tambahkan sintak berikut;
<?php
session_start();
if(isset($_SESSION['login']))
{
unset($_SESSION);
session_destroy();
//
echo"<h1>Anda sudah berhasil LOGOUT </h1>";
echo"<h2>klik <a href='index.php'>disini</a>untuk LOGIN kembali</h2>";
echo"<h2>Anda sekarang tidak bisa masuk ke halaman <a href='session2.php'>session2.php</a>lagi</h2>";
}
?>
Berikut Hasilnya :
0 Response to "Belajar Membuat SESSION Sederhana dengan PHP"
Post a Comment