このログイン スクリプトで何か問題が発生することはありますか。
public function login($username, $pass, $remember) {
// check username and password with db
// else throw exception
$connect = new connect();
$conn = $connect->login_connect();
// check username and password
$result = $conn->query("select * from login where
username='".$username."' and
password=sha1('".$pass."')");
if (!$result) {
throw new depException('Incorrect username and password combination. Please try again.');
} else {
echo $username, $pass;
}
説明する:
現時点では、スクリプトはすべてを許可しています。つまり、クエリは、渡されたユーザー名とパスワードに対して true を返します。チェックとして echo ステートメントを入れました。明らかに、スクリプトは通常の状況で続行されます。
正常に動作している登録スクリプトで使用しているため、接続クラスとlogin_connectメソッドが動作していることはわかっています。depException は Exception クラスの単なる拡張です。
関数 login() は、正常に動作している register() を含む同じクラスの一部です。
echo ステートメントがそれらを正確に出力しているため、2 つの変数 ($username と $pass) が関数に到達していることがわかります。(スクリプトのこの部分では、$remember 変数は必要ありません。後で、remember me プロセスに使用されます)。
私は困惑しています。助けてください!
アップデート
それらの応答に感謝します。クエリが何を返すのか混乱していました。完全なスクリプトは、返された行数をチェックしますが、ここでチェックを行う必要がありました。私の覚えている機能を除いて、すべてが機能しています。おそらく誰かがそれを助けることができますか?!?! 完全なスクリプトは次のとおりです。
public function login($username, $pass, $remember) {
// check username and password with db
// else throw exception
$connect = new connect();
$conn = $connect->login_connect();
// check username and password
$result = $conn->query("select * from login where
username='".$username."' and
password=sha1('".$pass."')");
if (!$result) {
throw new depException('Incorrect username and password combination. Please try again.');
}
if ($result->num_rows>0) {
$row = $result->fetch_assoc();
//assign id to session
$_SESSION['user_id'] = $row[user_id];
// assign username as a session variable
$_SESSION['username'] = $username;
// start rememberMe
$cookie_name = 'db_auth';
$cookie_time = (3600 * 24 * 30);*/ // 30 days
// check to see if user checked box
if ($remember) {
setcookie ($cookie_name, 'username='.$username, time()+$cookie_time);
}
// If all goes well redirect user to their homepage.
header('Location: http://localhost/v6/home/index.php');
} else {
throw new depException('Could not log you in.);
}
}
どうもありがとうございました。
アップデート2!
あなたの助けのおかげで、このスクリプトの主要部分が機能しています。ただし、最後に覚えているビットはまだ機能したくありません。誰かがそれを整理するために手を貸してくれませんか? $username、$pass、および $remember はすべて、$_POST['username'] などの書き込みを毎回保存するために関数に渡す前に割り当てた短い変数名です。$remember はチェックボックスを参照します。