SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript redirect

    Hi..
    I'm developing a site that needs a restricted area.
    So I created a small javascript function to check user and password.
    And then redirects to the homepage of the restricted area.

    Code:
    <script type="text/javascript">
    function Login(){
    	var done=0;
    	var username=document.login.username.value;
    	username=username.toLowerCase();
    	var password=document.login.password.value;
    	password=password.toLowerCase();
    	if (username=="user" && password=="pass") {
    		window.location="internal.php";
    		done=1;
    	}
    	if (done==0) {
    		alert("User or password incorrect.");
    	}
    }
    </script>
    Form:
    Code:
    <form name="login" method="post" action="">
    <input name="username" class="campos" id="username" title="user" type="text" value="user" size="22" style="color:#666; background-color:transparent"  onblur="if (this.value=='') this.value='user'" onfocus="if (this.value=='user') this.value=''" />
    <input name="password" class="campos" id="password" title="pass" type="password" value="pass" size="14" style="color:#666; background-color:transparent" onblur="if (this.value=='') this.value='pass'" onfocus="if (this.value=='pass') this.value=''" />
    <input name="Submit" type="submit" class="botao" value="OK" style="color:#666; border-color:#CCC; background-color:transparent; border:1;" onclick="Login()"/>
    </form>
    When I type incorrect username or password, I get the error message, but when I type correct username and password does not redirect to page "internal.php" and will not appear error message also. Just stay in the same page.

    Can anyone help me?

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,869
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You need to return false on the end of the JavaScript or the action for the form will still be triggered.

    Also you'd do better to attach that code to the onsubmit on the form tag rather than the onclick on the submit button.

    Finally that code will not prevent prople accessing the restricted page since all of the JavaScript will be available to be viewed by your visitors so they will be able to tell what the user and password are as well as the address of the restricted page.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    You need to return false on the end of the JavaScript or the action for the form will still be triggered.

    Also you'd do better to attach that code to the onsubmit on the form tag rather than the onclick on the submit button.

    Finally that code will not prevent prople accessing the restricted page since all of the JavaScript will be available to be viewed by your visitors so they will be able to tell what the user and password are as well as the address of the restricted page.
    I changed the code but still it does not wok.

    Code:
    <script type="text/javascript">
    function Login(){
    	var done=0;
    	var username=document.login.username.value;
    	username=username.toLowerCase();
    	var password=document.login.password.value;
    	password=password.toLowerCase();
    	if (username=="user" && password=="pass") {
    		window.location="internal.php";
    		done=1;
    	}
    	if (done==0) {
    		alert("User or password incorrect.");
    	}
            return false;
    }
    </script>
    Code:
    <form name="login" method="post" action="" onsubmit="Login()">
    <input name="username" class="campos" id="username" title="user" type="text" value="user" size="22" style="color:#666; background-color:transparent"  onblur="if (this.value=='') this.value='user'" onfocus="if (this.value=='user') this.value=''" />
    <input name="password" class="campos" id="password" title="pass" type="password" value="pass" size="14" style="color:#666; background-color:transparent" onblur="if (this.value=='') this.value='pass'" onfocus="if (this.value=='pass') this.value=''" />
    <input name="Submit" type="submit" class="botao" value="OK" style="color:#666; border-color:#CCC; background-color:transparent; border:1;"/>
    </form>
    Unfortunately, I can not use a database to register a username and password, this was the only way I could think of without using the database.
    There is another way?

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wops... it works

    Code:
    <form name="login" method="post" action="javascript:Login()">
    Thanks!!

    There is a better way to do this without using database?

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,869
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You should use action="" onsubmit="Login();return false" rather than put the code in the action.

    A better solution would be to use a server side language to do the test and redirect. You could still hard code the username and password in a PHP script (or whatever other language you have available on the server) so as to not need a database.

    The problem with JavaScript is that people will be able to view the JavaScript to see what values to enter in the form or alternatively just type the destination address into their browser address bar so as to bypass the form completely.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

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