Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2017
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help needed for simple password field with redirect pages

    Hi all,

    I'm obviously not an expert. Can anyone please help me with html code to make a simple password field? I need it to redirect to one page if the password is correct and also redirect to a different page if the password is incorrect.

    It doesn't have to be a very secure login. It's for a simple website form to submit an answer that is either right or wrong.

    The following code is what I've been playing with and the redirect for the false password doesn't work. Also, it's a button with a popup prompt and I would actually prefer a text field to submit on keying enter.

    If anyone can help me with either problem, I would so appreciate any help.

    Code:
    <html>
    <HEAD>
    <SCRIPT LANGUAGE="JavaScript">
    function LogIn(){
    password="mypassword";
    password=prompt("Enter password:","");
    password=password.toLowerCase();
    if (password=="mypassword") { 
    loggedin=true;
    window.location="http://MyURLredirectRightPWD.html";
    }
    if (password==false) {
    loggedin=false;
    window.location="http://MyURLredirectWrongPWD.html";
    }
    }
    </SCRIPT> 
    <BODY>
    <center>
    <form><input type=button value="Enter Password" onClick="LogIn()"></form>
    </center>
    </body>
    </html>
    Last edited by VIPStephan; 01-09-2017 at 12:33 AM. Reason: added code BB tags

  2. #2
    Regular Coder
    Join Date
    Oct 2015
    Posts
    302
    Thanks
    1
    Thanked 38 Times in 36 Posts
    There is quite a lot wrong with this code:
    PHP Code:
    <html>
    <
    HEAD>
    <
    SCRIPT LANGUAGE="JavaScript">
    function 
    LogIn(){
    // Declare password as the password, therein giving away any yes / no answer to any user that looks at the source code
    password="mypassword";
    //Then overwrite the password with whatever the user enters
    password=prompt("Enter password:","");
    password=password.toLowerCase();
    if (
    password=="mypassword") { //Again giving away any yes / no answer
    //Since you are trying to forward to another web page, this is meaningless
    loggedin=true;
    //Unless there is a website MyURLredirectRightPWD.html, you will not be able to forward to this address; that's what the http:// does
    window.location="http://MyURLredirectRightPWD.html";
    }

    // password will not equal false unless the user enters "0" zero or has a cunning plan to return something which evaluates to false
    if (password==false) {
    //Since you are trying to forward to another web page, this is meaningless
    loggedin=false;
    //Unless there is a website MyURLredirectWrongPWD.html, you will not be able to forward to this address; that's what the http:// does
    window.location="http://MyURLredirectWrongPWD.html";
    }
    }

    </SCRIPT> 
    <BODY>
    <center>
    <form><input type=button value="Enter Password" onClick="LogIn()"></form>
    </center>
    </body>
    </html> 
    As noted in the comments, the answer to any question is given in the code which anyone can view, really you need to look at using a server-side language where such details would be hidden.

    If you follow the comments you should see why it is not doing what you expected, if it's not obvious, a little experimentation will help.
    Last edited by wha; 01-09-2017 at 01:40 AM.

  3. Users who have thanked wha for this post:

    Nicole107 (01-09-2017)

  4. #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    19,242
    Thanks
    212
    Thanked 2,671 Times in 2,647 Posts
    <SCRIPT LANGUAGE="JavaScript"> is long deprecated and obsolete. Use <script type = "text/Javascript"> instead (also obsolete but still required for IE<9).

    It is possible to obfuscate the password so that non-programmers cannot easily decypher it. Simple example:-

    Code:
    var password = "70617373776f7264";  // the concatenated ASCII codes for the letters in password
    var result = "";
    for(var i=0;i<password.length;i=i+2){result=result+'%'+password.substr(i,2);}
    var pwd = unescape(result);
    alert (pwd); // password
    Last edited by Philip M; 01-09-2017 at 10:20 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •