tutorialswb

TutorialsWB hadir sebagai wadah bagi teman-teman yang ingin "Belajar PHP Dasar". Sehingga kami yakin bahwa TutorialsWB akan menjadi Pusat "Belajar Bahasa Pemrograman PHP" Disamping PHP website ini menyediakan berbagai tips dan trik Blogger. Kami menerima Pemasangan iklan banner atau artikel.

Hubungi Saya

Register dan Reset Password dengan Verifikasi Email Menggunakan PHP MySQL



Selamat malam bagaimana kabar kalian kali ini kami akan share bagaimana membuat form register dan reset password dengan verifikasi email menggunakan php mysql.



Kenapa perlu verifikasi email ?

Hal ini merupakan bagian dari security dari sebuah aplikasi, pada project kami yaitu aplikasi manajemen pemeliharaan gedung berbasis web, Pada aplikasi dan project pertama kami ini, kami belum menerapkan security di atas dan yang terjadi adalah banyak user yang hanya mendaftarkan email palsu mereka bahkan bukan mereka yang mempunyai email tersebut karena tidak adanya verifikasi aktivasi akun.

Disamping security di atas kmai juga menggunakan google captcha ketika user melakukan register, kemudian kami juga menerapkan security yang menurut kami sangat penting untuk di terapkan dalam aplikasi atau project anda yaitu ketika user melakukan reset password maka anda akan dikirmkan link reset password melalui email user. Untuk verifikasi email kami menggunakan SMTP (Simple mail transfer protocol).

Pada tutorial dibawah kami mempunyai banyak file karena kami memisahkan banyak file kemudian kami kumpulkan dalam satu folder, tujuannya adalah untuk mengurangi code yang ada dalam satu file.

Apa yang kami jelaskan di atas merupakan spesifikasi dari project sederhana yang kami akan share di bawah. Daripada kami banyak bicara langsung cara mari kita mulai dari awal.

1. Buat database dengan nama ecom kemudian masukkan struktur databse dibawah ini.
 CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `email` varchar(200) NOT NULL,
  `password` varchar(60) NOT NULL,
  `type` int(1) NOT NULL,
  `firstname` varchar(50) NOT NULL,
  `lastname` varchar(50) NOT NULL,
  `address` text NOT NULL,
  `contact_info` varchar(100) NOT NULL,
  `photo` varchar(200) NOT NULL,
  `status` int(1) NOT NULL,
  `activate_code` varchar(15) NOT NULL,
  `reset_code` varchar(15) NOT NULL,
  `created_on` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

2. Buat file dengan nama conn.php file ini berfungsi sebagai koneksi kedatabse.
 <?php
Class Database{
 private $server = "mysql:host=localhost;dbname=YOUR-DATABSE";
 private $username = "root";
 private $password = "";
 private $options  = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,);
 protected $conn;
 public function open(){
  try{
   $this->conn = new PDO($this->server, $this->username, $this->password, $this->options);
   return $this->conn;
  }
  catch (PDOException $e){
   echo "There is some problem in connection: " . $e->getMessage();
  } 
 }
 
 public function close(){
  $this->conn = null;
 }
}
$pdo = new Database();

?> 

3. Buat file dengan nama header.php, file ini kami gunakan untuk menyimpan direktory css yang kami gunakan dalam tutorial sederhana ini.
 <!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <title>Login dan Register</title>
 <!-- Tell the browser to be responsive to screen width -->
 <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
 <!-- Bootstrap 3.3.7 -->
 <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
 <!-- DataTables -->
 <link rel="stylesheet" href="bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css">
 <!-- Font Awesome -->
 <link rel="stylesheet" href="bower_components/font-awesome/css/font-awesome.min.css">
 <!-- Theme style -->
 <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
   <!-- AdminLTE Skins. Choose a skin from the css/skins
     folder instead of downloading all of them to reduce the load. -->
     <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">
     <!-- Magnify -->
     <link rel="stylesheet" href="magnify/magnify.min.css">

     <!-- Google Font -->
     <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic">


     <!-- Google Recaptcha -->
     <script src='https://www.google.com/recaptcha/api.js'></script>

     <!-- Custom CSS -->
     <style type="text/css">
     /* Small devices (tablets, 768px and up) */
     @media (min-width: 768px){ 
      #navbar-search-input{ 
        width: 60px; 
      }
      #navbar-search-input:focus{ 
        width: 100px; 
      }
    }

    /* Medium devices (desktops, 992px and up) */
    @media (min-width: 992px){ 
      #navbar-search-input{ 
        width: 150px; 
      }
      #navbar-search-input:focus{ 
        width: 250px; 
      } 
    }

    .word-wrap{
      overflow-wrap: break-word;
    }
    .prod-body{
      height:300px;
    }

    .box:hover {
      box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
    }
    .register-box{
      margin-top:20px;
    }

    #trending{
      list-style: none;
      padding:10px 5px 10px 15px;
    }
    #trending li {
      padding-left: 1.3em;
    }
    #trending li:before {
      content: "\f046";
      font-family: FontAwesome;
      display: inline-block;
      margin-left: -1.3em; 
      width: 1.3em;
    }

    /*Magnify*/
    .magnify > .magnify-lens {
      width: 100px;
      height: 100px;
    }

  </style>

