Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require("Conn.php");
- require("MySQLDao.php");
- if(isset($_POST['name'])){
- $name = $_POST['name'];
- $name = htmlentities($name);
- }
- if(isset($_POST['email'])) {
- $email = $_POST['email']
- $email = htmlentities($email);
- }
- if(isset($_POST['password'])){
- $password = $_POST["password"];
- $password = htmlentities($password);
- }
- $returnValue = array();
- if(empty($email) || empty($password) || empty($name)) {
- $returnValue["status"] = "error";
- $returnValue["message"] = "Missing required field";
- echo json_encode($returnValue);
- return;
- }
- $dao = new MySQLDao();
- $dao->openConnection();
- $userDetails = $dao->getUserDetails($email);
- if(!empty($userDetails)) {
- $returnValue["status"] = "error";
- $returnValue["message"] = "User already exists";
- echo json_encode($returnValue);
- return;
- }
- $secure_password = md5($password); // I do this, so that user password cannot be read even by me
- $result = $dao->registerUser($email,$name,$secure_password);
- if($result) {
- $returnValue["status"] = "Success";
- $returnValue["message"] = "User is registered";
- echo json_encode($returnValue);
- return;
- }
- $dao->closeConnection();
- ?>
- <?php
- class MySQLDao {
- var $dbhost = null;
- var $dbuser = null;
- var $dbpass = null;
- var $conn = null;
- var $dbname = null;
- var $result = null;
- function __construct() {
- $this->dbhost = Conn::$dbhost;
- $this->dbuser = Conn::$dbuser;
- $this->dbpass = Conn::$dbpass;
- $this->dbname = Conn::$dbname;
- }
- public function openConnection() {
- $this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
- if (mysqli_connect_errno())
- echo new Exception("Could not establish connection with database");
- }
- public function getConnection() {
- return $this->conn;
- }
- public function closeConnection() {
- if ($this->conn != null)
- $this->conn->close();
- }
- public function getUserDetails($email) {
- $returnValue = array();
- $sql = "select * from users where email='" . $email . "'";
- $result = $this->conn->query($sql);
- if ($result != null && (mysqli_num_rows($result) >= 1)) {
- $row = $result->fetch_array(MYSQLI_ASSOC);
- if (!empty($row)) {
- $returnValue = $row;
- }
- }
- return $returnValue;
- }
- public function getUserDetailsWithPassword($email, $userPassword,$name) {
- $returnValue = array();
- $sql = "select id,email,name from users where name='" . $name . "' and email='" . $email . "' and password='" .$userPassword . "'";
- $result = $this->conn->query($sql);
- if ($result != null && (mysqli_num_rows($result) >= 1)) {
- $row = $result->fetch_array(MYSQLI_ASSOC);
- if (!empty($row)) {
- $returnValue = $row;
- }
- }
- return $returnValue;
- }
- public function registerUser($email, $password, $name) {
- $sql = "insert into users set email=?, name=?,password=?";
- $statement = $this->conn->prepare($sql);
- if (!$statement)
- throw new Exception($statement->error);
- $statement->bind_param("sss", $email, $name, $password);
- $returnValue = $statement->execute();
- return $returnValue;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement