在Android应用程序中使用不同的帐户创建帐户

时间:2017-09-11 03:27:18

标签: php android localhost

我的应用程序现在有2个登录,用户可以使用应用程序帐户或Facebook登录,现在我想问一下,当我用facebook登录时,facebook帐户的用户名,电子邮件和密码可以与我的应用程序存储在同一个表中帐户注册?我使用localhost和php作为数据库,我使用android studio开发应用程序,因为现在我的任务是如果用户在我的应用程序中使用facebook登录,当他想创建新帐户时,他不能使用与用户登录的facebook相同的电子邮件地址现在。希望有人理解我的问题。以下是我的登录和注册申请帐户代码:

登录:

<?php 

require_once '../includes/DbOperations.php';

$response = array(); 

if($_SERVER['REQUEST_METHOD']=='POST'){
if(isset($_POST['username']) and isset($_POST['password'])){
    $db = new DbOperations(); 

    if($db->userLogin($_POST['username'], $_POST['password'])){
        $user = $db->getUserByUsername($_POST['username']);
        $response['error'] = false; 
        $response['user_id'] = $user['user_id'];
        $response['email'] = $user['email'];
        $response['username'] = $user['username'];


    }else{
        $response['error'] = true; 
        $response['message'] = "Invalid username or password";          
    }

}else{
    $response['error'] = true; 
    $response['message'] = "Required fields are missing";
}
}

echo json_encode($response);
?>

寄存器:

<?php 

require_once '../includes/DbOperations.php';

$response = array(); 

if($_SERVER['REQUEST_METHOD']=='POST'){
if(
    isset($_POST['username']) and
        isset($_POST['email']) and
            isset($_POST['password']))
    {
    //operate the data further 

    $db = new DbOperations(); 

    $result = $db->createUser(   $_POST['username'],
                                $_POST['password'],
                                $_POST['email']
                            );
    if($result == 1){
        $response['error'] = false; 
        $response['message'] = "User registered successfully";
    }elseif($result == 2){
        $response['error'] = true; 
        $response['message'] = "Some error occurred please try again";          
    }elseif($result == 0){
        $response['error'] = true; 
        $response['message'] = "It seems you are already registered, please choose a different email and username";                     
    }
}else{
    $response['error'] = true; 
    $response['message'] = "Required fields are missing";
}
}else{
$response['error'] = true; 
$response['message'] = "Invalid Request";
}

echo json_encode($response);

1 个答案:

答案 0 :(得分:0)

在这种情况下我做的是在数据库中存储电子邮件和用户数据。除此之外,我还存储用户名(以避免重复用户名)。因此,如果用户创建新帐户,则会检查此单独的表/集合是否存在此类电子邮件ID。如果存在,只需显示重置密码的消息。