EAS PWeb - Web PPDB

Nama: Donny Fitrado
NRP: 05111740000171
Kelas: PWeb C

Untuk memenuhi UAS PWeb, saya diberi tugas untuk membuat website PPDB.

1. Rancangan Databasenya:
2. Rancangan Interface Aplikasi:



3. Source code:
  • config.php
  <?php    
  $server = "localhost";    
  $user = "dfitrado";    
  $password = "donny123";    
  $nama_database = "uaspweb_ppdb";    
  $db = mysqli_connect($server, $user, $password, $nama_database);    
  if( !$db ){    
   die("Gagal terhubung dengan database: " . mysqli_connect_error());    
  }    
  ?>    

  • download.php
 <?php    
  //export.php    
  $connect = mysqli_connect("localhost", "dfitrado", "donny123", "uaspweb_ppdb");    
  $output = '';    
  if(isset($_POST["download"]))    
  {    
  $query = "SELECT * FROM calon_siswa";    
  $result = mysqli_query($connect, $query);    
  if(mysqli_num_rows($result) > 0)    
  {    
  $output .= '    
   <table class="table" bordered="1">    
     <tr>    
     <th>ID</th>    
     <th>Nama</th>    
     <th>Alamat</th>    
     <th>Jenis_Kelamin</th>    
     <th>Agama</th>    
     <th>Sekolah_asal</th>    
     <th>Sekolah_pilihan</th>    
     </tr>    
  ';    
  while($row = mysqli_fetch_array($result))    
  {    
   $output .= '    
   <tr>    
     <td>'.$row["id"].'</td>    
     <td>'.$row["nama"].'</td>    
     <td>'.$row["alamat"].'</td>    
     <td>'.$row["jenis_kelamin"].'</td>    
     <td>'.$row["agama"].'</td>    
     <td>'.$row["sekolah_asal"].'</td>    
     <td>'.$row["sekolah_pilihan"].'</td>    
     </tr>    
   ';    
  }    
  $output .= '</table>';    
  header('Content-Type: application/xls');    
  header('Content-Disposition: attachment; filename=download.xls');    
  echo $output;    
  }    
  }    
  ?>    

  • errors.php
  <?php    
  //export.php    
  $connect = mysqli_connect("localhost", "dfitrado", "donny123", "uaspweb_ppdb");    
  $output = '';    
  if(isset($_POST["download"]))    
  {    
  $query = "SELECT * FROM calon_siswa";    
  $result = mysqli_query($connect, $query);    
  if(mysqli_num_rows($result) > 0)    
  {    
  $output .= '    
   <table class="table" bordered="1">    
     <tr>    
     <th>ID</th>    
     <th>Nama</th>    
     <th>Alamat</th>    
     <th>Jenis_Kelamin</th>    
     <th>Agama</th>    
     <th>Sekolah_asal</th>    
     <th>Sekolah_pilihan</th>    
     </tr>    
  ';    
  while($row = mysqli_fetch_array($result))    
  {    
   $output .= '    
   <tr>    
     <td>'.$row["id"].'</td>    
     <td>'.$row["nama"].'</td>    
     <td>'.$row["alamat"].'</td>    
     <td>'.$row["jenis_kelamin"].'</td>    
     <td>'.$row["agama"].'</td>    
     <td>'.$row["sekolah_asal"].'</td>    
     <td>'.$row["sekolah_pilihan"].'</td>    
     </tr>    
   ';    
  }    
  $output .= '</table>';    
  header('Content-Type: application/xls');    
  header('Content-Disposition: attachment; filename=download.xls');    
  echo $output;    
  }    
  }    
  ?>    

  • form-daftar.php
 <!DOCTYPE html>   
  <html>   
  <head>   
   <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">   
   <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">   
   <style type="text/css">   
    .iconform {   
     float: left;   
     text-align: center;   
     width: 35px;   
     height: 42px;   
     line-height: 40px;   
    }   
   </style>   
  </head>   
  <body>   
   <div class="container m-5">   
     <header>   
      <h3>Formulir Pendaftaran Siswa Baru</h3>   
     </header>   
     <form class="form-horizontal" action="proses-pendaftaran.php" >   
      <div class="form-row">    
       <div class="form-group col-md-5">   
        <label class="control-label col-xs-3" for="nama">Nama: </label>   
        <input class="form-control" name="name" id="nama" type="text" placeholder="Nama Lengkap" />   
       </div>    
      </div>   
      <div class="form-row">     
       <div class="form-group col-md-5">   
        <label class="control-label" for="alamat">Alamat: </label>   
        <input class="form-control" name="alamat" id="alamat" placeholder="Alamat"></input>   
       </div>   
      </div>   
      <div class="form-group">   
       <div class="form-check form-check-inline">   
        <input class="form-check-input" type="radio" name="jenis_kelamin" id="jenisL" type="radio" value="Laki-laki">   
        <label class="iconform"><i class="fas fa-male fa-2x"></i></label>   
       </div>   
       <div class="form-check form-check-inline">   
        <input class="form-check-input" type="radio" name="jenis_kelamin" id="jenisP" type="radio" value="Perempuan">   
        <label class="iconform" ><i class="fas fa-female fa-2x"></i></label>   
       </div>    
      </div>    
      <div class="form-group">   
       <label for="agama">Agama: </label>   
        <select class="form-control col-md-2" name="agama" id="agama" >   
        <option>Islam</option>   
        <option>Kristen</option>   
        <option>Hindu</option>   
        <option>Budha</option>   
        <option>Atheis</option>   
        </select>   
      </div>    
      <div class="form-row">   
       <div class="form-group col-md-5">   
        <label for="sekolah_asal">Sekolah Asal: </label>   
        <input class="form-control" name="sekolah_asal" id="sekolah_asal" type="text" placeholder="Nama Sekolah" />   
       </div>   
      </div>   
      <div class="form-row">   
       <div class="form-group col-md-5">   
        <label for="sekolah_pilihan">Sekolah Pilihan: </label>   
        <input class="form-control" name="sekolah_pilihan" id="sekolah_pilihan" type="text" placeholder="Sekolah Pilihan" />   
       </div>   
      </div>   
      <div class="form-group">   
       <input class="btn btn-primary" type="button" value="Daftar" name="daftar" onclick="insert()" />   
      </div>    
     </form>   
   </div>   
  </body>   
  </html>   

  • form-edit.php
 <?php   
  include("config.php");   
  if( !isset($_GET['id']) ){   
   header('Location: list-siswa.php');   
  }   
  $id = $_GET['id'];   
  $sql = "SELECT * FROM calon_siswa WHERE id=$id";   
  $query = mysqli_query($db, $sql);   
  $siswa = mysqli_fetch_assoc($query);   
  ?>   
  <!DOCTYPE html>   
  <html>   
  <head>    
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">   
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">   
   <style type="text/css">   
   body{   
    background-image: url("download.jpg");   
    background-repeat: no-repeat;   
    background-size: cover;   
   }   
   </style>   
  </head>   
  <body>   
   <div class="container"><br>   
   <h2>Formulit Edit Siswa</h2>   
   <form class="form-horizontal" action="proses-edit.php">   
    <fieldset>   
     <input type="hidden" name="id" value="<?php echo $siswa['id'] ?>" />   
    <div class="form-row">   
     <div class="form group col-md-5">   
      <label class="control-label" for="nama">Nama: </label>   
      <input class="form-control" type="text" id="nama" name="nama" placeholder="Nama Lengkap" value="<?php echo $siswa['nama'] ?>">   
     </div>   
     <div class="col-md-5">   
      <label class="control-label" for="alamat">Alamat: </label>   
      <input class="form-control" id="alamat" name="alamat" value="<?php echo $siswa['alamat'] ?>"></input>   
     </div>   
    </div><br>   
    <div class="form-group">   
     <div class="form-check form-check-inline">   
      <input class="form-check-input" type="radio" name="jenis_kelamin" id="jenisL" type="radio" value="<?php echo ($siswa['jenis_kelamin'] == 'Laki-laki') ? "checked": "" ?>">   
      <label class="iconform"><i class="fas fa-male fa-2x"></i></label>   
     </div>   
     <div class="form-check form-check-inline">   
      <input class="form-check-input" type="radio" name="jenis_kelamin" id="jenisP" type="radio" value="<?php echo ($siswa['jenis_kelamin'] == 'Perempuan') ? "checked": "" ?>">   
      <label class="iconform" ><i class="fas fa-female fa-2x"></i></label>   
     </div>    
    </div>    
    <div class="form-group">   
     <label style="margin-right: 1%" for="agama">Agama: </label>   
      <select class="form-control col-md-2" name="agama" id="agama" >   
       <option>Islam</option>   
       <option>Kristen</option>   
       <option>Hindu</option>   
       <option>Budha</option>   
       <option>Atheis</option>   
      </select>   
    </div>    
    <div class="form-row">   
     <div class="col-md-5">   
      <label for="sekolah_asal">Sekolah Asal: </label>   
      <input class="form-control" id="sekolah_asal" type="text" name="sekolah_asal" placeholder="Sekolah Asal" value="<?php echo $siswa['sekolah_asal'] ?>" />   
     </div>   
    </div>   
    <br>   
    <div class="form-group">   
     <input class="btn btn-primary" type="button" value="Simpan" name="simpan"    
     onclick="update()" />   
    </div>   
    </fieldset>   
   </form>   
   </div>   
  </body>   
  </html>   
  <script type="text/javascript">   
    function update(){   
     var nama = document.getElementById("nama").value;   
     var alamat = document.getElementById("alamat").value;   
     var jenis_kelamin;   
     if(document.getElementById("jenisL").checked){   
      jenis_kelamin = "Laki-laki";   
     }   
     else{   
      jenis_kelamin = "Perempuan";   
     }   
     var agama = document.getElementById("agama").value;   
     var sekolah_asal = document.getElementById("sekolah_asal").value;   
     var sekolah_pilihan = document.getElementById("sekolah_pilihan").value;   
     if(window.XMLHttpRequest){   
      xmlhttp = new XMLHttpRequest();   
     }   
     xmlhttp.onreadystatechange=function(){   
      if(this.readyState==4 && this.status==200){   
       alert(this.responseText);   
       if(this.responseText == "Proses Edit Berhasil") {   
        window.location.replace("index.php");   
       }   
       else {   
        alert('GAGAL');    
       }   
      }   
     }   
     xmlhttp.open("GET","proses-edit.php?id=" + <?php echo $id;?> + "&nama=" + nama + "&alamat=" + alamat + "&jenis_kelamin=" + jenis_kelamin + "&agama=" + agama + "&sekolah_asal=" + sekolah_asal + "&sekolah_pilihan=" + sekolah_pilihan, true);   
     xmlhttp.send();   
    }   
  </script>   

  • hapus.php
 <?php    
  include("config.php");   
  if(isset($_GET['id']) ){   
   // ambil id dari query string   
   $id = $_GET['id'];   
   $sql = "DELETE FROM calon_siswa WHERE id=$id";   
   // buat query hapus  $sql = "DELETE FROM calon_siswa WHERE ID=$id";   
   $query = mysqli_query($db, $sql);   
   // apakah query hapus berhasil?   
   if( $query ){   
    echo "Hapus Data Berhasil";   
   } else {   
    echo "GAGAL";   
   }    
  }    
  ?>   

  • index.php
 <?php    
  session_start();    
  if (!isset($_SESSION['username'])) {    
    $_SESSION['msg'] = "You must log in first";    
    header('location: login.php');    
  }    
  if (isset($_GET['logout'])) {    
    session_destroy();    
    unset($_SESSION['username']);    
    header("location: login.php");    
  }    
  ?>    
  <!DOCTYPE html>   
  <html>   
  <head>   
   <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">    
   <style type="text/css">   
   body{   
    background-image: url("download.jpg");   
    background-repeat: no-repeat;   
    background-size: cover;   
   }   
   </style>   
  </head>   
  <body >   
   <div class="container-fluid">   
    <header>   
    <h3 align="center">E-Report</h3>   
    </header>   
    <hr>   
    <h4 align="center">Menu</h4>   
    <p align="center"><button class="btn btn-primary" type="button" onclick="showsiswa()">Student List</button></p>   
    <p align="center"><button class="btn btn-primary" onclick="daftar()">Pendaftaran Siswa</button></p>   
   </div>   
   </div>    
   <div class="container-fluid" align="center">    
   <?php if (isset($_SESSION['success'])) : ?>    
   <div class="error success" >    
    <h3>    
   <?php    
     echo $_SESSION['success'];    
     unset($_SESSION['success']);    
   ?>    
    </h3>    
   </div>    
   <?php endif ?>    
   <?php if (isset($_SESSION['username'])) : ?>    
    <p>Welcome <strong><?php echo $_SESSION['username']; ?></strong></p>    
    <p> <a href="index.php?logout='1'" style="color: red;">Logout</a> </p>    
   <?php endif ?>    
   </div>    
   <div class="container" id="demo"></div>   
  </body>   
  </html>   
  <script>   
   function showsiswa() {   
     if (window.XMLHttpRequest) {   
      xhttp = new XMLHttpRequest();   
     }   
     xhttp.onreadystatechange = function() {   
      if (this.readyState == 4 && this.status == 200)   
      {   
      document.getElementById("demo").innerHTML = this.responseText;   
      }   
     };   
    xhttp.open("GET", "list-siswa.php", true);   
    xhttp.send();   
    }   
    function del(id)    
    {   
     if(window.XMLHttpRequest){    
     xmlhttp = new XMLHttpRequest();    
     }   
     xmlhttp.onreadystatechange=function(){   
     if(this.readyState==4 && this.status==200)   
     {   
      alert(this.responseText);    
      showsiswa();    
     }   
     }   
     xmlhttp.open("GET","hapus.php?id=" + id, true);   
     xmlhttp.send();   
    }   
    function daftar() {   
     if (window.XMLHttpRequest) {   
      xhttp = new XMLHttpRequest();   
     }   
     xhttp.onreadystatechange = function() {   
      if (this.readyState == 4 && this.status == 200)   
      {   
      document.getElementById("demo").innerHTML = this.responseText;   
      }   
     };   
    xhttp.open("GET", "form-daftar.php", true);   
    xhttp.send();   
    }   
    function insert(){   
    var nama = document.getElementById("nama").value;   
    var alamat = document.getElementById("alamat").value;   
    var jenis_kelamin;   
    if(document.getElementById("jenisL").checked) {   
     jenis_kelamin = "Laki-laki";   
    }   
    else {   
     jenis_kelamin = "Perempuan";   
    }   
    var agama = document.getElementById("agama").value;   
    var sekolah_asal = document.getElementById("sekolah_asal").value;   
    var sekolah_pilihan = document.getElementById("sekolah_pilihan").value;   
    if(window.XMLHttpRequest) {   
     xmlhttp = new XMLHttpRequest();   
    }   
    xmlhttp.onreadystatechange=function(){   
     if(this.readyState==4 && this.status==200){   
     alert(this.responseText);   
     if(this.responseText == "Pendaftaran Siswa Berhasil") {   
      showsiswa();   
     }   
     else {   
      alert('Pendaftaran Siswa Gagal');    
     }   
     }   
    }   
    xmlhttp.open("GET","proses-pendaftaran.php?nama=" + nama + "&alamat=" + alamat + "&jenis_kelamin=" + jenis_kelamin + "&agama=" + agama + "&sekolah_asal=" + sekolah_asal + "&sekolah_pilihan=" + sekolah_pilihan, true);   
    xmlhttp.send();   
    }   
   </script>   

  • list-siswa.php
 <?php include("config.php"); ?>    
  <!DOCTYPE html>    
  <html>    
  <head>    
   <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">    
  </head>    
  <body>    
   <div class="container-fluid">    
   <header>    
    <h3>List Siswa</h3>    
   </header>    
    <table class="table table-bordered border="1px">    
    <tr>    
     <th>No</th>    
     <th>Nama</th>    
     <th>Alamat</th>    
     <th>Jenis Kelamin</th>    
     <th>Agama</th>    
     <th>Sekolah Asal</th>    
     <th></th>    
    </tr>    
    <tbody>    
    <?php    
    $sql = "SELECT * FROM calon_siswa";    
    $query = mysqli_query($db, $sql);    
    while($siswa = mysqli_fetch_array($query)){    
     echo "<tr>";    
     echo "<td>".$siswa['id']."</td>";    
     echo "<td>".$siswa['nama']."</td>";    
     echo "<td>".$siswa['alamat']."</td>";    
     echo "<td>".$siswa['jenis_kelamin']."</td>";    
     echo "<td>".$siswa['agama']."</td>";    
     echo "<td>".$siswa['sekolah_asal']."</td>";    
     echo "<td>";    
     echo "<a href='form-edit.php?id=".$siswa['id']."' class='btn btn-danger'>Edit</a> | ";   
     echo "<input type='button' onclick=del('".$siswa['id']."') value='Hapus' class='btn btn-info'>";   
     echo "</td>";    
     echo "</tr>";    
    }    
    ?>    
    </tbody>    
    </table>    
   <p>Total Siswa: <?php echo mysqli_num_rows($query) ?></p>    
   <div id="demo"></div>    
   </div>    
  <form method="post" action="download.php">    
   <input type="submit" name="download" class="btn btn-success" value="Download" />   
  </body>    
  </html>   

  • login.php
 <?php include('server.php') ?>    
  <!DOCTYPE html>    
  <html>    
  <head>    
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">   
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">   
  <title>Registrasi Pendaftaran Siswa</title>    
  <style type="text/css">   
   body{   
    background-image: url("download.jpg");   
    background-repeat: no-repeat;   
    background-size: cover;   
   }   
   </style>    
  </head>    
  <body>    
  <div class="container">    
    <h2>Login</h2>    
   <form class="form-horizontal" method="post" action="login.php">    
    <?php include('errors.php'); ?>    
    <div class="form-row">   
    <div class="col-md-5" >   
     <label align="left">Username</label>    
     <input class="form-control" type="text" name="username" ><br>   
    </div>   
    </div>    
    <div class="form-row">   
    <div class="col-md-5">   
     <label style="padding-left: 5px">Password</label>    
    <input class="form-control" type="password" name="password"><br>   
    </div>   
    </div>     
    <div class="form-group">   
    <button type="submit" class="btn btn-primary" name="login_user" style="margin-left: 10px">Login</button>    
    </div>   
    <p>    
     Belum memiliki akun? <a href="register.php">Sign up</a>    
    </p>    
   </form>   
  </div>    
  </body>    
  </html>   

  • proses-edit.php
 <?php    
  include("config.php");    
  // mengecek tombol save udh diclick atau belum   
  if(isset($_POST['simpan'])){    
   // ambil data dari formulir    
   $id = $_POST['id'];    
   $nama = $_POST['nama'];    
   $alamat = $_POST['slamat'];    
   $jk = $_POST['jenis_kelamin'];    
   $agama = $_POST['sgama'];    
   $sekolah = $_POST['dekolah_asal'];    
   $sekolahp = $_POST['dekolah_pilihan'];    
   // buat query update    
   $sql = "UPDATE calon_siswa SET nama='$nama', alamat='$alamat', jenis_aelamin='$jk', agama='$agama', sekolah_asal='$sekolah', sekolah_pilihan='$sekolahp' WHERE id=$id";    
   $query = mysqli_query($db, $sql);    
   // apakah query update berhasil?    
   if( $query ) {    
   // kalau berhasil alihkan ke halaman list-siswa.php    
   header('Location: list-siswa.php');    
   } else {    
   // kalau gagal tampilkan pesan    
   die("Gagal menyimpan perubahan...");    
   }    
  } else {    
   die("Akses dilarang...");    
  }    
  ?>    

  • proses-pendaftaran.php
 <?php include("config.php");   
    $nama = $_GET['nama'];   
    $alamat = $_GET['alamat'];   
    $jk = $_GET['jenis_kelamin'];   
    $agama = $_GET['agama'];   
    $sekolah_asal = $_GET['sekolah_asal'];   
    $sekolah_pilihan = $_GET['sekolah_pilihan'];   
    $sql = "INSERT INTO calon_siswa (nama, alamat, jenis_kelamin, agama, sekolah_asal, sekolah_pilihan) VALUE ('$nama', '$alamat', '$jk', '$agama', '$sekolah_asal','$sekolah_pilihan')";   
    $query = mysqli_query($db, $sql);   
    if( $query ) {   
     echo "Pendaftaran Siswa Berhasil";   
    } else {   
     echo "GAGAL";   
    }   
  ?>   

  • register.php
 <?php include('server.php') ?>    
  <!DOCTYPE html>    
  <html>    
  <head>    
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">   
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">   
   <style type="text/css">   
   body{   
    background-image: url("download.jpg");   
    background-repeat: no-repeat;   
    background-size: cover;   
   }   
   </style>    
  </head>    
  <body>    
  <div class="container">    
    <h2>Register</h2>    
   <form class="form-horizontal" method="post" action="register.php">    
    <?php include('errors.php'); ?>    
    <div class="form-row">   
     <div class="col-md-5">   
     <label>Username</label>    
     <input class="form-control" type="text" name="username" value="<?php echo $username; ?>">    
     </div>   
    </div> <br>    
    <div class="form-row">   
     <div class="col-md-5">   
     <label>Email</label>    
    <input class="form-control" type="email" name="email" value="<?php echo $email; ?>">    
     </div>   
    </div><br>   
    <div class="form-row">   
     <div class="col-md-5">   
     <label>Password</label>    
    <input class="form-control" type="password" name="password_1">    
     </div>   
    </div><br>   
    <div class="form-row">   
     <div class="col-md-5">   
     <label>Confirm password</label>    
    <input class="form-control" type="password" name="password_2">    
     </div><br>   
    </div>    
    </div><br>    
    <div class="form-row">    
    <div class="col-md-5">   
     <button style="margin-left: 130px" type="submit" class="btn btn-primary" name="reg_user">Register</button>   
     <p style="margin-left: 130px">Already a member? <a href="login.php">Sign in</a></p>    
    </div>   
    </div>    
   </form>    
  </div>   
  </body>    
  </html>    

  • server.php
 <?php    
  session_start();    
  // initializing variables    
  $username = "";    
  $email = "";    
  $errors = array();    
  // connect to the database    
  $db = mysqli_connect('localhost', 'dfitrado', 'donny123', 'uaspweb_ppdb');    
  // REGISTER USER    
  if (isset($_POST['reg_user'])) {    
  // receive all input values from the form    
  $username = mysqli_real_escape_string($db, $_POST['username']);    
  $email = mysqli_real_escape_string($db, $_POST['email']);    
  $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);    
  $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);    
  // form validation: ensure that the form is correctly filled ...    
  // by adding (array_push()) corresponding error unto $errors array    
  if (empty($username)) { array_push($errors, "Username is required"); }    
  if (empty($email)) { array_push($errors, "Email is required"); }    
  if (empty($password_1)) { array_push($errors, "Password is required"); }    
  if ($password_1 != $password_2) {    
   array_push($errors, "The two passwords do not match");    
  }    
  // first check the database to make sure    
  // a user does not already exist with the same username and/or email    
  $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";    
  $result = mysqli_query($db, $user_check_query);    
  $user = mysqli_fetch_assoc($result);    
  if ($user) { // if user exists    
   if ($user['username'] === $username) {    
   array_push($errors, "Username already exists");    
   }    
   if ($user['email'] === $email) {    
   array_push($errors, "email already exists");    
   }    
  }    
  // Finally, register user if there are no errors in the form    
  if (count($errors) == 0) {    
    $password = md5($password_1);//encrypt the password before saving in the database    
    $query = "INSERT INTO users (username, email, password)    
      VALUES('$username', '$email', '$password')";    
    mysqli_query($db, $query);    
    $_SESSION['username'] = $username;    
    $_SESSION['success'] = "You are now logged in";    
    header('location: index.php');    
  }    
  }    
  // LOGIN USER    
  if (isset($_POST['login_user'])) {    
  $username = mysqli_real_escape_string($db, $_POST['username']);    
  $password = mysqli_real_escape_string($db, $_POST['password']);    
  if (empty($username)) {    
   array_push($errors, "Username is required");    
  }    
  if (empty($password)) {    
   array_push($errors, "Password is required");    
  }    
  if (count($errors) == 0) {    
   $password = md5($password);    
   $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";    
   $results = mysqli_query($db, $query);    
   if (mysqli_num_rows($results) == 1) {    
   $_SESSION['username'] = $username;    
   $_SESSION['success'] = "You are now logged in";    
   header('location: index.php');    
   }else {    
   array_push($errors, "Wrong username/password combination");    
   }    
  }    
  }    
  ?>    

Terima kasih dan mohon maaf apabila ada kesalahan.

Comments

Popular posts from this blog

Tugas 2 - Pemrograman Orientasi Objek

Tugas 5 PBKK A

Tugas PBO - Tech Support