+6289-627-868-764 ahmaddwialfian@gmail.com

Sunday, July 24, 2016

Script Register, Login, Logout dan View dengan PHP, MySQL and Bootstrap

11:32 PM


Berbeda dengan postingan sebelumnya, kali ini di tambah dengan register form dan juga menggunakan Bootstrap.
*catatan : dan juga telah menggunakan mysqli
Mari belajar bersama-sama ya.


Nama File yang digunakan
  1. Registration
  2. Login
  3. Logout
  4. Welcome
  5. Admin_login
  6. View_users
  7. Db_conection
  8. Delete
*semua dalam format .php

Create Database:Create Database

Create Tables:

Create table


Create columns in Users table:Create columns

Crate Admin Table:

Admin Table


Create Admin Columns:

create admin column

Registration.php:
  1. <html>  
  2. <head lang="en">  
  3.     <meta charset="UTF-8">  
  4.     <link type="text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css">  
  5.     <title>Registration</title>  
  6. </head>  
  7. <style>  
  8.     .login-panel {  
  9.         margin-top: 150px;  
  10.   
  11. </style>  
  12. <body>  
  13.   
  14. <div class="container"><!-- container class is used to centered  the body of the browser with some decent width-->  
  15.     <div class="row"><!-- row class is used for grid system in Bootstrap-->  
  16.         <div class="col-md-4 col-md-offset-4"><!--col-md-4 is used to create the no of colums in the grid also use for medimum and large devices-->  
  17.             <div class="login-panel panel panel-success">  
  18.                 <div class="panel-heading">  
  19.                     <h3 class="panel-title">Registration</h3>  
  20.                 </div>  
  21.                 <div class="panel-body">  
  22.                     <form role="form" method="post" action="registration.php">  
  23.                         <fieldset>  
  24.                             <div class="form-group">  
  25.                                 <input class="form-control" placeholder="Username" name="name" type="text" autofocus>  
  26.                             </div>  
  27.   
  28.                             <div class="form-group">  
  29.                                 <input class="form-control" placeholder="E-mail" name="email" type="email" autofocus>  
  30.                             </div>  
  31.                             <div class="form-group">  
  32.                                 <input class="form-control" placeholder="Password" name="pass" type="password" value="">  
  33.                             </div>  
  34.   
  35.   
  36.                             <input class="btn btn-lg btn-success btn-block" type="submit" value="register" name="register" >  
  37.   
  38.                         </fieldset>  
  39.                     </form>  
  40.                     <center><b>Already registered ?</b> <br></b><a href="login.php">Login here</a></center><!--for centered text-->  
  41.                 </div>  
  42.             </div>  
  43.         </div>  
  44.     </div>  
  45. </div>  
  46.   
  47. </body>  
  48.   
  49. </html>  
  50.   
  51. <?php  
  52.   
  53. include("database/db_conection.php");//make connection here  
  54. if(isset($_POST['register']))  
  55. {  
  56.     $user_name=$_POST['name'];//here getting result from the post array after submitting the form.  
  57.     $user_pass=$_POST['pass'];//same  
  58.     $user_email=$_POST['email'];//same  
  59.   
  60.   
  61.     if($user_name=='')  
  62.     {  
  63.         //javascript use for input checking  
  64.         echo"<script>alert('Please enter the name')</script>";  
  65. exit();//this use if first is not work then other will not show  
  66.     }  
  67.   
  68.     if($user_pass=='')  
  69.     {  
  70.         echo"<script>alert('Please enter the password')</script>";  
  71. exit();  
  72.     }  
  73.   
  74.     if($user_email=='')  
  75.     {  
  76.         echo"<script>alert('Please enter the email')</script>";  
  77.     exit();  
  78.     }  
  79. //here query check weather if user already registered so can't register again.  
  80.     $check_email_query="select * from users WHERE user_email='$user_email'";  
  81.     $run_query=mysqli_query($dbcon,$check_email_query);  
  82.   
  83.     if(mysqli_num_rows($run_query)>0)  
  84.     {  
  85. echo "<script>alert('Email $user_email is already exist in our database, Please try another one!')</script>";  
  86. exit();  
  87.     }  
  88. //insert the user into the database.  
  89.     $insert_user="insert into users (user_name,user_pass,user_email) VALUE ('$user_name','$user_pass','$user_email')";  
  90.     if(mysqli_query($dbcon,$insert_user))  
  91.     {  
  92.         echo"<script>window.open('welcome.php','_self')</script>";  
  93.     }  
  94.   
  95.   
  96.   
  97. }  
  98.   
  99. ?>  
register

Login.php
  1. <?php  
  2. session_start();//session starts here  
  3.   
  4. ?>  
  5.   
  6.   
  7.   
  8. <html>  
  9. <head lang="en">  
  10.     <meta charset="UTF-8">  
  11.     <link type="text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css">  
  12.     <title>Login</title>  
  13. </head>  
  14. <style>  
  15.     .login-panel {  
  16.         margin-top: 150px;  
  17.   
  18. </style>  
  19.   
  20. <body>  
  21.   
  22.   
  23. <div class="container">  
  24.     <div class="row">  
  25.         <div class="col-md-4 col-md-offset-4">  
  26.             <div class="login-panel panel panel-success">  
  27.                 <div class="panel-heading">  
  28.                     <h3 class="panel-title">Sign In</h3>  
  29.                 </div>  
  30.                 <div class="panel-body">  
  31.                     <form role="form" method="post" action="login.php">  
  32.                         <fieldset>  
  33.                             <div class="form-group"  >  
  34.                                 <input class="form-control" placeholder="E-mail" name="email" type="email" autofocus>  
  35.                             </div>  
  36.                             <div class="form-group">  
  37.                                 <input class="form-control" placeholder="Password" name="pass" type="password" value="">  
  38.                             </div>  
  39.   
  40.   
  41.                                 <input class="btn btn-lg btn-success btn-block" type="submit" value="login" name="login" >  
  42.   
  43.                             <!-- Change this to a button or input when using this as a form -->  
  44.                           <!--  <a href="index.html" class="btn btn-lg btn-success btn-block">Login</a> -->  
  45.                         </fieldset>  
  46.                     </form>  
  47.                 </div>  
  48.             </div>  
  49.         </div>  
  50.     </div>  
  51. </div>  
  52.   
  53.   
  54. </body>  
  55.   
  56. </html>  
  57.   
  58. <?php  
  59.   
  60. include("database/db_conection.php");  
  61.   
  62. if(isset($_POST['login']))  
  63. {  
  64.     $user_email=$_POST['email'];  
  65.     $user_pass=$_POST['pass'];  
  66.   
  67.     $check_user="select * from users WHERE user_email='$user_email'AND user_pass='$user_pass'";  
  68.   
  69.     $run=mysqli_query($dbcon,$check_user);  
  70.   
  71.     if(mysqli_num_rows($run))  
  72.     {  
  73.         echo "<script>window.open('welcome.php','_self')</script>";  
  74.   
  75.         $_SESSION['email']=$user_email;//here session is used and value of $user_email store in $_SESSION.  
  76.   
  77.     }  
  78.     else  
  79.     {  
  80.       echo "<script>alert('Email or password is incorrect!')</script>";  
  81.     }  
  82. }  
  83. ?>  
login

Logout.php
  1. <?php  
  2. /** 
  3.  * Created by PhpStorm. 
  4.  * User: Ehtesham Mehmood 
  5.  * Date: 11/21/2014 
  6.  * Time: 2:46 AM 
  7.  */  
  8.   
  9. session_start();//session is a way to store information (in variables) to be used across multiple pages.  
  10. session_destroy();  
  11. header("Location: login.php");//use for the redirection to some page  
  12. ?>  
Welcome.php
  1. <?php  
  2. session_start();  
  3.   
  4. if(!$_SESSION['email'])  
  5. {  
  6.   
  7.     header("Location: login.php");//redirect to login page to secure the welcome page without login access.  
  8. }  
  9.   
  10. ?>  
  11.   
  12. <html>  
  13. <head>  
  14.   
  15.     <title>  
  16.         Registration  
  17.     </title>  
  18. </head>  
  19.   
  20. <body>  
  21. <h1>Welcome</h1><br>  
  22. <?php  
  23. echo $_SESSION['email'];  
  24. ?>  
  25.   
  26.   
  27. <h1><a href="logout.php">Logout here</a> </h1>  
  28.   
  29.   
  30. </body>  
  31.   
  32. </html>  
Admin_login.php
  1. <html>  
  2. <head lang="en">  
  3.     <meta charset="UTF-8">  
  4.     <link type="text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css">  
  5.     <title>Admin Login</title>  
  6. </head>  
  7. <style>  
  8.     .login-panel {  
  9.         margin-top: 150px;  
  10.   
  11. </style>  
  12.   
  13. <body>  
  14.   
  15. <div class="container">  
  16.     <div class="row">  
  17.         <div class="col-md-4 col-md-offset-4">  
  18.             <div class="login-panel panel panel-success">  
  19.                 <div class="panel-heading">  
  20.                     <h3 class="panel-title">Sign In</h3>  
  21.                 </div>  
  22.                 <div class="panel-body">  
  23.                     <form role="form" method="post" action="admin_login.php">  
  24.                         <fieldset>  
  25.                             <div class="form-group"  >  
  26.                                 <input class="form-control" placeholder="Name" name="admin_name" type="text" autofocus>  
  27.                             </div>  
  28.                             <div class="form-group">  
  29.                                 <input class="form-control" placeholder="Password" name="admin_pass" type="password" value="">  
  30.                             </div>  
  31.   
  32.   
  33.                             <input class="btn btn-lg btn-success btn-block" type="submit" value="login" name="admin_login" >  
  34.   
  35.   
  36.                         </fieldset>  
  37.                     </form>  
  38.                 </div>  
  39.             </div>  
  40.         </div>  
  41.     </div>  
  42. </div>  
  43.   
  44.   
  45. </body>  
  46.   
  47. </html>  
  48.   
  49. <?php  
  50. /** 
  51.  * Created by PhpStorm. 
  52.  * User: Ehtesham Mehmood 
  53.  * Date: 11/24/2014 
  54.  * Time: 3:26 AM 
  55.  */  
  56. include("database/db_conection.php");  
  57.   
  58. if(isset($_POST['admin_login']))//this will tell us what to do if some data has been post through form with button.  
  59. {  
  60.     $admin_name=$_POST['admin_name'];  
  61.     $admin_pass=$_POST['admin_pass'];  
  62.   
  63.     $admin_query="select * from admin where admin_name='$admin_name' AND admin_pass='$admin_pass'";  
  64.   
  65.     $run_query=mysqli_query($dbcon,$admin_query);  
  66.   
  67.     if(mysqli_num_rows($run_query)>0)  
  68.     {  
  69.   
  70.         echo "<script>window.open('view_users.php','_self')</script>";  
  71.     }  
  72.     else {echo"<script>alert('Admin Details are incorrect..!')</script>";}  
  73.   
  74. }  
  75.   
  76. ?>  
View_users.php
  1. <html>  
  2. <head lang="en">  
  3.     <meta charset="UTF-8">  
  4.     <link type="text/css" rel="stylesheet" href="bootstrap-3.2.0-dist\css\bootstrap.css"> <!--css file link in bootstrap folder-->  
  5.     <title>View Users</title>  
  6. </head>  
  7. <style>  
  8.     .login-panel {  
  9.         margin-top: 150px;  
  10.     }  
  11.     .table {  
  12.         margin-top: 50px;  
  13.   
  14.     }  
  15.   
  16. </style>  
  17.   
  18. <body>  
  19.   
  20. <div class="table-scrol">  
  21.     <h1 align="center">All the Users</h1>  
  22.   
  23. <div class="table-responsive"><!--this is used for responsive display in mobile and other devices-->  
  24.   
  25.   
  26.     <table class="table table-bordered table-hover table-striped" style="table-layout: fixed">  
  27.         <thead>  
  28.   
  29.         <tr>  
  30.   
  31.             <th>User Id</th>  
  32.             <th>User Name</th>  
  33.             <th>User E-mail</th>  
  34.             <th>User Pass</th>  
  35.             <th>Delete User</th>  
  36.         </tr>  
  37.         </thead>  
  38.   
  39.         <?php  
  40.         include("database/db_conection.php");  
  41.         $view_users_query="select * from users";//select query for viewing users.  
  42.         $run=mysqli_query($dbcon,$view_users_query);//here run the sql query.  
  43.   
  44.         while($row=mysqli_fetch_array($run))//while look to fetch the result and store in a array $row.  
  45.         {  
  46.             $user_id=$row[0];  
  47.             $user_name=$row[1];  
  48.             $user_email=$row[2];  
  49.             $user_pass=$row[3];  
  50.   
  51.   
  52.   
  53.         ?>  
  54.   
  55.         <tr>  
  56. <!--here showing results in the table -->  
  57.             <td><?php echo $user_id;  ?></td>  
  58.             <td><?php echo $user_name;  ?></td>  
  59.             <td><?php echo $user_email;  ?></td>  
  60.             <td><?php echo $user_pass;  ?></td>  
  61.             <td><a href="delete.php?del=<?php echo $user_id ?>"><button class="btn btn-danger">Delete</button></a></td> <!--btn btn-danger is a bootstrap button to show danger-->  
  62.         </tr>  
  63.   
  64.         <?php } ?>  
  65.   
  66.     </table>  
  67.         </div>  
  68. </div>  
  69.   
  70.   
  71. </body>  
  72.   
  73. </html>  
user status

Db_conection.php
  1. <?php  
  2. /** 
  3.  * Created by PhpStorm. 
  4.  * User: Ehtesham Mehmood 
  5.  * Date: 11/21/2014 
  6.  * Time: 1:13 AM 
  7.  */  
  8. $dbcon=mysqli_connect("localhost","root","");  
  9. mysqli_select_db($dbcon,"users");  
  10. ?>  
Delete.php
  1. <?php  
  2. /** 
  3.  * Created by PhpStorm. 
  4.  * User: Ehtesham Mehmood 
  5.  * Date: 11/24/2014 
  6.  * Time: 11:55 PM 
  7.  */  
  8. include("database/db_conection.php");  
  9. $delete_id=$_GET['del'];  
  10. $delete_query="delete  from users WHERE id='$delete_id'";//delete query  
  11. $run=mysqli_query($dbcon,$delete_query);  
  12. if($run)  
  13. {  
  14. //javascript function to open in the same window   
  15.     echo "<script>window.open('view_users.php?deleted=user has been deleted','_self')</script>";  
  16. }  
  17.   
  18. ?>  
Sumber : c-sharpcorner.com

0 comments:

Post a Comment