LOADING . . .

Membuat Website Professional dengan PHP MySql - PART #9



Selamat malam, pada posting kali ini saya akan melanjutkan seri Membuat Website Professional dengan PHP & MySql yang sudah sampai ke PART #9. Part 9 ini akan membahas pembuatan fitur Ubah Password seperti yang sudah saya janjikan di Membuat Website Professional dengan PHP & MySql - PART #5 : Membuat Halaman Profile Admin & Integrasi Gravatar. Bagi yang baru mengikuti posting ini, silahkan lihat posting-posting sebelumnya.

#1 Membuat Halaman Profile Password

  1. Hidupkan XAMPP anda.
  2. Buat file php baru dan beri nama profile-password.php dan simpan di dalam folder admin.
  3. Copy kode berikut dan simpan 
    <?php 
    	   
            include "../config/koneksi.php";
    		
            session_start();
            if($_SESSION['status'] !="login"){
                header("location:../");
            }
    		
    		
    	
    	if (isset($_POST['password_lama'])){
    		
    		$password_lama			= $_POST['password_lama'];
    		$password_baru			= $_POST['password_baru'];
    		$ulangi_password	= $_POST['ulangi_password'];
    		
    		
    		
    		
    		$password_lama_md5	= md5($password_lama);
    		$cek 			= mysqli_query($koneksi, "SELECT password FROM user WHERE password='$password_lama_md5'");
    		
    		if($cek->num_rows){
    			
    			
    			if(strlen($password_baru) >= 5){
    				
    				
    				if($password_baru == $ulangi_password){
    					
    					
    					
    					$password_baru 	= md5($password_baru);
    					$id_user 		= $_POST['id']; 
    					
    					$update 		= mysqli_query($koneksi, "UPDATE user SET password='$password_baru' WHERE id='$id_user'");
    					if($update){
    						
    						?><script> alert ("Password berhasil di rubah"); </script><?php
    					}else{
    						
    						?><script> alert ("pesan=Gagal merubah password");</script> <?php
    					}					
    				}else{
    					
    					?><script> alert ("pesan=Konfirmasi password tidak cocok");</script><?php
    				}
    			}else{
    				
    				?><script> alert ("Minimal password baru adalah 5 karakter");</script><?php
    			}
    		}else{
    			
    			?><script> alert ("Password lama tidak cocok");</script><?php
    		}
    	}else{
    	unset($_POST['password_lama']);
    }
        ?>
    <!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, shrink-to-fit=no">
      <meta name="description" content="">
      <meta name="author" content="">
    <head>
        <title>Suba Hospital | Profile Password</title>
        <!-- Custom fonts for this template-->
      <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
      <link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">
    
      <!-- Custom styles for this template-->
      <link href="css/sb-admin-2.min.css" rel="stylesheet">
      
      <!-- Custom styles for this page -->
      <link href="vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet">
    </head>
    <body id="page-top">
    
    <!-- Page Wrapper -->
      <div id="wrapper">
      	<?php include "sidebar.php"; ?>
        
        <!-- Content Wrapper -->
        <div id="content-wrapper" class="d-flex flex-column">
    
          <!-- Main Content -->
          <div id="content">
          
          <?php include "topbar.php";?>
          
          	<!-- Begin Page Content -->
            <div class="container-fluid">
              <!-- DataTales Example -->
              <div class="card shadow mb-4">
                <div class="card-header py-3">
                  <h6 class="m-0 font-weight-bold text-primary">Ubah Profile Password</h6>
                </div>
                <div class="card-body">
                  <div class="table-responsive">
                  <!-- pesan -->
    			<?php 
                if(isset($_GET['pesan'])){
                    echo '<i class="fas fa-info-circle"></i> '.$_GET['pesan'].'';
                }
                ?>
                <?php 
    			    
    			  	$sql = mysqli_query($koneksi,"select * from user WHERE username='".$_SESSION['username']."'");
    				
    				$row = mysqli_fetch_array($sql);
    			?>
        		<br/>
                   <form class="user" action="profile-password.php" method="post">
                        <div class="form-group">
                        <label>Username</label>
                          <input type="text" name="username" class="form-control" id="exampleInputJudul" value="<?php echo $row['username'];?>" readonly>
                        </div>
                        <div class="form-group">
                        <label>Password Lama</label>
                          <input type="password" name="password_lama" class="form-control" id="exampleInputEmail">
                        </div>
                        <hr>
                        <div class="form-group">
                        <label>Password Baru</label>
                          <input type="password" name="password_baru" class="form-control" id="exampleInputEmail">
                        </div>
                        <div class="form-group">
                        <label>Ulangi Password</label>
                          <input type="password" name="ulangi_password" class="form-control" id="exampleInputEmail">
                        </div>
                        <div class="form-group">
                        <input type="hidden" name="id" value="<?php echo $row['id'];?>">
                         <button style="width:200px; float:right;" class="btn btn-primary btn-block">Update</button>
                        </div>
                    </form>
                  </div>
                </div>
              </div>
              
              
            </div>
             <!-- End Page Content -->
          </div>
          <!-- End Content Wrapper -->
         <?php include "footer.php";?>
    
        </div>
        <!-- End Main Content -->
        </div>
        <!-- End Page Wrapper -->
       <!-- Bootstrap core JavaScript-->
      <script src="vendor/jquery/jquery.min.js"></script>
      <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
    
      <!-- Core plugin JavaScript-->
      <script src="vendor/jquery-easing/jquery.easing.min.js"></script>
    
      <!-- Custom scripts for all pages-->
      <script src="js/sb-admin-2.min.js"></script>
    
      <!-- Page level plugins -->
      <script src="vendor/datatables/jquery.dataTables.min.js"></script>
      <script src="vendor/datatables/dataTables.bootstrap4.min.js"></script>
    
      <!-- Page level custom scripts -->
      <script src="js/demo/datatables-demo.js"></script>
    </body>
    </html>​
    Didalam script kode php tersebut sudah saya sisipkan keterangan dan alur prosesnya, semoga dapat dimengerti.
  4. Tampilannya pada browser akan seperti ini 

Demikian posting kali ini tentang Ubah Password. Nantikan posting Seri Membuat Website Professional dengan PHP & MySql, jangan lupa untuk selalu menjaga kesehatan dan tetap dalam keadaan aman. Semangat!!

Note: Untuk File lengkapnya bisa anda download melalui link dibawah ini.

Download File