Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //index.php
- <?php
- if (isset($_POST['tag']) && $_POST['tag'] != '') {
- $tag = $_POST['tag'];
- require_once 'include/DB_Functions.php';
- $db = new DB_Functions();
- $response = array("tag" => $tag, "error" => FALSE);
- if ($tag == 'login') {
- $email = $_POST['email'];
- $password = $_POST['password'];
- $user = $db->getUserByEmailAndPassword($email, $password);
- if ($user != false) {
- $response["error"] = FALSE;
- $response["uid"] = $user["unique_id"];
- $response["user"]["name"] = $user["name"];
- $response["user"]["email"] = $user["email"];
- $response["user"]["created_at"] = $user["created_at"];
- $response["user"]["updated_at"] = $user["updated_at"];
- echo json_encode($response);
- } else {
- $response["error"] = TRUE;
- $response["error_msg"] = "Incorrect email or password!";
- echo json_encode($response);
- }
- } else if ($tag == 'register') {
- $name = $_POST['name'];
- $email = $_POST['email'];
- $password = $_POST['password'];
- if ($db->isUserExisted($email)) {
- $response["error"] = TRUE;
- $response["error_msg"] = "User already existed";
- echo json_encode($response);
- } else {
- $user = $db->storeUser($name, $email, $password);
- if ($user) {
- $response["error"] = FALSE;
- $response["uid"] = $user["unique_id"];
- $response["user"]["name"] = $user["name"];
- $response["user"]["email"] = $user["email"];
- $response["user"]["created_at"] = $user["created_at"];
- $response["user"]["updated_at"] = $user["updated_at"];
- echo json_encode($response);
- } else {
- $response["error"] = TRUE;
- $response["error_msg"] = "Error occured in Registartion";
- echo json_encode($response);
- }
- }
- } else {
- $response["error"] = TRUE;
- $response["error_msg"] = "Unknow 'tag' value. It should be either 'login' or 'register'";
- echo json_encode($response);
- }
- } else {
- $response["error"] = TRUE;
- $response["error_msg"] = "Required parameter 'tag' is missing!";
- echo json_encode($response);
- }
- ?>
- 2) Config.php
- <?php
- define("DB_HOST", "localhost");
- define("DB_USER", "root");
- define("DB_PASSWORD", "toor");
- define("DB_DATABASE", "android_api");
- ?>
- 3)DB_Connect.php
- <?php
- class DB_Connect {
- function __construct() {
- }
- function __destruct() {
- }
- public function connect() {
- require_once 'include/Config.php';
- $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysql_error());
- mysql_select_db(DB_DATABASE) or die(mysql_error());
- return $con;
- }
- public function close() {
- mysql_close();
- }
- }
- ?>
- 4)DB_Functions.php
- <?php
- class DB_Functions {
- private $db;
- function __construct() {
- require_once 'DB_Connect.php';
- $this->db = new DB_Connect();
- $this->db->connect();
- }
- function __destruct() {
- }
- public function storeUser($name, $email, $password) {
- $uuid = uniqid('', true);
- $hash = $this->hashSSHA($password);
- $encrypted_password = $hash["encrypted"];
- $salt = $hash["salt"];
- $result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())");
- if ($result) {
- $uid = mysql_insert_id();
- $result = mysql_query("SELECT * FROM users WHERE uid = $uid");
- return mysql_fetch_array($result);
- } else {
- return false;
- }
- }
- public function getUserByEmailAndPassword($email, $password) {
- $result = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
- $no_of_rows = mysql_num_rows($result);
- if ($no_of_rows > 0) {
- $result = mysql_fetch_array($result);
- $salt = $result['salt'];
- $encrypted_password = $result['encrypted_password'];
- $hash = $this->checkhashSSHA($salt, $password);
- if ($encrypted_password == $hash) {
- return $result;
- }
- } else {
- return false;
- }
- }
- public function isUserExisted($email) {
- $result = mysql_query("SELECT email from users WHERE email = '$email'");
- $no_of_rows = mysql_num_rows($result);
- if ($no_of_rows > 0) {
- return true;
- } else {
- return false;
- }
- }
- public function hashSSHA($password) {
- $salt = sha1(rand());
- $salt = substr($salt, 0, 10);
- $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
- $hash = array("salt" => $salt, "encrypted" => $encrypted);
- return $hash;
- }
- public function checkhashSSHA($salt, $password) {
- $hash = base64_encode(sha1($password . $salt, true) . $salt);
- return $hash;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment