I have some code from my android studio project, i am using the latest xampp with mysql and apache, i have php code to speak to query the database.
problem:
i am trying verify the username and password, yet the code is echoing back wrong username or password, so far i worked out the username and password is definitely getting passed, the php code is connected to the database and has access to the table as i have put in table names that arent there and it tells me so.
the table is called login,
it has one entry
username = a
password = a
can anyone point me to where the problem might be laying.
android code
protected void login(){
getPassword = findViewById(R.id.passwordInput);
getUsername = findViewById(R.id.usernameInput);
String username = String.valueOf(getUsername.getText()).trim();
String password = String.valueOf(getPassword.getText()).trim();
//Start ProgressBar first (Set visibility VISIBLE)
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
//Starting Write and Read data with URL
//Creating array for parameters
String[] field = new String[2];
field[0] = "username";
field[1] = "password";
//Creating array for data
String[] data = new String[2];
data[0] = username;
data[1] = password;
System.out.println(Arrays.asList(data));
PutData putData = new PutData("http://**MY_IP**/storemanager/login.php", "POST", field, data);
System.out.println(username + "" + password);
System.out.println(putData.getResult());
if (putData.startPut()) {
System.out.println("starting");
if (putData.onComplete()) {
System.out.println("complete");
String result = putData.getResult();
//End ProgressBar (Set visibility to GONE)
Log.i("PutData", result);
System.out.println(Arrays.asList(data));
System.out.println(Arrays.asList(field));
if(result.matches("Login Success")){
System.out.println("yupp");
}
}
}
//End Write and Read data with URL
}
});
login.php
<?php
require "DataBase.php";
$db = new DataBase();
if (isset($_POST['username']) && isset($_POST['password'])) {
if ($db->dbConnect()) {
if ($db->logIn("login", $_POST['username'], $_POST['password'])) {
echo "Login Success";
} else echo "Username or Password wrong";
} else echo "Error: Database connection";
} else echo "All fields are required";
?>
database.php
<?php
require "DataBaseConfig.php";
class DataBase
{
public $connect;
public $data;
private $sql;
protected $servername;
protected $username;
protected $password;
protected $databasename;
public function __construct()
{
$this->connect = null;
$this->data = null;
$this->sql = null;
$dbc = new DataBaseConfig();
$this->servername = $dbc->servername;
$this->username = $dbc->username;
$this->password = $dbc->password;
$this->databasename = $dbc->databasename;
}
function dbConnect()
{
$this->connect = mysqli_connect($this->servername, $this->username, $this->password, $this->databasename);
return $this->connect;
}
function prepareData($data)
{
return mysqli_real_escape_string($this->connect, stripslashes(htmlspecialchars($data)));
}
function logIn($table, $username, $password)
{
$username = $this->prepareData($username);
$password = $this->prepareData($password);
$this->sql = "select * from " . $table . " where username = '" . $username . "'";
$result = mysqli_query($this->connect, $this->sql);
$row = mysqli_fetch_assoc($result);
if (mysqli_num_rows($result) != 0) {
$dbusername = $row['username'];
$dbpassword = $row['password'];
if ($dbusername == $username && password_verify($password, $dbpassword)) {
$login = true;
} else $login = false;
} else $login = false;
return $login;
}
function signUp($table, $fullname, $email, $username, $password)
{
$fullname = $this->prepareData($fullname);
$username = $this->prepareData($username);
$password = $this->prepareData($password);
$email = $this->prepareData($email);
$password = password_hash($password, PASSWORD_DEFAULT);
$this->sql =
"INSERT INTO " . $table . " (fullname, username, password, email) VALUES ('" . $fullname . "','" . $username . "','" . $password . "','" . $email . "')";
if (mysqli_query($this->connect, $this->sql)) {
return true;
} else return false;
}
}
?>
databaseconfig.php
<?php
class DataBaseConfig
{
public $servername;
public $username;
public $password;
public $databasename;
public function __construct()
{
$this->servername = 'localhost';
$this->username = 'root';
$this->password = '';
$this->databasename = 'storemanager';
}
}
?>