main_login.phpHTML、PHP、MySQLデータベースを使ってログインフォームを作成するには以下のように作成しました。
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
checklogin.php
<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="database"; // Database name
$tbl_name="login"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>
login_success.php
<?php
session_start();
if(!session_is_registered(myusername)){
header("location:login.php");
}
?>
<html>
<body>
Login Successful
</body>
</html>
それは動作しません。ユーザー名とパスワードが正しい場合、何も起こりません。どのように修正すればよいですか?私は本当にこのコードで何が間違っているかわからないし、本当にあなたの助けに感謝します!
に
mysql_real_escape_string
を使用しないでください。 –**警告**:PHPを習得しているだけの方は、古い['mysql_query'](http://php.net/manual/en/function.mysql-query.php)インタフェースを学ばないでください。それはひどいので、PHP 7では削除されました。[PDOは学ぶのが難しくありません](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo- for database-access /)、[PHP The Right Way](http://www.phptherightway.com/)のようなガイドはベストプラクティスの説明に役立ちます。 **あなたのユーザーパラメータが[正しくエスケープされている](http://bobby-tables.com/php)か、重大な[SQLインジェクションバグ](http://bobby-tables.com/ )。 – tadman
**警告**:独自のアクセス制御レイヤーを作成するのは簡単ではなく、重大な誤りを犯す機会がたくさんあります。 [Laravel](http://laravel.com/)のような最新の[開発フレームワーク](http://codegeekz.com/best-php-frameworks-for-developers/)がある場合は、独自の認証システムを作成しないでください。強力な[認証システム](https://laravel.com/docs/5.2/authentication)が組み込まれています。絶対に[推奨されるセキュリティのベストプラクティス](http://www.phptherightway.com/#security)に従ってください。決してパスワードをプレーンテキストとして保存しないでください。 – tadman