Guest User

Untitled

a guest
Apr 11th, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.58 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. // Connect to database
  5. $conn=mysql_connect(DB_HOST,DB_USER,DB_PASS);
  6. mysql_select_db(DB_BASE);
  7.  
  8. // Instantiate Classes
  9. $login = new Login();
  10. //$session = new Sessions();
  11.  
  12. // Simple function to sanitize form input
  13. function protect($string) {
  14.     $newString = trim(mysql_escape_string($string));
  15.     return $newString;
  16. }
  17.  
  18. // Determine if loginSubmit has been set and the value of login is 1
  19. if (isset($_POST['loginSubmit']) && $_POST['login'] == 1) {
  20.     // Define $errors variable as an array
  21.     $errors = array();
  22.     // Determine if username or password is empty; if yes, assign error message
  23.     if (empty($_POST['username'])) {
  24.         $errors['username'] = 'You must provide a username';
  25.     }
  26.     if (empty($_POST['password'])) {
  27.         $errors['password'] = 'You must provide a password';
  28.     }
  29.  
  30.     // If there are no errors, begin functions to log user in
  31.     if (empty($errors)) {
  32.         // Sanitize data input
  33.         $username = protect($_POST['username']);
  34.         $password = protect($_POST['password']);
  35.  
  36.         // Check that user exists
  37.         $userExists = $login->loginExists($username, $password);
  38.  
  39.         if ($userExists['userCount'] == 1) {
  40.             // If the user exists, set session variables and redirect user to the appropriate dashboard
  41.                 header("Location:main.php");
  42.             }
  43.         }
  44.         else {
  45.             // If user cannot be logged in, display error message
  46.             $page->contentSet('<p class="error">There was an error logging you in. Please try again.</p>');
  47.         }
  48. }
  49. ?>
Add Comment
Please, Sign In to add comment