skip to Main Content

When signing into to the app with username, the email is not used hence the following error is throw and vice versa “Notice: Undefined index: email in /Applications/XAMPP/xamppfiles/htdocs/menthor/login.php on line 16”

I’ve tried putting the lines that were generating the error in conditions but those efforts proved futile
Login.php file(parts relevant to the error)

// STEP 1. Receive data / info paassed to current file
if ((empty($_REQUEST['username']) || empty($_REQUEST['password'])) && (empty($_REQUEST['email']) || empty($_REQUEST['password']))) {
    $return['status'] = '400';
    $return['message'] = 'Missing requird information';
    echo json_encode($return);
    return;

} 


// securing received info / data from hackers or injections
$email = htmlentities($_REQUEST['email']);
$username = htmlentities($_REQUEST['username']);
$password = htmlentities($_REQUEST['password']);

// STEP 2. Establish connection with the server
require('secure/access.php');
$access = new access('localhost' , 'root', '' , 'menthor');
$access->connect();

// STEP 3. Check existence of the user. Try to fetch the user with the same email address
// STEP 3. Check availability of the login/user information
$username_aval = $access->selectUser_Username($username);
$email_aval = $access->selectUser_Email($email);

//$return = array();

// user is found
if ($username_aval) {

    // Get encrypted password and salt from the server for validation
    $encryptedPassword = $username_aval['password'];
    $salt = $username_aval['salt'];

    if ($encryptedPassword == sha1($password . $salt)) {

        $return['status'] = '200';
        $return['message'] = 'Successfully Logged In';
        $return['id'] = $username_aval['id'];
        $return['username'] = $username_aval['username'];
        $return['email'] = $username_aval['email'];
        $return['fullName'] = $username_aval['fullName'];
        $return['lastName'] = $username_aval['lastName'];
        $return['birthday'] = $username_aval['birthday'];
        $return['gender'] = $username_aval['gender'];
        $return['cover'] = $username_aval['cover'];
        $return['ava'] = $username_aval['ava'];
        $return['bio'] = $username_aval['bio'];
        $return['allow_follow'] = $username_aval['allow_follow'];

    } else {

        // In event that encrypted password and salt does not match
        $return['status'] = '201';
        $return['message'] = 'Password do not match';

    }

} else if ($email_aval) {

    // Get encrypted password and salt from the server for validation
    $encryptedPassword = $email_aval['password'];
    $salt = $email_aval['salt'];

    if ($encryptedPassword == sha1($password . $salt)) {

        $return['status'] = '200';
        $return['message'] = 'Successfully Logged In';
        $return['id'] = $email_aval['id'];
        $return['username'] = $email_aval['username'];
        $return['email'] = $email_aval['email'];
        $return['fullName'] = $email_aval['fullName'];
        $return['lastName'] = $email_aval['lastName'];
        $return['birthday'] = $email_aval['birthday'];
        $return['gender'] = $email_aval['gender'];
        $return['cover'] = $email_aval['cover'];
        $return['ava'] = $email_aval['ava'];
        $return['bio'] = $email_aval['bio'];
        $return['allow_follow'] = $email_aval['allow_follow'];

    } else {

        // In event that encrypted password and salt does not match
        $return['status'] = '202';
        $return['message'] = 'Password do not match';

    } 
}else {

        // In event that user is not found
        $return['status'] = '403';
        $return['message'] = 'User was not found';

}

// stop connection with server
$access->disconnect();

// pass info as JSON
echo json_encode($return);

Access.php file(parts relevant to error)
Will try to select any value in the database based on received Email

public function  selectUser_Email($email) {

    // array to store full user related information with the logic: key=>Value
    $returnArray = array();

    // SQL Language / Commande to be sent to the server
    // SELECT * FROM users WHERE email='[email protected]'
    $sql = "SELECT * FROM users WHERE email='" . $email . "'";

    // executing query via already established connection with the server
    $result = $this->conn->query($sql);

    // result isn't zero and it has least 1 row / value / result
    if ($result != null && (mysqli_num_rows($result)) >= 1) {

        // converting to JSON
        $row = $result->fetch_array(MYSQLI_ASSOC);

        // assign fetched row to ReturnArray
        if (!empty($row)) {
            $returnArray = $row;
        }
    }

    // throw back returnArray
    return $returnArray;
}

// Will try to select any value in the database based on received Email
public function  selectUser_Username($username) {

    // array to store full user related information with the logic: key=>Value
    $returnArray = array();

    // SQL Language / Commande to be sent to the server
    // SELECT * FROM users WHERE username='rondell'
    $sql = "SELECT * FROM users WHERE username='" . $username . "'";

    // executing query via already established connection with the server
    $result = $this->conn->query($sql);

    // result isn't zero and it has least 1 row / value / result
    if ($result != null && (mysqli_num_rows($result)) >= 1) {

        // converting to JSON
        $row = $result->fetch_array(MYSQLI_ASSOC);

        // assign fetched row to ReturnArray
        if (!empty($row)) {
            $returnArray = $row;
        }
    }

    // throw back returnArray
    return $returnArray;
}

Current results when logging in via web server

Notice: Undefined index: email in /Applications/XAMPP/xamppfiles/htdocs/menthor/login.php on line 16
{"status":"200","message":"Successfully Logged In","id":"44","username":"rondell","email":"[email protected]","fullName":"rondell","lastName":"","birthday":"","gender":"","cover":"","ava":"","bio":"","allow_follow":"1"}

Expected Results

{"status":"200","message":"Successfully Logged In","id":"44","username":"rondell","email":"[email protected]","fullName":"rondell","lastName":"","birthday":"","gender":"","cover":"","ava":"","bio":"","allow_follow":"1"}

2

Answers


  1. Chosen as BEST ANSWER

    Turns out the solution was pretty simple came to be after a bit of hard think... I just needed to simply create two login.php files... One dedicated to the users signing in with username and password and the other for users signing in with email and password...Cheers


  2. use email as object or you can dump the request and see what is happening

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search