</head> 

4. Buat file dengan nama navbar.php file merupakan tampilan dari header.
 <header class="main-header">
  <nav class="navbar navbar-static-top">
    <div class="container">
      <div class="navbar-header">
        <a href="index.php" class="navbar-brand"><b>REGISTER DAN RESET PASSWORD DENGAN VERIF EMAIL</b></a>

        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse">
          <i class="fa fa-bars"></i>
        </button>
      </div>

      <!-- Collect the nav links, forms, and other content for toggling -->

      <!-- /.navbar-collapse -->
      <!-- Navbar Right Menu -->
      <div class="navbar-custom-menu">
        <ul class="nav navbar-nav">
          <?php
          if(isset($_SESSION['user'])){
            $image = (!empty($user['photo'])) ? 'images/'.$user['photo'] : 'img/user-location.png';
            echo '
            <li class="dropdown user user-menu">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
            <img src="'.$image.'" class="user-image" alt="User Image">
            <span class="hidden-xs">'.$user['firstname'].' '.$user['lastname'].'</span>
            </a>
            <ul class="dropdown-menu">
            <!-- User image -->
            <li class="user-header">
            <img src="'.$image.'" class="img-circle" alt="User Image">

            <p>
            '.$user['firstname'].' '.$user['lastname'].'
            <small>Member since '.date('M. Y', strtotime($user['created_on'])).'</small>
            </p>
            </li>
            <li class="user-footer">
            <div class="pull-left">
            <a href="#" class="btn btn-default btn-flat">Profile</a>
            </div>
            <div class="pull-right">
            <a href="logout.php" class="btn btn-default btn-flat">Sign out</a>
            </div>
            </li>
            </ul>
            </li>
            ';
          }
          else{
            echo "
            <li><a href='login.php'>LOGIN</a></li>
            <li><a href='signup.php'>SIGNUP</a></li>
            ";
          }
          ?>
        </ul>
      </div>
    </div>
  </nav>
</header> 

5. activate.php file ini yang berfungsi ketika user baru melakukan register, file ini berisi script untuk mengaktifkan emai yang kita daftarkan
 <?php include 'includes/session.php'; ?>
<?php
$output = '';
if(!isset($_GET['code']) OR !isset($_GET['user'])){
 $output .= '
 <div class="alert alert-danger">
 <h4><i class="icon fa fa-warning"></i> Error!</h4>
 Code to activate account not found.
 </div>
 <h4>You may <a href="signup.php">Signup</a> or back to <a href="index.php">Homepage</a>.</h4>
 '; 
}
else{
 $conn = $pdo->open();

 $stmt = $conn->prepare("SELECT *, COUNT(*) AS numrows FROM users WHERE activate_code=:code AND id=:id");
 $stmt->execute(['code'=>$_GET['code'], 'id'=>$_GET['user']]);
 $row = $stmt->fetch();

 if($row['numrows'] > 0){
  if($row['status']){
   $output .= '
   <div class="alert alert-danger">
   <h4><i class="icon fa fa-warning"></i> Error!</h4>
   Account already activated.
   </div>
   <h4>You may <a href="login.php">Login</a> or back to <a href="index.php">Homepage</a>.</h4>
   ';
  }
  else{
   try{
    $stmt = $conn->prepare("UPDATE users SET status=:status WHERE id=:id");
    $stmt->execute(['status'=>1, 'id'=>$row['id']]);
    $output .= '
    <div class="alert alert-success">
    <h4><i class="icon fa fa-check"></i> Success!</h4>
    Account activated - Email: <b>'.$row['email'].'</b>.
    </div>
    <h4>You may <a href="login.php">Login</a> or back to <a href="index.php">Homepage</a>.</h4>
    ';
   }
   catch(PDOException $e){
    $output .= '
    <div class="alert alert-danger">
    <h4><i class="icon fa fa-warning"></i> Error!</h4>
    '.$e->getMessage().'
    </div>
    <h4>You may <a href="signup.php">Signup</a> or back to <a href="index.php">Homepage</a>.</h4>
    ';
   }

  }
  
 }
 else{
  $output .= '
  <div class="alert alert-danger">
  <h4><i class="icon fa fa-warning"></i> Error!</h4>
  Cannot activate account. Wrong code.
  </div>
  <h4>You may <a href="signup.php">Signup</a> or back to <a href="index.php">Homepage</a>.</h4>
  ';
 }

 $pdo->close();
}
?>
<?php include 'includes/header.php'; ?>
<body class="hold-transition skin-blue layout-top-nav">
 <div class="wrapper">

  <?php include 'includes/navbar.php'; ?>
  
  <div class="content-wrapper">
   <div class="container">

    <!-- Main content -->
    <section class="content">
     <div class="row">
      <div class="col-sm-9">
       <?php echo $output; ?>
      </div>
      <div class="col-sm-3">
       <?php include 'includes/sidebar.php'; ?>
      </div>
     </div>
    </section>
    
   </div>
  </div>
  
  <?php include 'includes/footer.php'; ?>
 </div>

 <?php include 'includes/scripts.php'; ?>
</body>
</html> 

6. cart_view.php merupakan tampilan utama yang dituju ketika user berhasil melakukan login.
 <?php include 'includes/session.php'; ?>
<?php include 'includes/header.php'; ?>
<body class="hold-transition skin-blue layout-top-nav">
 <div class="wrapper">
  <?php include 'includes/navbar.php'; ?>
  <div class="content-wrapper">
   <div class="container">

   </div>
  </div>
 </div>
 <?php include 'includes/scripts.php'; ?>
</body>
</html> 

7. Buat file dengan nama index.php file merupakan tampilan utama ketika membuka aplikasi.
 <?php include 'includes/session.php'; ?>
<?php include 'includes/header.php'; ?>
<body class="hold-transition skin-blue layout-top-nav">
 <div class="wrapper">
  <?php include 'includes/navbar.php'; ?>
  <div class="content-wrapper">
   <div class="container">
   </div>
  </div>
 </div>
 <?php include 'includes/scripts.php'; ?>
</body>
</html> 

8. login.php file ini berfungsi sebagai form login bagi user.
 <?php include 'includes/session.php'; ?>
<?php
if(isset($_SESSION['user'])){
  header('location: cart_view.php');
}
?>
<?php include 'includes/header.php'; ?>
<body class="hold-transition login-page">
  <div class="login-box">
   <?php
    if(isset($_SESSION['error'])){
      echo "
      <div class='callout callout-danger text-center'>
      <p>".$_SESSION['error']."</p> 
      </div>
      ";
      unset($_SESSION['error']);
    }
    if(isset($_SESSION['success'])){
      echo "
      <div class='callout callout-success text-center'>
      <p>".$_SESSION['success']."</p> 
      </div>
      ";
      unset($_SESSION['success']);
    }
    ?>
    <div class="login-box-body">
     <p class="login-box-msg">Sign in to start your session</p>

     <form action="verify.php" method="POST">
        <div class="form-group has-feedback">
          <input type="email" class="form-control" name="email" placeholder="Email" required>
          <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
        </div>
        <div class="form-group has-feedback">
          <input type="password" class="form-control" name="password" placeholder="Password" required>
          <span class="glyphicon glyphicon-lock form-control-feedback"></span>
        </div>
        <div class="row">
         <div class="col-xs-4">
           <button type="submit" class="btn btn-primary btn-block btn-flat" name="login"><i class="fa fa-sign-in"></i> Sign In</button>
         </div>
       </div>
     </form>
     <br>
     <a href="password_forgot.php">I forgot my password</a><br>
     <a href="signup.php" class="text-center">Register a new membership</a><br>
     <a href="index.php">Home</a>
   </div>
 </div>
 <?php include 'includes/scripts.php' ?>
</body>
</html> 

9. logout.php pasti kalian tahu fungsinya lah ya.
 <?php
session_start();
session_destroy();
header('location: index.php');
?> 

10. password_forgot.php file ini berfungsi sebgaai form reset password.
 <?php include 'includes/session.php'; ?>
<?php include 'includes/header.php'; ?>
<body class="hold-transition login-page">
  <div class="login-box">
   <?php
    if(isset($_SESSION['error'])){
      echo "
      <div class='callout callout-danger text-center'>
      <p>".$_SESSION['error']."</p> 
      </div>
      ";
      unset($_SESSION['error']);
    }
    if(isset($_SESSION['success'])){
      echo "
      <div class='callout callout-success text-center'>
      <p>".$_SESSION['success']."</p> 
      </div>
      ";
      unset($_SESSION['success']);
    }
    ?>
    <div class="login-box-body">
     <p class="login-box-msg">Enter email associated with account</p>

     <form action="reset.php" method="POST">
        <div class="form-group has-feedback">
          <input type="email" class="form-control" name="email" placeholder="Email" required>
          <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
        </div>
        <div class="row">
         <div class="col-xs-4">
           <button type="submit" class="btn btn-primary btn-block btn-flat" name="reset"><i class="fa fa-mail-forward"></i> Send</button>
         </div>
       </div>
     </form>
     <br>
     <a href="login.php">I rememberd my password</a><br>
     <a href="index.php">Home</a>
   </div>
 </div>
 
 <?php include 'includes/scripts.php' ?>
</body>
</html> 

11. password_new.php file ini berfungsi sebagai script untuk memasukkan password baru.
 <?php
include 'includes/session.php';

if(!isset($_GET['code']) OR !isset($_GET['user'])){
 header('location: index.php');
 exit(); 
}

$path = 'password_reset.php?code='.$_GET['code'].'&user='.$_GET['user'];

if(isset($_POST['reset'])){
 $password = $_POST['password'];
 $repassword = $_POST['repassword'];

 if($password != $repassword){
  $_SESSION['error'] = 'Passwords did not match';
  header('location: '.$path);
 }
 else{
  $conn = $pdo->open();

  $stmt = $conn->prepare("SELECT *, COUNT(*) AS numrows FROM users WHERE reset_code=:code AND id=:id");
  $stmt->execute(['code'=>$_GET['code'], 'id'=>$_GET['user']]);
  $row = $stmt->fetch();

  if($row['numrows'] > 0){
   $password = password_hash($password, PASSWORD_DEFAULT);

   try{
    $stmt = $conn->prepare("UPDATE users SET password=:password WHERE id=:id");
    $stmt->execute(['password'=>$password, 'id'=>$row['id']]);

    $_SESSION['success'] = 'Password successfully reset';
    header('location: login.php');
   }
   catch(PDOException $e){
    $_SESSION['error'] = $e->getMessage();
    header('location: '.$path);
   }
  }
  else{
   $_SESSION['error'] = 'Code did not match with user';
   header('location: '.$path);
  }

  $pdo->close();
 }

}
else{
 $_SESSION['error'] = 'Input new password first';
 header('location: '.$path);
}

?> 

12. password_reset.php file ini berfungsi sebagai form untuk reset password.
 <?php include 'includes/session.php'; ?>
