Guest User

Untitled

a guest
Nov 30th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. <?php
  2. // Other settings
  3. session_start();
  4.  
  5. // Connect to the database
  6. class Database {
  7. private $host = "localhost";
  8. private $db_name = "login";
  9. private $username = "root";
  10. private $password = "";
  11. public $conn;
  12.  
  13. public function dbConnection() {
  14. $this->conn = null;
  15. try {
  16. $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
  17. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  18. }
  19. catch(PDOException $exception) {
  20. echo "Connection error: " . $exception->getMessage();
  21. }
  22. return $this->conn;
  23. }
  24. }
  25.  
  26. // Functions for managing users
  27. class USER {
  28. private $conn;
  29. public function __construct() {
  30. $database = new Database();
  31. $db = $database->dbConnection();
  32. $this->conn = $db;
  33. }
  34.  
  35.  
  36. public function runQuery($sql) {
  37. $stmt = $this->conn->prepare($sql);
  38. return $stmt;
  39. }
  40.  
  41. public function register($uname,$umail,$upass) {
  42. try {
  43.  
  44.  
  45. ///// SECURITY ENCRYPTING AND HASHING PW //////
  46. //// Salting and Pepper PW////
  47.  
  48. //salt
  49. $salt=base64_encode(mcrypt_create_iv(16,MCRYPT_DEV_URANDOM));
  50.  
  51. //pepper "static salt"
  52. //$password="This1sApassw0rd";
  53.  
  54. //$new_password = hash("sha256",$password."mySecret".$salt);
  55.  
  56. $password="123456";
  57.  
  58. $pepper="ThisIs a sdæfkjglsfjdhleqrbh 3204i9245m";
  59.  
  60. $new_password = password_hash($upass, PASSWORD_DEFAULT);
  61.  
  62. $stmt = $this->conn->prepare("INSERT INTO users(user_name,user_email,user_pass) VALUES(:uname, :umail, :upass)");
  63.  
  64.  
  65. // Escaping SQLi in PHP
  66. // Use prepared statements and parameterized queries.
  67. // These are SQL statements that are sent to and parsed
  68. // by the database server separately from any parameters.
  69. // This way it is impossible for an attacker to inject
  70. // malicious SQL.
  71.  
  72.  
  73. $stmt->bindparam(":uname", $uname);
  74. $stmt->bindparam(":umail", $umail);
  75. $stmt->bindparam(":upass", $new_password);
  76. $stmt->execute();
  77. return $stmt;
  78. }
  79. catch(PDOException $e) {
  80. echo $e->getMessage();
  81. }
  82. }
  83.  
  84. public function update($dob,$job,$gender,$location, $user_id) {
  85.  
  86. try {
  87. $stmt = $this->conn->prepare("UPDATE users(user_dob,user_job,user_gender,user_location) VALUES(:dob, :job, :gender, :location) WHERE users(user_id=:id)");
  88.  
  89.  
  90. // Escaping SQLi in PHP
  91. // Use prepared statements and parameterized queries.
  92. // These are SQL statements that are sent to and parsed
  93. // by the database server separately from any parameters.
  94. // This way it is impossible for an attacker to inject
  95. // malicious SQL.
  96.  
  97.  
  98. $stmt->bindparam(":dob", $dob);
  99. $stmt->bindparam(":job", $job);
  100. $stmt->bindparam(":gender", $gender);
  101. $stmt->bindparam(":location", $location);
  102. $stmt->bindparam(":id", $user_id);
  103. $stmt->execute();
  104. return $stmt;
  105. }
  106. catch(PDOException $e) {
  107. echo $e->getMessage();
  108. }
  109.  
  110.  
  111.  
  112. }
  113.  
  114. public function doLogin($uname,$umail,$upass) {
  115.  
  116. try {
  117. $stmt = $this->conn->prepare("SELECT user_id, user_name, user_email, user_pass FROM users WHERE user_name=:uname OR user_email=:umail ");
  118.  
  119. // Escaping SQLi in PHP
  120. // Use prepared statements and parameterized queries.
  121. // These are SQL statements that are sent to and parsed
  122. // by the database server separately from any parameters.
  123. // This way it is impossible for an attacker to inject
  124. // malicious SQL.
  125.  
  126.  
  127. $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
  128. $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
  129. if($stmt->rowCount() == 1) {
  130. if(password_verify($upass, $userRow['user_pass'])) {
  131. $_SESSION['user_session'] = $userRow['user_id'];
  132. return true;
  133. } else {
  134. return false;
  135. }
  136. }
  137. }
  138. catch(PDOException $e) {
  139. echo $e->getMessage();
  140. }
  141. }
  142.  
  143. public function is_loggedin() {
  144. if(isset($_SESSION['user_session'])) {
  145. return true;
  146. }
  147. }
  148.  
  149. public function redirect($url) {
  150. header("Location: $url");
  151. exit;
  152. }
  153.  
  154. public function doLogout() {
  155. unset($_SESSION['user_session']);
  156. return true;
  157. }
  158.  
  159. function is_html($string) {
  160. // Check if string contains any html tags.
  161. return preg_match('/<\s?[^\>]*\/?\s?>/i', $string);
  162. }
  163. }
  164. ?>
Add Comment
Please, Sign In to add comment