Advertisement
Guest User

Untitled

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