<?php
if(!isset($_GET['code']) OR !isset($_GET['user'])){
  header('location: index.php');
  exit(); 
}
?>
<?php include 'includes/header.php'; ?>
<body class="hold-transition login-page">
  <div class="login-box">
   <?php
    if(isset($_SESSION['error'])){
      echo "
      <div class='callout callout-danger text-center'>
      <p>".$_SESSION['error']."</p> 
      </div>
      ";
      unset($_SESSION['error']);
    }
    ?>
    <div class="login-box-body">
     <p class="login-box-msg">Enter new password</p>

     <form action="password_new.php?code=<?php echo $_GET['code']; ?>&user=<?php echo $_GET['user']; ?>" method="POST">
        <div class="form-group has-feedback">
          <input type="password" class="form-control" name="password" placeholder="New password" required>
          <span class="glyphicon glyphicon-lock form-control-feedback"></span>
        </div>
        <div class="form-group has-feedback">
          <input type="password" class="form-control" name="repassword" placeholder="Re-type password" required>
          <span class="glyphicon glyphicon-log-in form-control-feedback"></span>
        </div>
        <div class="row">
         <div class="col-xs-4">
           <button type="submit" class="btn btn-primary btn-block btn-flat" name="reset"><i class="fa fa-check-square-o"></i> Reset</button>
         </div>
       </div>
     </form>
   </div>
 </div>
 
 <?php include 'includes/scripts.php' ?>
</body>
</html> 

13. register.php file ini berfisi script yang berfungsi dan mengeksekusi fungsi register.
 <?php
 use PHPMailer\PHPMailer\PHPMailer;
 use PHPMailer\PHPMailer\Exception;

 include 'includes/session.php';

 if(isset($_POST['signup'])){
  $firstname = $_POST['firstname'];
  $lastname = $_POST['lastname'];
  $email = $_POST['email'];
  $password = $_POST['password'];
  $repassword = $_POST['repassword'];

  $_SESSION['firstname'] = $firstname;
  $_SESSION['lastname'] = $lastname;
  $_SESSION['email'] = $email;

  if(!isset($_SESSION['captcha'])){
   require('recaptcha/src/autoload.php');  
   $recaptcha = new \ReCaptcha\ReCaptcha('YOUR SECRET KEY', new \ReCaptcha\RequestMethod\SocketPost());
   $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);

   if (!$resp->isSuccess()){
      $_SESSION['error'] = 'Please answer recaptcha correctly';
      header('location: signup.php'); 
      exit(); 
     } 
     else{
      $_SESSION['captcha'] = time() + (10*60);
     }

  }

  if($password != $repassword){
   $_SESSION['error'] = 'Passwords did not match';
   header('location: signup.php');
  }
  else{
   $conn = $pdo->open();

   $stmt = $conn->prepare("SELECT COUNT(*) AS numrows FROM users WHERE email=:email");
   $stmt->execute(['email'=>$email]);
   $row = $stmt->fetch();
   if($row['numrows'] > 0){
    $_SESSION['error'] = 'Email already taken';
    header('location: signup.php');
   }
   else{
    $now = date('Y-m-d');
    $password = password_hash($password, PASSWORD_DEFAULT);

    //generate code
    $set='123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $code=substr(str_shuffle($set), 0, 12);

    try{
     $stmt = $conn->prepare("INSERT INTO users (email, password, firstname, lastname, activate_code, created_on) VALUES (:email, :password, :firstname, :lastname, :code, :now)");
     $stmt->execute(['email'=>$email, 'password'=>$password, 'firstname'=>$firstname, 'lastname'=>$lastname, 'code'=>$code, 'now'=>$now]);
     $userid = $conn->lastInsertId();

     $message = "
      <h2>Thank you for Registering.</h2>
      <p>Your Account:</p>
      <p>Email: ".$email."</p>
      <p>Password: ".$_POST['password']."</p>
      <p>Please click the link below to activate your account.</p>
      <a href='YOUR-HOST/ecommerce/activate.php?code=".$code."&user=".$userid."'>Activate Account</a>
     ";

     //Load phpmailer
        require 'vendor/autoload.php';

        $mail = new PHPMailer(true);                             
        try {
            //Server settings
            $mail->isSMTP();                                     
            $mail->Host = 'YOUR-HOST';                      
           $mail->SMTPAuth = true;                               
           $mail->Username = 'YOUR-EMAIL-SMTP';     
           $mail->Password = 'PASSWORD-HOST';                    
           $mail->SMTPOptions = array(
                'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
                )
            );                         
            $mail->SMTPSecure = 'ssl';                           
            $mail->Port = 465;                                   

            $mail->setFrom('YOUR-EMAIL-SMTP');
            
            //Recipients
            $mail->addAddress($email);              
            $mail->addReplyTo('YOUR-EMAIL-SMTP');
           
            //Content
            $mail->isHTML(true);                                  
            $mail->Subject = 'ECommerce Site Sign Up';
            $mail->Body    = $message;

            $mail->send();

            unset($_SESSION['firstname']);
            unset($_SESSION['lastname']);
            unset($_SESSION['email']);

            $_SESSION['success'] = 'Account created. Check your email to activate.';
            header('location: signup.php');

        } 
        catch (Exception $e) {
            $_SESSION['error'] = 'Message could not be sent. Mailer Error: '.$mail->ErrorInfo;
            header('location: signup.php');
        }


    }
    catch(PDOException $e){
     $_SESSION['error'] = $e->getMessage();
     header('location: register.php');
    }

    $pdo->close();

   }

  }

 }
 else{
  $_SESSION['error'] = 'Fill up signup form first';
  header('location: signup.php');
 }

