Advertisement
Guest User

login function

a guest
Oct 9th, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.11 KB | None | 0 0
  1. <?php
  2.     class User {
  3.         public $authorized = false;
  4.         public $uid;
  5.         public $username;
  6.  
  7.  
  8.         public function __construct() {
  9.             $this->db = new PDO($dsn, $db_user, $db_pass);
  10.             $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
  11.  
  12.             if (isset($_SESSION['uid'])) {
  13.                 $this->authorized = true;
  14.                 $this->uid = $_SESSION['uid'];
  15.                 $this->username = $_SESSION['username'];
  16.             } else if (isset($_POST['reset'])) {
  17.                 $user = $_POST['reset'];
  18.                 $this->reset($user);
  19.             } else if (isset($_POST['username']) && isset($_POST['password'])) {
  20.                 $user = $_POST['username'];
  21.                 $pass = $_POST['password'];
  22.                 $this->login($user, $pass);
  23.             }
  24.         }
  25.  
  26.  
  27.         private function login($user, $pass) {
  28.             $st = $this->db->prepare('SELECT `uid`, `username`, `password`
  29.                    FROM users
  30.                    WHERE username = :u');
  31.             $st->execute(array(':u' => $user));
  32.             $row = $st->fetch();
  33.  
  34.             if ($row && $row->password == sha1($pass)) {
  35.                 $this->authorized = true;
  36.  
  37.                 $this->uid = $row->uid;
  38.                 $_SESSION['uid'] = $this->uid;
  39.                
  40.                 $this->username = $row->username;
  41.                 $_SESSION['username'] = $this->username;
  42.  
  43.                 return true;
  44.             } else {
  45.                 return false;
  46.             }
  47.         }
  48.  
  49.  
  50.         private function reset($user) {
  51.             $st = $this->db->prepare('SELECT `uid`, `username`, `email`
  52.                    FROM users
  53.                    WHERE username = :u');
  54.             $st->execute(array(':u' => $user));
  55.             $row = $st->fetch();
  56.  
  57.             if ($row) {
  58.                 $token = $this->generateRequest();
  59.  
  60.                 $st = $this->db->prepare('UPDATE users SET `reset` = :reset, password = 0 WHERE uid = :uid LIMIT 1');
  61.                 $status = $st->execute(array(':uid' => $row->uid, ':reset' => $token));
  62.  
  63.                 $body = "We received a request for your account details.<br/><br/>Username: {$row->username}<br/>To reset your password, click on this link: <a href='http://www.example.org/?reset={$token}'>http://www.example.org/?reset={$token}</a>";
  64.  
  65.                 $to = $row->email;
  66.                 $subject = 'Password request';
  67.                 $from = 'no-reply@example.org';
  68.                  
  69.                 // To send HTML mail, the Content-type header must be set
  70.                 $headers  = 'MIME-Version: 1.0' . "\r\n";
  71.                 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  72.                  
  73.                 // Create email headers
  74.                 $headers .= 'From: '.$from."\r\n".
  75.                             'Reply-To: '.$from."\r\n";
  76.  
  77.                 mail($to, $subject, $body, $headers);
  78.             }
  79.         }
  80.  
  81.         private function generateRequest() {
  82.             $token = md5(openssl_random_pseudo_bytes(32));
  83.             return $token;
  84.         }
  85.  
  86.     }
  87. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement