KEMBAR78
Pemrograman Web 9 - Input Form DB dan Session | PDF
<?php
// mulai session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// set variable pada session
$_SESSION["favcolor"] = “hijau";
$_SESSION["favanimal"] = “kucing";
echo “Variabel telah dibuat.";
?>
</body>
</html>
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// menampilkan variable yang dibuat sebelumnya
echo “Warna favorit adalah " . $_SESSION["favcolor"] .
".<br>";
echo “Binatang peliharaan favorit adalah " .
$_SESSION["favanimal"] . ".";
?>
</body>
</html>
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// menghapus semua variable yang di set
session_unset();
// menghapus/menghancurkan session
session_destroy();
?>
</body>
</html>
<?php
mysql_connect("localhost","root","") or die
("Gagal Terhubung Ke Database");
mysql_select_db(“dbtes") or die ("Database
Tidak Ditemukan");
?>
<form class="form-horizontal" method="post" name="pendaftaran" action="registrasi.php">
<div class="form-group">
<label class="control-label col-sm-2" for="name">Nama:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="nama" placeholder="masukkan nama" name="nama">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="name">Email:</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" placeholder="masukkan email" name="email">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="name">Username:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="username" placeholder="masukkan username" name="username">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" placeholder="masukkan password" name="password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input class="btn btn-primary" type="submit" name="submit" value="DAFTAR">
</div>
</div>
</form>
<?php
include "koneksi.php";
$nama = $_POST['nama'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($nama)){
echo "<script>alert('Nama belum diisi')</script>";
echo "<meta http-equiv='refresh' content='1 url=daftar.php'>";
}else
if (empty($email)){
echo "<script>alert('Email belum diisi')</script>";
echo "<meta http-equiv='refresh' content='1 url=daftar.php'>";
}else
if(empty($username)){
echo "<script>alert('Username belum diisi')</script>";
echo "<meta http-equiv='refresh' content='1 url=daftar.php'>";
}else
if (empty($password)){
echo "<script>alert('Password belum diisi')</script>";
echo "<meta http-equiv='refresh' content='1 url=daftar.php'>";
}else{
$daftar = mysql_query("INSERT INTO pengguna (id_user,nama,username,password) values
('$nama','$email','$username','$password')");
if ($daftar){
echo "<script>alert('Berhasil Mendaftar')</script>";
echo "<meta http-equiv='refresh' content='1 url=daftar.php'>";
}else{
echo "<script>alert('Gagal Mendaftar')</script>";
echo "<meta http-equiv='refresh' content='1 url=daftar.php'>";
}
}
?>
<?php
include "koneksi.php";
session_start();
if (isset($_SESSION['username'])){
header ("location:index.php");
}
?>
<div class="container">
<div class="page-header">
<h1><b>Form Masuk Akun</b></h1>
<br />
</div>
<br />
<form class="form-horizontal" method="post" name="login" action="logincek.php">
<div class="form-group">
<label class="control-label col-sm-2" for="email">Username:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="username" placeholder="masukkan username" name="username">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" placeholder="Enter password" name="password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" value="LOGIN">Masuk</button>
</div>
</div>
</form>
</div>
<?php
include "koneksi.php";
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username)){
echo "<script>alert('Username belum diisi')</script>";
echo "<meta http-equiv='refresh' content='1 url=login.php'>";
}else if (empty($password)){
echo "<script>alert('Password belum diisi')</script>";
echo "<meta http-equiv='refresh' content='1 url=login.php'>";
}else{
session_start();
$login = mysql_query("select * from pengguna where
username='$username' and password='$password'");
if (mysql_num_rows($login) > 0){
$_SESSION['username'] = $username;
header("location:index.php");
}else{
echo "<script>alert('Username atau Password salah')</script>";
echo "<meta http-equiv='refresh' content='1 url=login.php'>";
}
}
?>
<?php
include "koneksi.php";
session_start();
if (!isset($_SESSION['username'])){
header ("location:login.php");
}
?>
Berhasil Login, <a href="logout.php">Logout</a>
<?php
session_start();
session_destroy();
echo "<script>alert('Terima kasih, Anda Berhasil
Logout')</script>";
echo "<meta http-equiv='refresh' content='1
url=login.php'>";
?>

Pemrograman Web 9 - Input Form DB dan Session

  • 3.
    <?php // mulai session session_start(); ?> <!DOCTYPEhtml> <html> <body> <?php // set variable pada session $_SESSION["favcolor"] = “hijau"; $_SESSION["favanimal"] = “kucing"; echo “Variabel telah dibuat."; ?> </body> </html>
  • 5.
    <?php session_start(); ?> <!DOCTYPE html> <html> <body> <?php // menampilkanvariable yang dibuat sebelumnya echo “Warna favorit adalah " . $_SESSION["favcolor"] . ".<br>"; echo “Binatang peliharaan favorit adalah " . $_SESSION["favanimal"] . "."; ?> </body> </html>
  • 7.
    <?php session_start(); ?> <!DOCTYPE html> <html> <body> <?php // menghapussemua variable yang di set session_unset(); // menghapus/menghancurkan session session_destroy(); ?> </body> </html>
  • 10.
    <?php mysql_connect("localhost","root","") or die ("GagalTerhubung Ke Database"); mysql_select_db(“dbtes") or die ("Database Tidak Ditemukan"); ?>
  • 11.
    <form class="form-horizontal" method="post"name="pendaftaran" action="registrasi.php"> <div class="form-group"> <label class="control-label col-sm-2" for="name">Nama:</label> <div class="col-sm-10"> <input type="text" class="form-control" id="nama" placeholder="masukkan nama" name="nama"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="name">Email:</label> <div class="col-sm-10"> <input type="email" class="form-control" id="email" placeholder="masukkan email" name="email"> </div> </div>
  • 12.
    <div class="form-group"> <label class="control-labelcol-sm-2" for="name">Username:</label> <div class="col-sm-10"> <input type="text" class="form-control" id="username" placeholder="masukkan username" name="username"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="pwd">Password:</label> <div class="col-sm-10"> <input type="password" class="form-control" id="password" placeholder="masukkan password" name="password"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <input class="btn btn-primary" type="submit" name="submit" value="DAFTAR"> </div> </div> </form>
  • 13.
    <?php include "koneksi.php"; $nama =$_POST['nama']; $email = $_POST['email']; $username = $_POST['username']; $password = $_POST['password']; if (empty($nama)){ echo "<script>alert('Nama belum diisi')</script>"; echo "<meta http-equiv='refresh' content='1 url=daftar.php'>"; }else if (empty($email)){ echo "<script>alert('Email belum diisi')</script>"; echo "<meta http-equiv='refresh' content='1 url=daftar.php'>"; }else
  • 14.
    if(empty($username)){ echo "<script>alert('Username belumdiisi')</script>"; echo "<meta http-equiv='refresh' content='1 url=daftar.php'>"; }else if (empty($password)){ echo "<script>alert('Password belum diisi')</script>"; echo "<meta http-equiv='refresh' content='1 url=daftar.php'>"; }else{ $daftar = mysql_query("INSERT INTO pengguna (id_user,nama,username,password) values ('$nama','$email','$username','$password')"); if ($daftar){ echo "<script>alert('Berhasil Mendaftar')</script>"; echo "<meta http-equiv='refresh' content='1 url=daftar.php'>"; }else{ echo "<script>alert('Gagal Mendaftar')</script>"; echo "<meta http-equiv='refresh' content='1 url=daftar.php'>"; } } ?>
  • 15.
    <?php include "koneksi.php"; session_start(); if (isset($_SESSION['username'])){ header("location:index.php"); } ?> <div class="container"> <div class="page-header"> <h1><b>Form Masuk Akun</b></h1> <br /> </div> <br /> <form class="form-horizontal" method="post" name="login" action="logincek.php"> <div class="form-group"> <label class="control-label col-sm-2" for="email">Username:</label>
  • 16.
    <div class="col-sm-10"> <input type="text"class="form-control" id="username" placeholder="masukkan username" name="username"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="pwd">Password:</label> <div class="col-sm-10"> <input type="password" class="form-control" id="password" placeholder="Enter password" name="password"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default" value="LOGIN">Masuk</button> </div> </div> </form> </div>
  • 17.
    <?php include "koneksi.php"; $username =$_POST['username']; $password = $_POST['password']; if (empty($username)){ echo "<script>alert('Username belum diisi')</script>"; echo "<meta http-equiv='refresh' content='1 url=login.php'>"; }else if (empty($password)){ echo "<script>alert('Password belum diisi')</script>"; echo "<meta http-equiv='refresh' content='1 url=login.php'>"; }else{
  • 18.
    session_start(); $login = mysql_query("select* from pengguna where username='$username' and password='$password'"); if (mysql_num_rows($login) > 0){ $_SESSION['username'] = $username; header("location:index.php"); }else{ echo "<script>alert('Username atau Password salah')</script>"; echo "<meta http-equiv='refresh' content='1 url=login.php'>"; } } ?>
  • 19.
    <?php include "koneksi.php"; session_start(); if (!isset($_SESSION['username'])){ header("location:login.php"); } ?> Berhasil Login, <a href="logout.php">Logout</a>
  • 20.
    <?php session_start(); session_destroy(); echo "<script>alert('Terima kasih,Anda Berhasil Logout')</script>"; echo "<meta http-equiv='refresh' content='1 url=login.php'>"; ?>