1

私は次の表を持っています

ログイン

IdUser (整数)

ユーザー名(varchar)

パスワード(varchar)

メール(varchar)

アクティブ(整数)

Active は、ユーザーの電子メールが検証されているかどうかに応じて、0 または 1 になります。アカウントが確認されると、テーブルのアクティブな行は 1 で更新されます。アカウントが確認されない場合、テーブルのアクティブな行は 0 のままです。

ユーザーは、アカウントが確認された場合にのみログインできるようにする必要があります。

これまでのところ、私のログインは次のように機能します。

//login API
function login($user, $pass) {

// try to match a row in the "login" table for the given username and password
$result = query("SELECT IdUser, username FROM login WHERE username='%s' AND pass='%s' limit 1", $user, $pass);

if (count($result['result'])>0) {
    // a row was found in the database for username/pass combination
    // save a simple flag in the user session, so the server remembers that the user is authorized
    $_SESSION['IdUser'] = $result['result'][0]['IdUser'];
    // print out the JSON of the user data to the iPhone app; it looks like this:
    // {IdUser:1, username: "Name"}
    print json_encode($result);
} else {
    // no matching username/password was found in the login table
    errorJson('Authorization failed');
}
}

認証済みのユーザーのみにログインを許可するにはどうすればよいですか?

4

4 に答える 4

0

クエリのAND active = 1前に追加するだけです。limit 1

余談ですが、コードにはより広範な問題がいくつかあります。

  • パスワードをデータベースに直接保存することは避け、代わりに bcrypt を使用します。たとえば、
  • SQL インジェクションを回避するために、プリペアド ステートメントで mysqli または別のデータベース インターフェイスを使用する
于 2013-10-24T03:43:14.980 に答える
0

ここ:

$result = query("SELECT IdUser, username FROM login WHERE username='%s' AND pass='%s' AND emailVerified='1' limit 1", $user, $pass);

ここで、emailVerified はメール検証済みステータス フィールド名です。これを独自の名前に置き換えます。

于 2013-10-24T03:43:33.810 に答える
0

ええと、あなたの説明に何かが欠けていない限り、単にAND active=1あなたのWHERE節に a を追加する必要があるようです. したがって、次のようになります。

SELECT IdUser, username FROM login WHERE username='%s' AND pass='%s' AND active=1  limit 1

更新しました

//login API
function login($user, $pass) {

    // try to match a row in the "login" table for the given username and password
    $result = query("SELECT IdUser, username, active, email FROM login WHERE username='%s' AND pass='%s' limit 1", $user, $pass);

    if (count($result['result'])>0) {
        // a row was found in the database for username/pass combination
        if (!$result['result'][0]['active']) {
            // not activated yet
            errorJson('Not activated yet: ' + $result['result'][0]['email']);

        } else {
            // save a simple flag in the user session, so the server remembers that the user is authorized
            $_SESSION['IdUser'] = $result['result'][0]['IdUser'];
            // print out the JSON of the user data to the iPhone app; it looks like this:
            // {IdUser:1, username: "Name"}
            print json_encode($result);
        }
    } else {
        // no matching username/password was found in the login table
        errorJson('Authorization failed');
    }
}

ちなみに、他の人が言及したように、あなたのコードはSQLインジェクションに敏感であるように見え、パスワードをデータベースに生のテキストで保存しているように見えますが、これは非常に悪い習慣です. クエリに mysqli + プレースホルダーを使用することを検討する必要があります。また、プロセスのどの時点でも、パスワードをハッシュする必要があります。簡単な方法 (最善ではありませんが) は、MySQL のパスワード機能を使用することです。したがって、クエリは単純に次のように変更されます。

$result = query("SELECT IdUser, username, active, email FROM login WHERE username=? AND password=PASSWORD(?) limit 1", $user, $pass);
于 2013-10-24T03:40:47.333 に答える
0

まず、ユーザーがアクティブかどうかを確認する必要があります

select active from login where username='%s';//execute this query and check result and store in active..

if(!$active)
{
errorJson("your account is not activated yet!);
}
else
{
//login code
}
于 2013-10-24T03:46:10.047 に答える