2017-02-18 5 views
0

パスワードの回復コードを書きました。既存のユーザーが必要なフィールドに自分のメールアドレスを入力すると、パスワード回復用のリンクが電子メールで送信され、受信したリンクをクリックすると、パスワード回復ページが表示されます。それは完璧に働いています。パスワードリカバリ用の電子メールの時刻を設定する

しかし、私は時間のリンクが必要です。私が60秒以内に電子メールのパスワード回復リンクのための60秒を設定すれば。ユーザーがパスワードを回復できないと、リンクが失敗します。どうすればいいか分かりませんか?私にヒントを与えてください。表中の

私のフィールドは、次のとおりです。名前、電子メール、モバイル、パスワード、パスコード

update.phpを(インサートメールが予備のメールリンクを送信するために)

<?php 
include("connection.php"); 
extract($_POST); 
if(isset($email)==true) 
{ 
$query=mysql_query("select * from account where email='$email'") or die(mysql_error());  
$result=mysql_num_rows($query); 

if($result==1) 
{ 
$salt = "498#2D83B631%3800EBD!801600D*7E3CC13"; 
    $restkey = hash('sha512', $salt.$email); 

mysql_query("update account SET passcode='$restkey' where email='$email'") or die(mysql_error()); 

$to="$email"; 
$subject="Password Reset"; 
    $pwrurl = "demo.cstechnology.net/updatepass.php?restkey=".$restkey; 
$message="To reset your password, please click the link below. If you cannot click it, please paste it into your web browser's address bar.\n\n" . $pwrurl . "\n\nThanks,\nThe Administration"; 
    $header="FROM:[email protected]"; 
    mail($to,$subject,$message,$header);  

    } 
    else 
    { 
echo "Invalid Email"; 

} 
} 

?> 
<form action="" method="post"> 
<table> 
<tr><td>Email</td><td><input type="email" required name="email" /></td></tr> 
    <tr><td></td><td><input type="submit" name="submit" value="submit" /></td></tr> 
    </form> 

updatepass。 php(電子メールのリカバリ電子メールリンクをクリックすると、パスワードが更新されます)

<?php 
    session_start(); 
    include("connection.php"); 
    extract($_REQUEST); 
    extract($_POST); 

    if(isset($submit)==true) 
    { 
    $query=mysql_query("update account SET password='$password' where passcode='$restkey'") or die(mysql_error()); 
    echo"Password Updated Successfully"; 
    } 



    if(isset($restkey)==true) 
    { 

    echo"<form action='' method='post'> 
    <table> 
    <tr><td>Password</td><td><input type='password' required name='password' /></td></tr> 
<tr><td></td><td><input type='submit' name='submit' value='submit' /></td></tr> 
</form>"; 
} 
?> 
+1

'$ salt =" 498#2D83B631%3800EBD!801600D * 7E3CC13 ";'塩は、保存されている各クレデンシャル([OWASP:パスワード保存チートシート](https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet#Use_a_cryptographically_strong_credential-specific_salt) – Andreas

+0

$塩はユニークで、$ emailで追加しました。 – user3189881

+0

私はライブ環境でこのコードを使用しません。それはあなたが思っているほど安全ではありません。 –

答えて

0

テーブルにtimestampカラムを追加する必要があります。そのため、ユーザがパスワードリカバリをクリックすると、ここで時間を節約できます。

ALTER TABLE account ADD timestamp INT 

ユーザーがupdatepass.phpを呼び出すと、現在の時刻とtableに保存されているタイムスタンプを比較する必要があります。

update.phpを

$resettimestamp=time(); 
$query=mysql_query("update account set timestamp=$resettimestamp where email='$email'"); 

updatepass.php

$query=mysql_query("select timestamp from account where email='$email'") or die(mysql_error());  
$row = mysql_fetch_row($query); 
$timestamp=$row[0]; 
$currtime=time(); 
$timewindow=60; // 60 seconds 
if ($currtime-$timestamp<=$timewindow) { 
//your logic to reset password 
} 
else { 
//Tell the user that he is too slow) 
} 

またmysqlのはもうPHP 7.0でサポートされていることを、注意していないので、代わりに

可能であれば mysqliのを使用してください
+0

ありがとうございます。それは働くことができます。私は確信しています。 – user3189881

+0

質問に答えてマークしてください。コードサンプルを数分で更新します –

+0

フィールドのタイプは何ですか? – user3189881

関連する問題