?> 

14. reset.php file ini berisi script untuk mengirimkan email reset password ke email user yang melakuakn reset password.
 <?php
 use PHPMailer\PHPMailer\PHPMailer;
 use PHPMailer\PHPMailer\Exception;

 include 'includes/session.php';

 if(isset($_POST['reset'])){
  $email = $_POST['email'];

  $conn = $pdo->open();

  $stmt = $conn->prepare("SELECT *, COUNT(*) AS numrows FROM users WHERE email=:email");
  $stmt->execute(['email'=>$email]);
  $row = $stmt->fetch();

  if($row['numrows'] > 0){
   //generate code
   $set='123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
   $code=substr(str_shuffle($set), 0, 15);
   try{
    $stmt = $conn->prepare("UPDATE users SET reset_code=:code WHERE id=:id");
    $stmt->execute(['code'=>$code, 'id'=>$row['id']]);
    
    $message = "
     <h2>Password Reset</h2>
     <p>Your Account:</p>
     <p>Email: ".$email."</p>
     <p>Please click the link below to reset your password.</p>
     <a href='https://ecommerce.arman.my.id/password_reset.php?code=".$code."&user=".$row['id']."'>Reset Password</a>
    ";

    //Load phpmailer
       require 'vendor/autoload.php';

       $mail = new PHPMailer(true);                             
       try {
           //Server settings
           $mail->isSMTP();                                     
             $mail->Host = 'YOUR-HOST';                      
           $mail->SMTPAuth = true;                               
           $mail->Username = 'YOUR-EMAIL-SMTP';     
           $mail->Password = 'PASSWORD-HOST';                     
           $mail->SMTPOptions = array(
               'ssl' => array(
               'verify_peer' => false,
               'verify_peer_name' => false,
               'allow_self_signed' => true
               )
           );                         
           $mail->SMTPSecure = 'ssl';                           
           $mail->Port = 465;                                   

           $mail->setFrom('YOUR-EMAIL-SMTP');
           
           //Recipients
           $mail->addAddress($email);              
           $mail->addReplyTo('YOUR-EMAIL-SMTP');
          
           //Content
           $mail->isHTML(true);                                  
           $mail->Subject = 'ECommerce Site Password Reset';
           $mail->Body    = $message;

           $mail->send();

           $_SESSION['success'] = 'Password reset link sent';
        
       } 
       catch (Exception $e) {
           $_SESSION['error'] = 'Message could not be sent. Mailer Error: '.$mail->ErrorInfo;
       }
   }
   catch(PDOException $e){
    $_SESSION['error'] = $e->getMessage();
   }
  }
  else{
   $_SESSION['error'] = 'Email not found';
  }

  $pdo->close();

 }
 else{
  $_SESSION['error'] = 'Input email associated with account';
 }

 header('location: password_forgot.php');

?> 

15. signup.php file ini berfungsi sebagai form register
 <?php include 'includes/session.php'; ?>
<?php
  if(isset($_SESSION['user'])){
    header('location: cart_view.php');
  }

  if(isset($_SESSION['captcha'])){
    $now = time();
    if($now >= $_SESSION['captcha']){
      unset($_SESSION['captcha']);
    }
  }

?>
<?php include 'includes/header.php'; ?>
<body class="hold-transition register-page">
<div class="register-box">
   <?php
      if(isset($_SESSION['error'])){
        echo "
          <div class='callout callout-danger text-center'>
            <p>".$_SESSION['error']."</p> 
          </div>
        ";
        unset($_SESSION['error']);
      }

      if(isset($_SESSION['success'])){
        echo "
          <div class='callout callout-success text-center'>
            <p>".$_SESSION['success']."</p> 
          </div>
        ";
        unset($_SESSION['success']);
      }
    ?>
   <div class="register-box-body">
     <p class="login-box-msg">Register a new membership</p>

     <form action="register.php" method="POST">
          <div class="form-group has-feedback">
            <input type="text" class="form-control" name="firstname" placeholder="Firstname" value="<?php echo (isset($_SESSION['firstname'])) ? $_SESSION['firstname'] : '' ?>" required>
            <span class="glyphicon glyphicon-user form-control-feedback"></span>
          </div>
          <div class="form-group has-feedback">
            <input type="text" class="form-control" name="lastname" placeholder="Lastname" value="<?php echo (isset($_SESSION['lastname'])) ? $_SESSION['lastname'] : '' ?>"  required>
            <span class="glyphicon glyphicon-user form-control-feedback"></span>
          </div>
        <div class="form-group has-feedback">
          <input type="email" class="form-control" name="email" placeholder="Email" value="<?php echo (isset($_SESSION['email'])) ? $_SESSION['email'] : '' ?>" required>
          <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
        </div>
          <div class="form-group has-feedback">
            <input type="password" class="form-control" name="password" placeholder="Password" required>
            <span class="glyphicon glyphicon-lock form-control-feedback"></span>
          </div>
          <div class="form-group has-feedback">
            <input type="password" class="form-control" name="repassword" placeholder="Retype password" required>
            <span class="glyphicon glyphicon-log-in form-control-feedback"></span>
          </div>
          <?php
            if(!isset($_SESSION['captcha'])){
              echo '
                <di class="form-group" style="width:100%;">
                  <div class="g-recaptcha" data-sitekey="YOUR-SITE-KEY"></div>
                </di>
              ';
            }
          ?>
          <hr>
        <div class="row">
       <div class="col-xs-4">
             <button type="submit" class="btn btn-primary btn-block btn-flat" name="signup"><i class="fa fa-pencil"></i> Sign Up</button>
          </div>
        </div>
     </form>
      <br>
      <a href="login.php">I already have a membership</a><br>
      <a href="index.php">Home</a>
   </div>
</div>
 
<?php include 'includes/scripts.php' ?>
</body>
</html> 

16. verify.php file ini berisi script yang berfungsi untuk verifikasi user yang melakukan pendaftaran.
 <?php
include 'includes/session.php';
$conn = $pdo->open();

if(isset($_POST['login'])){
 
 $email = $_POST['email'];
 $password = $_POST['password'];

 try{

  $stmt = $conn->prepare("SELECT *, COUNT(*) AS numrows FROM users WHERE email = :email");
  $stmt->execute(['email'=>$email]);
  $row = $stmt->fetch();
  if($row['numrows'] > 0){
   if($row['status']){
    if(password_verify($password, $row['password'])){
     if($row['type']){
      $_SESSION['admin'] = $row['id'];
     }
     else{
      $_SESSION['user'] = $row['id'];
     }
    }
    else{
     $_SESSION['error'] = 'Incorrect Password';
    }
   }
   else{
    $_SESSION['error'] = 'Account not activated.';
   }
  }
  else{
   $_SESSION['error'] = 'Email not found';
  }
 }
 catch(PDOException $e){
  echo "There is some problem in connection: " . $e->getMessage();
 }

}
else{
 $_SESSION['error'] = 'Input login credentails first';
}

$pdo->close();

header('location: login.php');

?> 

Kalian bisa melihat demonya pada video yang telah kami buat, kalian juga bisa melihat demonya dengan mencobanya langsung bahkan kalian bisa mendownloadnya langsung secara free.




Semoga apa yang kami share diatas bermanfaat bagi anda jangan lupa share dengan orang terdekat anda, support kami juga dengan menekan iklan yang muncul pada website kami. Silahkan tinggalkan komentar dan mulai berdiskusi dengan kami.



Baca Juga

closed