Search code examples
phpandroidauthenticationclassnotfound

Class 'update_user_info' not found in \android_login_example\login.php


Says Class 'update_user_info' not found in \android_login_example\login.php Here is Login.php...

IDK whats happening but i also tried removing public in front of functions in update_user_info.php file! It didnt worked! Im creating a login page and registration page for an android app!

Thanks Tracy

<?php
require_once 'update_user_info.php';
$db = new update_user_info();

// 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->VerifyUserAuthentication($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"]["age"] = $user["age"];
        $response["user"]["gender"] = $user["gender"];
        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);
}
?>

Now This is update_user_info.php..

<?php
     function StoreUserInfo($name, $email, $password, $gender, $age) {
        $hash = $this->hashFunction($password);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"]; // salt

        $stmt = $this->conn->prepare("INSERT INTO android_php_post(name, email,
encrypted_password, salt, gender, age) VALUES(?, ?, ?, ?, ?, ?)");
        $stmt->bind_param("ssssss", $name, $email, $encrypted_password, $salt, $gender, $age);
        $result = $stmt->execute();
        $stmt->close();

        // check for successful store
        if ($result) {
            $stmt = $this->conn->prepare("SELECT name, email, encrypted_password, salt, gender, age FROM android_php_post WHERE email = ?");
            $stmt->bind_param("s", $email);
            $stmt->execute();
            $stmt-> bind_result($token2,$token3,$token4,$token5,$token6,$token7);

            while ( $stmt-> fetch() ) {
               $user["name"] = $token2;
               $user["email"] = $token3;
               $user["gender"] = $token6;
               $user["age"] = $token7;
            }
            $stmt->close();
            return $user;
        } else {
          return false;
        }
    }

     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;
    }

 function VerifyUserAuthentication($email, $password) {

        $stmt = $this->conn->prepare("SELECT name, email, encrypted_password, salt, gender, age FROM android_php_post WHERE email = ?");

        $stmt->bind_param("s", $email);

        if ($stmt->execute()) {
            $stmt-> bind_result($token2,$token3,$token4,$token5,$token6,$token7);

            while ( $stmt-> fetch() ) {
               $user["name"] = $token2;
               $user["email"] = $token3;
               $user["encrypted_password"] = $token4;
               $user["salt"] = $token5;
               $user["gender"] = $token6;
               $user["age"] = $token7;
            }

            $stmt->close();

            // verifying user password
            $salt = $token5;
            $encrypted_password = $token4;
            $hash = $this->CheckHashFunction($salt, $password);
            // check for password equality
            if ($encrypted_password == $hash) {
                // user authentication details are correct
                return $user;
            }
        } else {
            return NULL;
        }
    }

 function checkHashFunction($salt, $password) {
        $hash = base64_encode(sha1($password . $salt, true) . $salt);
        return $hash;
    }

 function CheckExistingUser($email) {
        $stmt = $this->conn->prepare("SELECT email from android_php_post 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;
        }
    }

    ?>

Solution

  • You need to encapsulate your update_user_info.php in class if you are going to use it as a class

    <?php
    class update_user_info {
        function StoreUserInfo($name, $email, $password, $gender, $age) {
            $hash = $this->hashFunction($password);
            $encrypted_password = $hash["encrypted"]; // encrypted password
            $salt = $hash["salt"]; // salt
    
            $stmt = $this->conn->prepare("INSERT INTO android_php_post(name, email,
                                            encrypted_password, salt, gender, age) VALUES(?, ?, ?, ?, ?, ?)");
            $stmt->bind_param("ssssss", $name, $email, $encrypted_password, $salt, $gender, $age);
            $result = $stmt->execute();
            $stmt->close();
    
            // check for successful store
            if ($result) {
                $stmt = $this->conn->prepare("SELECT name, email, encrypted_password, salt, gender, age FROM android_php_post WHERE email = ?");
                $stmt->bind_param("s", $email);
                $stmt->execute();
                $stmt-> bind_result($token2,$token3,$token4,$token5,$token6,$token7);
    
                while ( $stmt-> fetch() ) {
                    $user["name"] = $token2;
                    $user["email"] = $token3;
                    $user["gender"] = $token6;
                    $user["age"] = $token7;
                }
                $stmt->close();
                return $user;
            } else {
                return false;
            }
        }
    
        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;
        }
    
        function VerifyUserAuthentication($email, $password) {
    
            $stmt = $this->conn->prepare("SELECT name, email, encrypted_password, salt, gender, age FROM android_php_post WHERE email = ?");
    
            $stmt->bind_param("s", $email);
    
            if ($stmt->execute()) {
                $stmt-> bind_result($token2,$token3,$token4,$token5,$token6,$token7);
    
                while ( $stmt-> fetch() ) {
                    $user["name"] = $token2;
                    $user["email"] = $token3;
                    $user["encrypted_password"] = $token4;
                    $user["salt"] = $token5;
                    $user["gender"] = $token6;
                    $user["age"] = $token7;
                }
    
                $stmt->close();
    
                // verifying user password
                $salt = $token5;
                $encrypted_password = $token4;
                $hash = $this->CheckHashFunction($salt, $password);
                // check for password equality
                if ($encrypted_password == $hash) {
                    // user authentication details are correct
                    return $user;
                }
            } else {
                return NULL;
            }
        }
    
        function checkHashFunction($salt, $password) {
            $hash = base64_encode(sha1($password . $salt, true) . $salt);
            return $hash;
        }
    
        function CheckExistingUser($email) {
            $stmt = $this->conn->prepare("SELECT email from android_php_post 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;
            }
        }
    }
    

    Or convert class method calls to plain functions:

    $user = $db->VerifyUserAuthentication($email, $password);
    

    to

    $user = VerifyUserAuthentication($email, $password);