2

I took the following code from another webpage "http://www.javascriptkit.com/script/cut10.shtml".

<SCRIPT>
function passWord() {
    var testV = 1;
    var pass1 = prompt('Please Enter Your Password',' ');
    while (testV < 3) {
        if (!pass1) history.go(-1);
        if (pass1.toLowerCase() == "letmein") {
            alert('You Got it Right!');
            window.open('protectpage.html');
            break;
        } 
        testV+=1;
        var pass1 = prompt('Access Denied - Password Incorrect, Please Try Again.','Password');
    }
    if (pass1.toLowerCase()!="password" & testV ==3) history.go(-1);
    return " ";
}
</SCRIPT>
<CENTER>
<FORM>
<input type="button" value="Enter Protected Area" onClick="passWord()">
</FORM>
</CENTER>

It works only in case the password is entered second time, but not when it is entered first time. When you enter password it says wrong password prompting you to enter the password again and then it goes through.I need a script that shall prompt me of the correct password, in case I enter the wrong password. Can any one help me with the code as i am a beginner in JavaScript.

4

4 に答える 4

4

リンクにアクセスすると、コードが機能しているように見えます。

私はあなたが学んでいることを知っています。それでも、実際には何も保護していないため、このような認証を行うべきではありません。任意のブラウザーで [ページ コードの表示] オプションを使用すると、誰でもソース コードを読み取ることができます (通常は、ページを右クリックします)。これは、誰でも簡単にパスワードを取得できることを意味します。

真の認証を行うには、サーバー側の言語 (PHP など) を使用するか、Web サーバーで構成されたHTTP ダイジェスト認証を使用する必要があります。Digest は MD5 を使用しているため少し時代遅れですが、あなたがしていることよりも 100 万倍優れています。

Apache Web サーバーで HTTP ダイジェストを設定する方法の詳細については、次を参照してください。

http://httpd.apache.org/docs/2.2/mod/mod_auth_digest.html

Nginx で同じことを行う場合:

http://wiki.nginx.org/HttpAuthDigestModule

HTTP 基本認証も機能しますが、パスワードはユーザーのブラウザからプレーン テキストで送信されます。HTTP ダイジェストでは、パスワードはハッシュされます。

JavaScript を学んでいることを知っているので、最善の策は、使用している Web サーバーを構成することです。ほとんどの Web ホスティング サービスは Apache を使用しているため、ほとんどの場合、.htaccess ファイルを使用できます。これを設定する方法に関するチュートリアルについては、「.htaccess http ダイジェスト」を検索できます。

一部の Web ホスティング サービスには、ダイジェスト/基本認証を使用してディレクトリを保護する機能を備えたコントロール パネルがあります。非常に一般的な cPanel では、 「パスワード保護ディレクトリ」と呼ばれます。

あなたがもっと上級者なら、PHP で行うことをお勧めしますが、それはかなり複雑な問題です。

于 2013-08-07T05:27:16.600 に答える
0

これを試してください..falseケース内にアクセス拒否プロンプトを追加するだけです...

  function passWord() 
    {
             var testV = 1;
             var pass1 = prompt('Please Enter Your Password',' ');
             while (testV < 3) 
             {
                    if (!pass1) 
                    {
                           history.go(-1);
                           var pass1 = prompt('Access Denied - Password Incorrect, Please Try     
                           Again.','Password');
                    }
                    else if (pass1.toLowerCase() == "letmein") 
                    {
                         alert('You Got it Right!');
                         window.open('protectpage.html');
                         break;
                    } 
                    testV+=1;

              }
              if (pass1.toLowerCase()!="password" & testV ==3) 
              history.go(-1);
              return " ";
    } 
于 2013-08-07T05:23:54.127 に答える