2016-04-12 21 views
0

私はユーザ名とパスワードでログインしようとすると、私を覚えています。ログインして再度ログインページに戻ると、そこにユーザ名とパスワードは表示されません。私は間違って何をしているのですか?PHPで動作していないことを記憶してください

<?php 
$title = "Login Page"; 
$heading = "Login Form"; 
include "includes/home_page_header.php"; 
?> 
<?php 
$cookie_username = $cookie_password = ""; 


if(isset($_POST['login'])) 
{ 
    //echo "<pre>"; print_r($_POST); echo "</pre>";die; 
    if(!empty($_POST['cookie_username']) && !empty($_POST['cookie_password'])) 
    { 
     $cookie_username = $_POST["cookie_username"]; 
     $cookie_password = $_POST["cookie_password"]; 
$sql_users="SELECT * FROM users WHERE username='$username' AND password='$password'"; 
$res_users = mysqli_query($link,$sql_users); 

    if(mysqli_num_rows($res_users) > 0) 
     {  
       $_SESSION["Username"]= $cookie_username; 
       $_SESSION["Password"]= $cookie_password; 
       header('Location:http://localhost/sample/home_page.php');   
     } 
     else 
     { 
      echo 'The username or password are incorrect!'; 
     } 
    } 


if($_POST['rememberMe'] =='on') 
{  
    setcookie('cookie_username', $cookie_username, time() + (86400 * 30)); 
    setcookie('cookie_password', $cookie_password, time() + (86400 * 30)); 
    if(!isset($_COOKIE['cookie_username'])) 
    { 
     echo "Cookie named '" . $cookie_username . "' is not set!"; 
    } 
    else 
    { 
     echo "Cookie '" . $cookie_username . "' is set!<br>"; 
     echo "Value is: " . $_COOKIE['cookie_username']; 
    }   
} 
} 
?> 

<table class="login_table"> 
<form name="login" action="" method="post"> 
<tr> 
<td>User Name :-</td> 
<td><input type="text" name="cookie_username" value="<?php echo $cookie_username;?>"/></td> 
</tr> 
<tr> 
<td>Password :-</td> 
<td><input type="password" name="cookie_password" value="<?php echo $cookie_password;?>"/></td> 
</tr> 
<tr> 
<td></td> 
<td><input type="checkbox" name="remember" <?php if(isset($_COOKIE['remember'])) { 
     echo 'checked="checked"'; 
    } 
    else { 
     echo ''; 
    } 
    ?> >Remember Me 
</td> 
</tr> 
<tr> 
<td></td> 
<td><input type="submit" name="login" value="Login"/> 
<input type="submit" name="cancel" value="Cancel"/></td> 
</tr> 
</form> 
</table> 
+3

パスワードをクッキーに保存しないでください。 – syck

+0

'$ cookie_username'変数は、ログインページから来た場合にのみ設定されますが、再度入力した場合は設定されません。 – syck

+0

ログインシステムとセッションシステムは非常に機密です。ベストプラクティスに関する多くの論理と議論があります。あなたの質問に基づいて、基本的な暗号化方法、衛生、適切な機密データ保管技術について最初に学ぶことをお勧めします。 – jjonesdesign

答えて

2

私を覚えている機能がと比較することができる、その後、誰かがログインするたびに生成されたトークンのようなものが含まれている必要があり


クッキーに機密データを保管しないでくださいデータベース。ユーザーが自分のユーザー名/パスワードをフォームに記憶させたい場合は、ログイン時に「ログイン情報を記憶」をクリックしてクライアントサイトを実行する必要があります。とにかく

、あなたの質問に答えるために...ページがロードされると

、あなたは空

$cookie_username = $cookie_password = ""; 

として、ユーザー名/パスワードの変数を設定しているあなたは

のようなものに次の行を変更する必要があります
$cookie_username = isset($_COOKIE['cookie_username']) ? $_COOKIE['cookie_username'] : ''; 
$cookie_password = isset($_COOKIE['cookie_password']) ? $_COOKIE['cookie_password'] : ''; 

また、

<input type="checkbox" name="rememberMe" value="on" <?=isset($_COOKIE['rememberMe']) ? 'checked="checked"' : ''?> 
01にあなたのチェックボックスを変更してください
1

あなたが名前の入力ボックスを収集している「リメンバー・ミー」しかし、あなたは、名前の入力を掲示していない「リメンバー・ミー」

if($_POST['rememberMe'] =='on') 

あなたは「覚えている」投稿と「リメンバー・ミー」

を集めています
<td><input type="checkbox" name="remember" 

だから、どちらかの変更:

<td><input type="checkbox" name="remember" 

また

if($_POST['remember'] =='on') 

から

<td><input type="checkbox" name="rememberMe" 

それとも

if($_POST['rememberMe'] =='on') 

、あなたはおそらくクッキーに機密情報を保存しないでください。 THISを読んでください。

関連する問題