I'm working on Php web services which are working on localhost when I upload it to Cpanel server it is not responding. it is used is registered but response message is not showing while checking with postman
DB_Function has the main function where it is connected to table to insert data.Which is working.
DB_Function.php
<?php
class DB_Functions {
private $conn;
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$db = new Db_Connect();
$this->conn = $db->connect();
}
// destructor
function __destruct() {
}
/**
* Storing new user
* returns user details
*/
public function storeUser($name, $email, $password, $phone, $address, $address_2,$education, $position, $gender, $bank_account_no, $experience, $company_name, $company_temp, $references_description, $amount ) {
//$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$stmt = $this->conn->prepare("INSERT INTO emp_registration( full_name, email, password, salt, phone, address, address_2, education, position, gender, bank_account_no, experience, company_name, company_temp,references_description,amount, created_at) VALUES(?,?, ?, ?, ?, ?, ?, ?,?,?,?,?,?,?,?,?, NOW())");
$stmt->bind_param("sssssssssssssssi", $name, $email, $encrypted_password, $salt, $phone, $address, $address_2,$education, $position, $gender, $bank_account_no, $experience, $company_name, $company_temp, $references_description, $amount );
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM emp_registration WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return false;
}
}
/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($email, $password) {
$stmt = $this->conn->prepare("SELECT * FROM emp_registration WHERE email = ?");
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
// verifying user password
$salt = $user['salt'];
$encrypted_password = $user['password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $user;
}
} else {
return NULL;
}
}
/**
* Check user is existed or not
*/
public function isUserExisted($email) {
$stmt = $this->conn->prepare("SELECT email from emp_registration WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
// user existed
$stmt->close();
return true;
} else {
// user not existed
$stmt->close();
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;
}
}
?>
In Emp_registration it is sending store_user fuction in DB_Functions class but Response is not comming from it on server.
Emp_registration.php
<?php
require_once 'include/DB_Function.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['phone']) && isset($_POST['address']) && isset($_POST['address_2']) &&
isset($_POST['education']) && isset($_POST['position']) && isset($_POST['gender']) && isset($_POST['bank_account_no']) && isset($_POST['experience']) && isset($_POST['company_name']) && isset($_POST['company_temp']) && isset($_POST['references_description'])
&& isset($_POST['amount']) ) {
// receiving the post params
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$address_2 = $_POST['address_2'];
$education = $_POST['education'];
$position = $_POST['position'];
$gender = $_POST['gender'];
$bank_account_no = $_POST['bank_account_no'];
$experience = $_POST['experience'];
$company_name = $_POST['company_name'];
$company_temp = $_POST['company_temp'];
$references_description = $_POST['references_description'];
$amount = intval( $_POST['amount']);
// check if emp_registration is already existed with the same email
if ($db->isUserExisted($email)) {
// emp_registration already existed
$response["error"] = TRUE;
$response["error_msg"] = "User already existed with " . $email;
echo json_encode($response);
} else {
// create a new emp_registration
$emp_registration = $db->storeUser($name, $email, $password, $phone, $address, $address_2, $education, $position, $gender, $bank_account_no, $experience, $company_name, $company_temp, $references_description, $amount);
if ($emp_registration) {
// emp_registration stored successfully
$response["error"] = FALSE;
$response["emp_id"] = $emp_registration["id"];
$response["emp_registration"]["full_name"] = $emp_registration["full_name"];
$response["emp_registration"]["email"] = $emp_registration["email"];
$response["emp_registration"]["created_at"] = $emp_registration["created_at"];
$response["emp_registration"]["updated_at"] = $emp_registration["updated_at"];
echo json_encode($response);
} else {
// emp_registration failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (name, email or password) is missing!";
echo json_encode($response);
}
?>
.htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^web\.ddagroindore\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.web\.ddagroindore\.com$
RewriteRule ^/?$ "http\:\/\/ddagroindore\.com\/webservice" [R=301,L]
Related
I have this code to make login with my application android ,
but I need to test with some value
<?php
require_once 'DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
// receiving the post params
/* $email = $_POST['email'];
$password = $_POST['password'];*/
$email ="bi#yahoo.fr";
$password ="123456";
// get the user by email and password
$user = $db->getUserByEmailAndPassword($email,$password);
if ($user != false) {
// use is found
$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 {
// user is not found with the credentials
$response["error"] = TRUE;
$response["error_msg"] = "Login credentials are wrong. Please try again!";
echo json_encode($response);
}
/*else {
// required post params is missing
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters email or password is
missing!";
echo json_encode($response);
}*/
?>
file DB_Functions.php is this
<?php
class DB_Functions {
private $conn;
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$db = new Db_Connect();
$this->conn = $db->connect();
}
// 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
$stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
$stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return false;
}
}
/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($email, $password) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
// verifying user password
$salt = $user['salt'];
$encrypted_password = $user['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $user;
}
} else {
return NULL;
}
}
/**
* Check user is existed or not
*/
public function isUserExisted($email) {
$stmt = $this->conn->prepare("SELECT email from users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
// user existed
$stmt->close();
return true;
} else {
// user not existed
$stmt->close();
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;
}
}
?>
how to fix that
the file DB_Connect Contains all informations about database name and password ... all thing are correctly
but when i put 127.0.0.1/myproject/myfileLogin to test with this values
email:bi#yahoo.fr
password :123456
he show me Login credentials are wrong. Please try again! in My data base I have These two
email:bi#yahoo.fr
password :123456
I need help with below codes,I can't figure out why I can't register an account on db.
Below my PHP scripts:
update_user_info.php
<?php
class update_user_info {
public function StoreUserInfo($fullname, $matno, $dept, $phone, $email, $password) {
$hash = $this->hashFunction($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$stmt = $this->conn->prepare("INSERT INTO users(fullname, matno, dept, phone, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, ?, ?, Now())");
$stmt->bind_param("ssssssss", $fullname, $matno, $dept, $phone, $email, $encrypted_password, $salt, $created_at);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT fullname, matno, dept, phone, email, encrypted_password, salt FROM users WHERE matno = ?");
$stmt->bind_param("s", $matno);
$stmt->execute();
$stmt-> bind_result($token2,$token3,$token4,$token5,$token6,$token7,$token8);
while ( $stmt-> fetch() ) {
$user["fullname"] = $token2;
$user["matno"] = $token3;
$user["dept"] = $token4;
$user["phone"] = $token5;
$user["email"] = $token6;
}
$stmt->close();
return $user;
} else {
return false;
}
}
public function hashFunction($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 VerifyUserAuthentication($matno, $password) {
$stmt = $this->conn->prepare("SELECT fullname, matno, dept, phone, email, encrypted_password, salt FROM users WHERE matno = ?");
$stmt->bind_param("s", $matno);
if ($stmt->execute()) {
$stmt-> bind_result($token2,$token3,$token4,$token5,$token6,$token7,$token8);
while ( $stmt-> fetch() ) {
$user["fullname"] = $token2;
$user["matno"] = $token3;
$user["dept"] = $token4;
$user["phone"] = $token5;
$user["email"] = $token6;
$user["encrypted_password"] = $token7;
$user["salt"] = $token8;
}
$stmt->close();
// verifying user password
$salt = $token8;
$encrypted_password = $token7;
$hash = $this->CheckHashFunction($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $user;
}
} else {
return NULL;
}
}
public function checkHashFunction($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}
public function CheckExistingUser($matno) {
$stmt = $this->conn->prepare("SELECT matno from users WHERE matno = ?");
$stmt->bind_param("s", $matno);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
// user existed
$stmt->close();
return true;
} else {
// user not existed
$stmt->close();
return false;
}
}
}
?>
login.php
<?php
require_once 'update_user_info.php';
$db = new update_user_info();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['matno']) && isset($_POST['password'])) {
// receiving the post params
$matno = $_POST['matno'];
$password = $_POST['password'];
// get the user by email and password
$user = $db->VerifyUserAuthentication($matno, $password);
if ($user != false) {
// user is found
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["fullname"] = $user["fullname"];
$response["user"]["email"] = $user["email"];
$response["user"]["matno"] = $user["matno"];
$response["user"]["dept"] = $user["dept"];
$response["user"]["phone"] = $user["phone"];
echo json_encode($response);
} else {
// user is not found with the credentials
$response["error"] = TRUE;
$response["error_msg"] = "Login credentials are wrong. Please try again!";
echo json_encode($response);
}
} else {
// required post params is missing
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters email or password is missing!";
echo json_encode($response);
}
?>
running above on postman and puttin all the required parameters shows below error:
["error_msg"] = "Required parameters email or password is missing!";
register.php
<?php
require_once 'update_user_info.php';
$db = new update_user_info();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['fullname']) && isset($_POST['matnum']) && isset($_POST['depart']) && isset($_POST['phone']) && isset($_POST['email']) && isset($_POST['passworded'])) {
// receiving the post params
$fullname = $_POST['fullname'];
$matno = $_POST['matnum'];
$email = $_POST['email'];
$dept = $_POST['depart'];
$phone = $_POST['phone'];
$password = $_POST['passworded'];
// check if user is already existed with the same email
if ($db->CheckExistingUser($matno)) {
// user already existed
$response["error"] = TRUE;
$response["error_msg"] = "User already existed with " . $matno;
echo json_encode($response);
} else {
// create a new user
$user = $db->StoreUserInfo($fullname, $matno, $dept, $phone, $email, $password);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["user"]["fullname"] = $user["fullname"];
$response["user"]["matno"] = $user["matno"];
$response["user"]["dept"] = $user["dept"];
$response["user"]["phone"] = $user["phone"];
$response["user"]["email"] = $user["email"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (fullname, email or password) is missing!";
echo json_encode($response);
}
?>
Running above code in postman with all params filled shows below error:
$response["error_msg"] = "Required parameters (fullname, email or password) is missing!";
I must be doing something wrong. Thank you for your help.
issues resolved. on postman i needed to select x-wwww-form-urlencoded under body option for my script to work. thanks
In my application I am using 2 php files where one contains all the funtions required for the application and the another is to retrive and send the data back to the user
My first php file is DB_Functions.php
which contains the following code
<?php
class DB_Functions {
private $conn;
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$db = new Db_Connect();
$this->conn = $db->connect();
}
/**
* Storing new user
* returns user details
*/
public function storeUser($name,$sex,$dob,$email,$college,$password,$latitude,$longitude,$pass) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$stmt = $this->conn->prepare("INSERT INTO CFLASH_USERS(Name, sex, dob, mail, college, password, latitude, longitude, pass, salt, created_at) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())");
$stmt->bind_param("ssssssssss", $name, $sex, $dob, $email, $college, $password, $latitude, $longitude, $pass, $salt);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM CFLASH_USERS WHERE mail = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
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;
}
}
?>
My second php file Register.php contains
<?php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password'])) {
// receiving the post params
$name = $_POST['name'];
$sex = $_POST['sex'];
$dob = $_POST['dob'];
$email = $_POST['email'];
$college = $_POST['college'];
$password = $_POST['password'];
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
$pass = $_POST['pass'];
// create a new user
$user = $db->storeUser($name, $sex, $dob, $email, $college ,$password, $latitude, $longitude, $pass);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["uid"] = $user["mail"];
$response["user"]["name"] = $user["Name"];
$response["user"]["sex"] = $user["sex"];
$response["user"]["dob"] = $user["dob"];
$response["user"]["email"] = $user["mail"];
$response["user"]["college"] = $user["college"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (name, email or password) is missing!";
echo json_encode($response);
}
?>
My table structure is
I am unable to insert the data into it. I have checked for the post meathod to be wrong but it not wrong. So I thought that there will be an error with bind_param(). Please let me know where the error is and a solution to rectify it.
Please explain me about "Call to undefined method mysqli_stmt::get_result()"
You are likely missing MySQLND. That method is only available with that driver.
http://php.net/manual/en/mysqli-stmt.get-result.php
I am familiar with this error but it looks like i am blind or something . followings are php codes:
DB_functions.php
<?php
class DB_Functions {
private $conn;
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$db = new Db_Connect();
$this->conn = $db->connect();
}
// destructor
function __destruct() {
}
/*update user data*/
public function updateUser($name, $email, $oldpassword, $newpassword)
{
$uuid = uniqid('', true);
$hash = $this->hashSSHA($newpassword);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$stmt = $this->conn->prepare("update users set name ='$name', email ='$email', encrypted_password = '$encrypted_password' , updated_at = NOW() where email ='$email' ");
$stmt->bind_param("sssss", $name, $email, $encrypted_password , $updated_at);
$stmt->execute();
$stmt ->bind_result($row_name, $row_email, $row_encryptedpassword, $row_updatedat);
$user = array(
'name',
'email',
'encrypted_password',
'updated_at',
);
return $user;
$stmt->close();
}
/**
* 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
$stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, totalpoints, digipoints,total_coupons, created_at, updated_at) VALUES(?, ?, ?, ?, ?,0,0, 0, NOW(), NOW())");
$stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt , $totalpoints, $digipoints, $total_coupons, $created_at,$updated_at );
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT id, name, email, encrypted_password, salt, totalpoints, digipoints, total_coupons, created_at, unique_id, updated_at FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->bind_result($row_user_id, $row_user_name, $row_user_email, $row_user_encryptedpass, $row_user_salt,$row_totalpoints, $row_digipoints,$row_totalcoupons, $row_user_createdat,$row_user_uniqueid, $row_user_updatedat);
$stmt->fetch();
$user = array(
'id' => $row_user_id,
'name' => $row_user_name,
'email' => $row_user_email,
'encrypted_password' => $row_user_encryptedpass,
'salt' => $row_user_salt,
'totalpoints' => $row_totalpoints,
'digipoints' => $row_digipoints,
'total_coupons' => $row_totalcoupons,
'created_at'=>$row_user_createdat,
'unique_id' => $row_user_uniqueid,
'updated_at' => $row_user_updatedat,
);
$stmt->close();
return $user;
} else {
return false;
}
}
/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($email, $password) {
$stmt = $this->conn->prepare("SELECT unique_id, name, email, encrypted_password, salt, totalpoints, digipoints, created_at, unique_id, updated_at FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
$stmt->bind_result($row_user_id, $row_user_name, $row_user_email, $row_user_encryptedpass, $row_user_salt, $row_totalpoints, $row_digipoints,$row_user_createdat,$row_user_uniqueid, $row_user_updatedat);
$stmt->fetch();
$user = array(
'id' => $row_user_id,
'name' => $row_user_name,
'email' => $row_user_email,
'encrypted_password' => $row_user_encryptedpass,
'salt' => $row_user_salt,
'totalpoints' => $row_totalpoints,
'digipoints' => $row_digipoints,
'created_at' => $row_user_createdat,
'unique_id' => $row_user_uniqeid,
'updated_at' => $row_user_updatedat,
);
$stmt->close();
return $user;
} else {
return NULL;
}
}
/**
* Check user is existed or not
*/
public function isUserExisted($email) {
$stmt = $this->conn->prepare("SELECT email from users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
// user existed
$stmt->close();
return true;
} else {
// user not existed
$stmt->close();
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;
}
}
?>
register.php
<?php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password'])) {
// receiving the post params
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
// check if user is already existed with the same email
if ($db->isUserExisted($email)) {
// user already existed
$response["error"] = TRUE;
$response["error_msg"] = "User already existed with " . $email;
echo json_encode($response);
} else {
// create a new user
$user = $db->storeUser($name, $email, $password);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["totalpoints"] = $user["totalpoints"];
$response["user"]["digipoints"] = $user["digipoints"];
$response["user"]["total_coupons"] = $user["total_coupons"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (name, email or password) is missing!";
echo json_encode($response);
}
?>
and here is the warning that is my server error_log
[09-Feb-2016 08:36:43 UTC] PHP Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in /home/hevak/public_html/beeken/include/DB_Functions.php on line 62
please do not downgrade i Know this has been asked so many times but i am really really confused here.
You do not use prepared Statement properbly in updateUser.
You have no placeholder in the query string:
$stmt = $this->conn->prepare("update users set name ='$name', email ='$email', encrypted_password = '$encrypted_password' , updated_at = NOW() where email ='$email' ");
Change to:
$stmt = $this->conn->prepare("update users set name =?, email =?, encrypted_password = ?, updated_at = NOW() where email =? ");
$stmt->bind_param("ssssss", $name, $email, $encrypted_password , $updated_at, $email);
PHP Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables
Look at this statement here in storeUser() method,
$stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt , $totalpoints, $digipoints, $total_coupons, $created_at,$updated_at );
It should be,
$stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt);
i'm still build an API for android app. I follow this tutorial: http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/.
When I try to submit: It will be SERVER 500 ERROR.
Check the demo:
FORM: http://simedik.masterbiz.net/api/form.php
<form action="login.php" method="post">
<input type="text" name="email"/>
<input type="text" name="password"/>
<input type="submit" value="SUBMIT"/>
</form>
Anyone have solution for this? Thanks in advance!
LOGIN.PHP SCRIPT
<?php
require_once 'DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['email']) && isset($_POST['password'])) {
// receiving the post params
$email = $_POST['email'];
$password = $_POST['password'];
// get the user by email and password
$user = $db->getUserByEmailAndPassword($email, $password);
if ($user != false) {
// use is found
$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 {
// user is not found with the credentials
$response["error"] = TRUE;
$response["error_msg"] = "Login credentials are wrong. Please try again!";
echo json_encode($response);
}
} else {
// required post params is missing
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters email or password is missing!";
echo json_encode($response);
}
?>
DB_FUNCTIONS.PHP
<?php
class DB_Functions {
private $conn;
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$db = new Db_Connect();
$this->conn = $db->connect();
}
// 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
$stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
$stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return false;
}
}
/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($email, $password) {
$stmt = $this->conn->prepare("SELECT * FROM pasien WHERE kd_pasien = ?");
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return NULL;
}
}
/**
* Check user is existed or not
*/
public function isUserExisted($email) {
$stmt = $this->conn->prepare("SELECT email from users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
// user existed
$stmt->close();
return true;
} else {
// user not existed
$stmt->close();
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;
}
}
?>