Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.53 KB | None | 0 0
  1. <?php # LOGIN HELPER FUNCTIONS.
  2.  
  3. # Function to load specified or default URL.
  4. function load( $page = 'login.php' )
  5. {
  6.   # Begin URL with protocol, domain, and current directory.
  7.  $url = 'http://' . $_SERVER[ 'HTTP_HOST' ] . dirname( $_SERVER[ 'PHP_SELF' ] ) ;
  8.  
  9.   # Remove trailing slashes then append page name to URL.
  10.  $url = rtrim( $url, '/\\' ) ;
  11.   $url .= '/' . $page ;
  12.  
  13.   # Execute redirect then quit.
  14.  header( "Location: $url" ) ;
  15.   exit() ;
  16. }
  17.  
  18. # Function to check email address and password.
  19. function validate( $dbc, $email = '', $pwd = '')
  20. {
  21.   # Initialize errors array.
  22.  $errors = array() ;
  23.  
  24.   # Check email field.
  25.  if ( empty( $email ) )
  26.   { $errors[] = 'Enter your email address.' ; }
  27.   else  { $e = mysqli_real_escape_string( $dbc, trim( $email ) ) ; }
  28.  
  29.   # Check password field.
  30.  if ( empty( $pwd ) )
  31.   { $errors[] = 'Enter your password.' ; }
  32.   else { $p = mysqli_real_escape_string( $dbc, trim( $pwd ) ) ; }
  33.  
  34.   # On success retrieve user_id, first_name, and last name from 'users' database.
  35.  if ( empty( $errors ) )
  36.   {
  37.     $q = "SELECT user_id, first_name, last_name FROM users WHERE email='$e' AND pass=SHA1('$p')" ;  
  38.     $r = mysqli_query ( $dbc, $q ) ;
  39.     if ( @mysqli_num_rows( $r ) == 1 )
  40.     {
  41.       $row = mysqli_fetch_array ( $r, MYSQLI_ASSOC ) ;
  42.       return array( true, $row ) ;
  43.     }
  44.     # Or on failure set error message.
  45.    else { $errors[] = 'Email address and password not found.' ; }
  46.   }
  47.   # On failure retrieve error message/s.
  48.  return array( false, $errors ) ;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement