Advertisement
Guest User

Untitled

a guest
Mar 14th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. <?php
  2. //session_start allows us to access the session array
  3. session_start();
  4.  
  5. //inluding connection.php connects us to the database
  6. require_once('connection.php');
  7.  
  8. //store success messages, error messages in data array
  9. $data = NULL;
  10.  
  11. //redirect user to a page and pass data via session
  12. function redirect($session_data, $url)
  13. {
  14. $_SESSION = $session_data;
  15. header('location:'.$url);
  16. }
  17.  
  18. //if re_password fied isset process registration else login
  19. if(isset($_POST['re_password']))
  20. {
  21. //validate email and password, set validation errors
  22. if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === FALSE)
  23.  
  24. $data['errors'][] = "Invalid email";
  25. if($_POST['password'] != $_POST['re_password'] OR $_POST['password'] == NULL)
  26. $data['errors'][] = "Passwords do not match";
  27.  
  28. //if no errors and email is not in use then add user
  29. if($data['errors'] == NULL)
  30. {
  31. $check_user = $connection->query("SELECT * FROM users WHERE users.email = '".$_POST['email']."' ")->fetch_assoc();
  32.  
  33. if($check_user == NULL)
  34. {
  35. $new_user = $connection->query("INSERT INTO users (username, first_name, last_name, email, password) ('','".$_POST['username']."','".$_POST['first_name']."', '".$_POST['last_name']."', '".$_POST['email']."', '".md5($_POST['password'])."')");
  36.  
  37. if($new_user === TRUE)
  38. {
  39. $data['registered'] = $_POST['email'];
  40. redirect($data, 'login.php');
  41. }
  42. }
  43. else
  44. {
  45. $data['errors'][] = "Email already in use";
  46. redirect($data, 'register.php');
  47. }
  48. }
  49. else
  50. {
  51. $date['success'] = "You are now Registered... Please Login";
  52. redirect($data, 'register.php');
  53. }
  54. }
  55. else
  56. {
  57. //check if user exist in database with given email and password
  58. $check_user = $connection->query("SELECT * FROM users WHERE users.email = '".$_POST['email']."' AND users.password = '".md5($_POST['password'])."' ")->fetch_assoc();
  59. //if user exist set session variables and redirect user to profile page
  60. if($check_user != NULL)
  61. {
  62. $_SESSION['id'] = $check_user['id'];
  63. $_SESSION['email'] = $check_user['email'];
  64. $_SESSION['login'] = TRUE;
  65.  
  66. if($_SESSION['login'] === TRUE)
  67. header('location: user_profile.php?id='.$check_user['id']);
  68. }
  69. else
  70. {
  71. $data['errors'][] = "Invalid email or password";
  72. redirect($data, 'login.php');
  73. }
  74. }
  75. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement