Advertisement
Guest User

Untitled

a guest
Nov 5th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. <?php
  2.  
  3. function absolute_url($page = 'index.php') {
  4. //header('Location: http:\localhost');
  5. //exit(); //terminates the script
  6.  
  7. $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
  8. $url = rtrim($url, '/\');
  9. $url .= '/' . $page;
  10.  
  11. return $url;
  12. }
  13.  
  14. function checkLogin($email = '', $password = '') {
  15. $errors = array();
  16.  
  17. if (empty($email)){
  18. $errors[] = 'You must enter your email';
  19. }
  20. if (empty($password)){
  21. $errors[] = 'You must enter a password';
  22. }
  23. if (empty($errors)) {
  24. ////set up database econnection
  25. require_once 'DO_Classes/mysqli_connect.php';
  26.  
  27. $db = new Database();
  28. $dbc = $db->getConnection();
  29.  
  30. $stmt = $dbc->prepare("SELECT user_ID FROM User WHERE user_email=? AND AES_DECRYPT(user_password, 'p0ly')=?");
  31.  
  32. if ($stmt) {
  33. $stmt->bind_param('ss', $email, $password);
  34.  
  35. if ($stmt->execute()) {
  36. $stmt->store_result();
  37. $stmt->bind_result($user_ID);
  38. $stmt->fetch();
  39. $stmt->close();
  40. if(!empty($user_ID)){
  41. return array(true, $user_ID);
  42. }else{
  43. /*
  44. * <div class="alert alert-success alert-dismissable">
  45. <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
  46. Invalid email or password
  47. </div>
  48. */
  49. $errors[] = '<div class="alert alert-danger alert-dismissable">
  50. <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
  51. <p align="center">Invalid email or password</p>
  52. </div>';
  53. }
  54.  
  55. } else {
  56. $errors[] = 'Passwords do not match';
  57. }
  58. }else {
  59. echo '<p class="error"> Oh dear. There was a databse error</p>';
  60. echo '<p class = "error">' . mysqli_error($stmt) . '</p>';
  61. }
  62. }
  63. return array(false, $errors);
  64. }
  65.  
  66. ?>
  67.  
  68. <?php
  69. if (isset($_POST['submitted'])) {
  70. //require_once is similar to 'include' but ensures the code is not copied multiple times
  71. require_once('Functions/loginFunctions.php');
  72.  
  73. //list() is a way of assigning multiple values at the same time
  74. //checkLogin() function returns an array so list here assigns the values in the array to $check and $data
  75. list($check, $data) = checkLogin($_POST['email'], $_POST['password']);
  76.  
  77.  
  78. if ($check) {
  79. //setcookie('FName', $data['FName'], time()+ 900 ) ; //cookie expires after 15 mins
  80. //setcookie('LName', $data['LName'], time() + 900 ) ;
  81. session_start();
  82. require_once 'Classes/DO_Users.php';
  83. $user = new DO_User();
  84. $user->get($data);
  85. //use session variables instead of cookies
  86. //these variables should now be available to all pages in the application as long as the users session exists
  87. $_SESSION['userID'] = $user->userID;
  88. $_SESSION['userType'] = $user->userTypeID;
  89. $_SESSION['last_activity'] = time(); //your last activity was now, having logged in.
  90. $_SESSION['expire_time'] = 60 * 5; //expire time in seconds: three hours (you must change this)
  91. //to enable $_SESSION array to be populated we always need to call start_session() - this is done in header.php
  92. //print_r is will print out the contents of an array
  93. //print_r($_SESSION);
  94. //
  95. //Redirect to another page
  96.  
  97. $url = absolute_url('index.php'); //function defined in Loginfunctions.php to give absolute path for required page
  98. //this version of the header function is used to redirect to another page
  99. header("Location: $url"); //since we have entered correct login details we are now being directed to the home page
  100. exit();
  101. } else {
  102. $errors = $data;
  103. }
  104. }
  105.  
  106.  
  107. if (!empty($errors)) {
  108. //foreach is a simplified version of the 'for' loop
  109. foreach ($errors as $err) {
  110. echo "$err <br />";
  111. }
  112.  
  113. echo '</p>';
  114. }
  115.  
  116. //display the form
  117. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement