Facebook
From Ezeomeke Stephen Ozioma , 2 Years ago, written in PHP.
This paste is a reply to Re: undefined array key from Ezeomeke Stephen Ozioma - view diff
Embed
Download Paste or View Raw
Hits: 223
  1. //this is where email is suppose to be post
  2.  
  3.  
  4.  
  5. <?php
  6.  
  7.  
  8. require 'config/db.php';
  9.  
  10. $errors = array();
  11. $username = "";
  12. $email = "";
  13.  
  14.  
  15. // if user clicks on the sign up button
  16. if (isset($_POST ['signup-btn'])) {
  17.  
  18. $username= $_POST['username'];
  19. $email = $_POST['email'];
  20. $password = $_POST['password'];
  21. $passwordConf = $_POST["passwordConf"];
  22.  
  23. }
  24.  
  25.  
  26.  
  27. //validation
  28. if (empty($username)) {
  29.         $errors['username'] = 'Username required';
  30.        
  31. }
  32.  
  33. if (empty($email)) {
  34.         $errors['email'] = 'Email required';
  35.        
  36. }
  37.  
  38. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  39.         $errors['email'] = 'Email address is invalid';
  40. }
  41.  
  42. if (empty($password)) {
  43.         $errors['password'] = 'Password required';
  44. }
  45.  
  46. if (isset($password) && isset($passwordConf) && $password !== $passwordConf) {
  47.         $errors['password'] = "The two password do not match";
  48. }
  49.  
  50. $emailQuery = "SELECT * FROM users WHERE email=? LIMIT 1";
  51. $stmt = $conn->prepare($emailQuery);
  52. $stmt->bind_param('s', $email);
  53. $stmt->execute();
  54. $result = $stmt->get_result();
  55. $userCount = $result->num_rows;
  56. $stmt->close();
  57.  
  58. if ($userCount > 0) {
  59.         $errors['email'] = "Email already esists";
  60. }
  61.  
  62. if (count($errors) === 0) {
  63.         $password = password_hash($passord, PASSWORD_DEFAULT);
  64.         $token = bin2hex(random_bytes(50));
  65.         $verified = false;
  66.  
  67.         $sql = "INSERT INTO users (username, email, verified, token, password) VALUES (?, ?, ?, ?, ?)";
  68.         $stmt = $conn->prepare($sql);
  69.         $stmt->bind_param('ssbss', $username, $email, $verified, $token, $password);
  70.  
  71.         if ($stmt->excute()){
  72.                 //login user
  73.                 $user_id = $conn->insert_id;
  74.                 $_SESSION['id'] = $user_id;
  75.                 $_SESSION ['username'] = $username;
  76.                 $_SESSION ['email'] = $email;
  77.                 $_SESSION ['verified'] = $verified;
  78.                 // set flash message
  79.                 $_SESSION['message'] = "You are now logged in! Continue with your upload";
  80.                 $_SESSION['alert-class'] = "alert-success";
  81.                 header('location: profilepage.php');
  82.                 exit();
  83.         }
  84.         else {
  85.                 $errors['db_error'] = "Database error: failed to register";
  86.         }
  87. }
  88.  
  89.  
  90. // if user clicks on the login button
  91.  
  92. if (isset($_POST ['login-btn'])) {
  93.  
  94.         $username= $_POST['username'];
  95.         $password = $_POST['password'];
  96.        
  97.        
  98.        
  99.        
  100.         //validation
  101.         if (empty($username)) {
  102.                 $errors['username'] = 'Username required';
  103.                
  104.         }
  105.  
  106.                
  107.         if (empty($password)) {
  108.                 $errors['password'] = 'Password required';
  109.         }
  110.  
  111. if(count($errors) ===0){
  112.         $sql = "SELECT * FROM users WHERE email=? OR username=? LIMIT 1";
  113.         $stmt = $conn->prepare($sql);
  114.         $stmt->bind_param('ss', $username, $username);
  115.         $stmt->execute();
  116.         $result = $stmt->get_result();
  117.         $user = $result->fetch_assoc();
  118.  
  119.                 if (password_verify($password, $user['password'])) {
  120.                 //login sucess
  121.                
  122.                
  123.                         $_SESSION['id'] = $user['id'];
  124.                         $_SESSION['username'] = $user['username'];
  125.                         $_SESSION['email'] = $user['email'];
  126.                         $_SESSION['verified'] = $user['verified'];
  127.                 // set flash message
  128.                         $_SESSION['message'] = "You are now logged in! Continue with your upload";
  129.                         $_SESSION['alert-class'] = "alert-success";
  130.                         header('location: profilepage.php');
  131.                         exit();
  132.                 }
  133.                 else {
  134.                 $errors['login_fail'] = "Wrong credentials";
  135.                 }
  136.                
  137.        
  138.         }
  139. }              
  140.  
  141. // logout user
  142. if (isset($_GET['logout'])){
  143.         session_destroy();
  144.         unset($_SESSION['id']);
  145.         unset($_SESSION['username']);
  146.         unset($_SESSION['email']);
  147.         unset($_SESSION['verified']);
  148.         header('location: home.php');
  149.         exit();
  150. }