I downloaded this code from androidhive. It's login/registration code for android apps. Now the php code is from two years ago and so I was originally gettin depreciated errors and so I began updating the code to mysqli.So far when converting, I've been changing the mysql functions to mysqli, and if need be adjusting the parameters. I'm fairly new to php in general so I've been running into a few problems, the main one being that of isUserExisted function in DB_functions the variable db is undefined for the mysqli_query. I'm not sure why I'm getting this error because I've defined it in the constructor. I've looked at similar questions and the answers that fixed their problems were usually syntax errors, which when I compare this code, I don't seem to have. Any help would be greatly appreciated. Here's the DB_Functions.php and the DB_Connect.php respectively:
<?php
class DB_Functions {
private $db = null;
//put your code here
// constructor
function __construct() {
require 'DB_Connect.php';
// connecting to database
$this->db = new DB_Connect();
$this->db->connectThis();
}
// destructor
function __destruct() {
}
/**
* Storing new user
* returns user details
*/
public function storeUser($name, $email, $password) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$result = mysqli_query($db,"INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())");
// check for successful store
if ($result) {
// get user details
$uid = mysqli_insert_id(); // last inserted id
$result = mysqli_query($db,"SELECT * FROM users WHERE uid = $uid");
// return user details
return mysqli_fetch_array($result);
} else {
return false;
}
}
/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($email, $password) {
$result = mysqli_query($db,"SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
// check for result
$no_of_rows = mysqli_num_rows($result);
if ($no_of_rows > 0) {
$result = mysqli_fetch_array($result);
$salt = $result['salt'];
$encrypted_password = $result['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $result;
}
} else {
// user not found
return false;
}
}
/**
* Check user is existed or not
*/
public function isUserExisted($email) {
$result = mysqli_query($db,"SELECT email from users WHERE email = '$email'");
$no_of_rows = mysqli_num_rows($result);
if ($no_of_rows > 0) {
// user existed
return true;
} else {
// user not existed
return false;
}
}
/**
* Encrypting password
* @param password
* returns salt and encrypted password
*/
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;
}
/**
* Decrypting password
* @param salt, password
* returns hash string
*/
public function checkhashSSHA($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}
}
?>
<?php
class DB_Connect {
// constructor
function __construct() {
}
// destructor
function __destruct() {
// $this->close();
}
// Connecting to database
public function connectThis() {
require 'include/Config.php';
// connecting to mysql
$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE) or die(mysqli_error());
// selecting database
// return database handler
return $db;
}
// Closing database connection
public function close() {
mysqli_close();
}
}
?>
</div>