私のコードで時間の豚を持っています。私はこれに新しいですし、非常に苦労しています。ログインフォームはPHP/MySQLを発行します。
私はいくつかの問題があります。まず、SQLデータベースに接続されたログインフォームを使用しようとしていますが、間違ったデータやデータが入力されていないときにエラーは発生しません。
第2に、ユーザーがログインしたときに各ページにユーザー名を表示しようとしていますが、これはデータベースに手動で入力されたユーザーに対してのみ有効です。 phpmyadminに表示されていても、登録フォームから追加されたユーザーは表示されません。最初の問題のため
マイログインページコードは次のとおりです。何かアドバイスのため
<?php
echo '<h3>Sign in</h3>';
if($_SERVER['REQUEST_METHOD'] != 'POST')
{
/*the form hasn't been posted yet, display it
note that the action="" will cause the form to post to the same page it is on */
echo '<form method="post" action="">
Username: <input type="text" name="Username" />
Password: <input type="password" name="Password" />
<input type="submit" value="Sign in" />
</form>';
}
else
{
//the form has been posted without errors, so save it
//notice the use of mysql_real_escape_string, keep everything safe!
$username = mysql_real_escape_string($_POST['Username']);
$password = mysql_real_escape_string($_POST['Password']);
$sql = "SELECT * FROM Users WHERE Username = '$username' AND Password = '$password'";
$result = mysql_query($sql);
if(!$result)
{
//something went wrong, display the error
echo 'Something went wrong while signing in. Please try again later.';
header("location:index.php");
//echo mysql_error(); //debugging purposes, uncomment when needed
}
else
{
{
{
//set the $_SESSION['signed_in'] variable to TRUE
$_SESSION['signed_in'] = true;
//we also put the user_id and user_name values in the $_SESSION, so we can use it at various pages
while($row = mysql_fetch_assoc($result))
{
$_SESSION['UserID'] = $row['UserID'];
$_SESSION['Username'] = $row['Username'];
}
echo 'Welcome, ' . $_SESSION['Username'] . ' <a href="index2.php">Proceed to the forum Home page</a>.';
}
}
}
}
?>
感謝。
まず、mysql_の代わりに 'PDO'を使用してください –
注:' mysql_ * '関数は廃止され、PHP 7から削除されました。そのバージョンにアップグレードするとコードは機能しなくなります。代わりに['mysqli_ *'やPDO](http://php.net/manual/en/mysqlinfo.api.choosing.php)を使って新しいコードを書くべきです。 –
また、SQLインジェクションのために開いていることに注意してください。 –