Question

I am creating an Android application that has a login/registration page. To be able to use...

I am creating an Android application that has a login/registration page. To be able to use the application, the user must be 21 + years old. I have previously created php files for login, register, and update user info. I am having trouble on where to add code that will check if the user is 21+ into the update_user_info.php file I have created.

I have figured out the correct mysql command to add to the php file, I am just not sure how or where to add the command into the php file. I need to use the timestampdiff function to find the difference between the dob entered and the curdate, then to check if the user entry is over 21 years old.

Here is the code for my register.php

<?php

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

// json response array
$response = array("error" => FALSE);

if (isset($_POST['name']) && isset($_POST['email']) && 
isset($_POST['password']) && isset($_POST['gender']) && 
isset($_POST['dob'])) {

    // receiving the post params
    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $gender = $_POST['gender'];
    $dob = $_POST['dob'];

    // check if user is already existed with the same email
    if ($db->CheckExistingUser($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->StoreUserInfo($name, $email, $password, $gender, 
$dob);
        if ($user) {
            // user stored successfully
            $response["error"] = FALSE;
            $response["user"]["name"] = $user["name"];
            $response["user"]["email"] = $user["email"];
            $response["user"]["gender"] = $user["gender"];
            $response["user"]["dob"] = $user["dob"];
            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, password, 
gender or date of birth) is missing!";
    echo json_encode($response);
}
?>

Here is the code for update_user_info.php

<?php

class update_user_info {

    private $conn;

    // constructor
    function __construct() {
        require_once 'android_login_connect.php';
        // connecting to database
        $db = new android_login_connect();
        $this->conn = $db->connect();
    }

    // destructor
    function __destruct() {

    }

    /**
     * Storing new user
     * returns user details
     */
    public function StoreUserInfo($name, $email, $password, $gender, $dob) 
{
        $hash = $this->hashFunction($password);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"]; // salt

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

        // check for successful store
        if ($result) {
            $stmt = $this->conn->prepare("SELECT name, email, 
encrypted_password, salt, gender, dob FROM beerpressure_users 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["dob"] = $token7;
            }
            $stmt->close();
            return $user;
        } else {
          return false;
        }
    }

    /**
     * Get user by email and password
     */
    public function VerifyUserAuthentication($email, $password) {

        $stmt = $this->conn->prepare("SELECT name, email, 
encrypted_password, salt, gender, dob FROM beerpressure_users 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["dob"] = $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;
        }
    }

    /**
     * Check user is existed or not
     */
    public function CheckExistingUser($email) {
        $stmt = $this->conn->prepare("SELECT email from beerpressure_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 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;
    }

    /**
     * Decrypting password
     * @param salt, password
     * returns hash string
     */
    public function checkHashFunction($salt, $password) {
        $hash = base64_encode(sha1($password . $salt, true) . $salt);
        return $hash;
    }

}

?>
0 0
Add a comment Improve this question Transcribed image text
Answer #1

update code:

<?php

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

// json response array
$response = array("error" => FALSE);

if (isset($_POST['name']) && isset($_POST['email']) &&
isset($_POST['password']) && isset($_POST['gender']) &&
isset($_POST['dob'])) {

// receiving the post params
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$gender = $_POST['gender'];
$dob = $_POST['dob'];

// check if user is already existed with the same email
if ($db->CheckExistingUser($email)) {
// user already existed
$response["error"] = TRUE;
$response["error_msg"] = "User already existed with " . $email;
echo json_encode($response);
}else if ($db->checkDateofBirth($dob)) { //check dob is greater than 21 or not
// user already existed
$response["error"] = TRUE;
$response["error_msg"] = $dob. " :Date of birth is not greater than 21";
echo json_encode($response);
}else {
// create a new user
$user = $db->StoreUserInfo($name, $email, $password, $gender,
$dob);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["gender"] = $user["gender"];
$response["user"]["dob"] = $user["dob"];
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, password,
gender or date of birth) is missing!";
echo json_encode($response);
}
?>
Here is the code for update_user_info.php

<?php

class update_user_info {

private $conn;

// constructor
function __construct() {
require_once 'android_login_connect.php';
// connecting to database
$db = new android_login_connect();
$this->conn = $db->connect();
}

// destructor
function __destruct() {

}

/**
* Storing new user
* returns user details
*/
public function StoreUserInfo($name, $email, $password, $gender, $dob)
{
$hash = $this->hashFunction($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt

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

// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT name, email,
encrypted_password, salt, gender, dob FROM beerpressure_users 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["dob"] = $token7;
}
$stmt->close();
return $user;
} else {
return false;
}
}

/**
* Get user by email and password
*/
public function VerifyUserAuthentication($email, $password) {

$stmt = $this->conn->prepare("SELECT name, email,
encrypted_password, salt, gender, dob FROM beerpressure_users 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["dob"] = $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;
}
}

/**
* Check user is existed or not
*/
public function CheckExistingUser($email) {
$stmt = $this->conn->prepare("SELECT email from beerpressure_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;
}
}
   /* *
   *Check date of birth
   */
   public function checkDateofBirth($userdob) {

       //Create a DateTime object using the user's date of birth.
       $dob = new DateTime($userDob);

       //We need to compare the user's date of birth with today's date.
       $now = new DateTime();

       //Calculate the time difference between the two dates.
       $difference = $now->diff($dob);

       //Get the difference in years, as we are looking for the user's age.
       $age = $difference->y;
       if($age>21){
           return false;
       }
       return true;
}

/**
* Encrypting password
* @param password
* returns salt and encrypted password
*/
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;
}

/**
* Decrypting password
* @param salt, password
* returns hash string
*/
public function checkHashFunction($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}

}

?>

write age check before updating or insert user details into sql table.

So after check the user is existing or not then check for the dob is valid or not.

Add a comment
Know the answer?
Add Answer to:
I am creating an Android application that has a login/registration page. To be able to use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help showing the first column of the table to show up exactly with the...

    I need help showing the first column of the table to show up exactly with the mysql table into the php page. Is there a way to fix it ? So I created a php form where the user enters the information. sand.truman.edu/~jyl6557/assignment5/hw5-dataentry.php . it asks for name, hometown, gender(only male and female) and status (freshman, sophmore, junior, senior). once it checks and passes through those tests, it would say added successfully and then show the links to add another...

  • I need help please to add function for clean up any inputs form that we receive from the users for this code below : &lt...

    I need help please to add function for clean up any inputs form that we receive from the users for this code below : <?php session_start(); // initializing variables $fname = ""; $lname = ""; $address = ""; $city = ""; $state = ""; $zip = ""; $email = ""; $phone = ""; $errors = array(); // connect to the database $db = mysqli_connect("localhost","root","password","db"); // REGISTER USER if (isset($_POST['reg_user1'])) { // receive all input values from the form $fname =...

  • How can I print the Database table in PHP please ? here I have form for name and email, also I have php code that allow...

    How can I print the Database table in PHP please ? here I have form for name and email, also I have php code that allow user to add his name and email to my database, all I need to print my final database when the user click on submit. <form action="" method="post"> <label>Name :</label> <input type="text" name="name" required="required" placeholder="Please Enter Name"/><br /><br /> <label>Email :</label> <input type="email" name="email" required="required" /><br/><br /> <input type="submit" value=" Submit " name="submit"/><br /> </form>...

  • LANGUAGE JAVASCRIPT, PHP Need help with PHP and ajax code. The user needs to login but,...

    LANGUAGE JAVASCRIPT, PHP Need help with PHP and ajax code. The user needs to login but, I keep getting an error that I coded "Wrong username and password!" ***PLEASE DONT GIVE ME A NEW CODE THAT IS NOWHERE NEAR THE CODE I SUBMITTED***** PLEASE LOOK AT THE CODE AND SEE ANY ISSUES login.html <!DOCTYPE html> <html> <head> <title>login popup</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <style type="text/css"> body { background-color: white; } </style> </head> <body> <center> <h1 style="text-align: center;"> Login </h1> <form>             Login ID:...

  • PHP - Use of classes This is a further development of the previous task, the participant...

    PHP - Use of classes This is a further development of the previous task, the participant registration. You must use the following Participant class. <?php class Deltaker { private $etterNavn; private $forNavn; private $fAar; function __construct(string $fornavn, string $etternavn, string $aar) { $this->forNavn = $fornavn; $this->etterNavn = $etternavn; $this->fAar = $aar; } function hentEtterNavn() : string { return $this->etterNavn; } function hentForNavn() : string { return $this->forNavn; } function hentFAar() : string{ return $this->fAar; } //Setters function settForNavn(string $fornavn) {...

  • For this code below, I need to add the form information to mysql when I click...

    For this code below, I need to add the form information to mysql when I click on submit, at the same time when I click on submit I need to move to another page like Welcome.php WITH NOTE THAT THE ENTERED INFORMATION NOW ALREADY IN DATABASE how can I male that with my code below? THANKS ................................................................................................................................................   <form method="POST"> <div class="container">    <label for="fname"><b>First Name</b></label> <input type="text" placeholder="Enter First Name" name="fname" required> <label for="lname"><b>Last Name</b></label> <input type="text" placeholder="Enter Last Name"...

  • I am having an issue. When i press submit to test the code, it goes to...

    I am having an issue. When i press submit to test the code, it goes to a blank screen. Please, will some one assist me? Thanks! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Sign Guest Book</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> </head> <body> <?php if (empty($_POST['?rst_name']) || empty($_POST['last_name']))     echo "<p>You must enter your ?rst and last name! Click your browser's Back button to return to the Guest Book form.</p>"; else {     $DBConnect = @mysql_connect("localhost", "root", "");    ...

  • PHP Programming with MySQL - Why do I get the following error and how do I...

    PHP Programming with MySQL - Why do I get the following error and how do I connect to my database. Looks like I might need to use mysqli instead of mysql but I dont know how to do that or what to change. I appreciate it is you fixed anything that needs to be re-written. Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /Applications/XAMPP/xamppfiles/htdocs/Week9/VerifyLogin.php:12 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/Week9/VerifyLogin.php on line 12 Line 12: $DBConnect =...

  • PHP Programming In this project, you will create a Web page that allows visitors to your...

    PHP Programming In this project, you will create a Web page that allows visitors to your site to sign a guest book that is saved to a database. Create a new document in your text editor and type the <!DOCTYPE> declaration, <html> element, document head, and <body> element. Use the strict DTD and “Guest Book” as the content of the <title> element. Add the following text and elements to the document body: <h2>Enter your name to sign our guest book</h2>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT