Advertisement
Guest User

Untitled

a guest
May 14th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. require("Conn.php");
  5. require("MySQLDao.php");
  6.  
  7. if(isset($_POST['name'])){
  8. $name = $_POST['name'];
  9. $name = htmlentities($name);
  10. }
  11. if(isset($_POST['email'])) {
  12. $email = $_POST['email']
  13. $email = htmlentities($email);
  14. }
  15. if(isset($_POST['password'])){
  16. $password = $_POST["password"];
  17. $password = htmlentities($password);
  18. }
  19.  
  20.  
  21. $returnValue = array();
  22.  
  23. if(empty($email) || empty($password) || empty($name)) {
  24. $returnValue["status"] = "error";
  25. $returnValue["message"] = "Missing required field";
  26. echo json_encode($returnValue);
  27. return;
  28. }
  29.  
  30. $dao = new MySQLDao();
  31. $dao->openConnection();
  32. $userDetails = $dao->getUserDetails($email);
  33.  
  34. if(!empty($userDetails)) {
  35. $returnValue["status"] = "error";
  36. $returnValue["message"] = "User already exists";
  37. echo json_encode($returnValue);
  38. return;
  39. }
  40.  
  41. $secure_password = md5($password); // I do this, so that user password cannot be read even by me
  42.  
  43. $result = $dao->registerUser($email,$name,$secure_password);
  44.  
  45. if($result) {
  46. $returnValue["status"] = "Success";
  47. $returnValue["message"] = "User is registered";
  48. echo json_encode($returnValue);
  49. return;
  50. }
  51.  
  52. $dao->closeConnection();
  53.  
  54. ?>
  55.  
  56. <?php
  57. class MySQLDao {
  58. var $dbhost = null;
  59. var $dbuser = null;
  60. var $dbpass = null;
  61. var $conn = null;
  62. var $dbname = null;
  63. var $result = null;
  64.  
  65. function __construct() {
  66. $this->dbhost = Conn::$dbhost;
  67. $this->dbuser = Conn::$dbuser;
  68. $this->dbpass = Conn::$dbpass;
  69. $this->dbname = Conn::$dbname;
  70. }
  71.  
  72. public function openConnection() {
  73.  
  74. $this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
  75.  
  76. if (mysqli_connect_errno())
  77. echo new Exception("Could not establish connection with database");
  78. }
  79.  
  80. public function getConnection() {
  81. return $this->conn;
  82. }
  83.  
  84. public function closeConnection() {
  85. if ($this->conn != null)
  86. $this->conn->close();
  87. }
  88.  
  89. public function getUserDetails($email) {
  90. $returnValue = array();
  91. $sql = "select * from users where email='" . $email . "'";
  92.  
  93. $result = $this->conn->query($sql);
  94.  
  95. if ($result != null && (mysqli_num_rows($result) >= 1)) {
  96. $row = $result->fetch_array(MYSQLI_ASSOC);
  97.  
  98. if (!empty($row)) {
  99. $returnValue = $row;
  100. }
  101.  
  102. }
  103.  
  104. return $returnValue;
  105. }
  106.  
  107. public function getUserDetailsWithPassword($email, $userPassword,$name) {
  108.  
  109. $returnValue = array();
  110. $sql = "select id,email,name from users where name='" . $name . "' and email='" . $email . "' and password='" .$userPassword . "'";
  111.  
  112. $result = $this->conn->query($sql);
  113.  
  114. if ($result != null && (mysqli_num_rows($result) >= 1)) {
  115. $row = $result->fetch_array(MYSQLI_ASSOC);
  116.  
  117. if (!empty($row)) {
  118. $returnValue = $row;
  119. }
  120. }
  121.  
  122. return $returnValue;
  123. }
  124.  
  125. public function registerUser($email, $password, $name) {
  126. $sql = "insert into users set email=?, name=?,password=?";
  127. $statement = $this->conn->prepare($sql);
  128.  
  129. if (!$statement)
  130. throw new Exception($statement->error);
  131. $statement->bind_param("sss", $email, $name, $password);
  132. $returnValue = $statement->execute();
  133.  
  134. return $returnValue;
  135.  
  136. }
  137. }
  138. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement