2014-07-14 42 views
1

我使用PHP创建了一个简单的单词密码登录。密码存储在php文件中 - 我知道这不是大规模安全的,但我只想让人们无法漫步到该网站。登录表单提交按钮不起作用

为了测试密码的目的是密码

你可以看到,我有一个jQuery摇效果时,这是错误的。

我的问题:

为什么我的提交按钮不正确的密码在击中提交按钮工作,为什么不同的击球回报的行为,以提交按钮?

我的代码:

HTML:

<!DOCTYPE html> 
<html lang="en"> 
    <head> 

    <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script> 
    <script type="text/javascript" src="js/vibrate.js"></script> 

    </head> 
    <body> 

     <!-- body content --> 
     <div class="wrapper"> 

      <!-- container --> 
      <div class="container"> 

      <div class="col-lg-12"> 
       <div id="login" class="form-wrapper content-flood"> 
        <form action='php/login.php' method="post" class="login-form" id="loginform"> 
          <h3 class="text-center">Enter your password:</h3> 
          <input type="password" name="password"> 
          <input type="button" name="Submit" id="submit_butt" value="Submit" formaction="php/login.php"/> 
        </form> 
       </div> 
      </div> 

      </div> 
      <!-- end container --> 

     </div> 
     <!-- end body content --> 

<script type="text/javascript"> 

$(document).ready(function() { 

    $("#submit_butt").click(function() { 

     // configurations for the buzzing effect. Be careful not to make it too annoying! 
     var conf = { 
       frequency: 5000, 
       spread: 5, 
       duration: 600 
      }; 

     /* do your AJAX call and processing here... 
      .... 
      .... 
     */ 

     // this is the call we make when the AJAX callback function indicates a login failure 
     $("#login").vibrate(conf); 

     // let's also display a notification 
     if($("#errormsg").text() == "") 
      $("#loginform").append('<p id="errormsg">Oops, wrong password!</p>'); 

     // clear the fields to discourage brute forcing :) 

     $("#pwd").val(""); 
    }); 

}); 
</script> 

</body> 
</html> 

PHP:

<?php 
// this password may come from any source. 
// it's a variable for the sake of simplicity 

$password = 'password'; 

if($_POST['password'] == $password){ 
    $_SESSION["userid1"] = $id1; 
header("Location: ../home.html"); // redirects 
}else{ 

} 
?> 

的Javascript:

jQuery.fn.vibrate = function(conf) { 
     var config = jQuery.extend({ 
       speed: 30, 
       duration: 2000, 
       frequency: 10000, 
       spread: 3 
     }, conf); 

     return this.each(function() { 
       var t = jQuery(this); 

       var vibrate = function() { 
         var topPos = Math.floor(Math.random() * config.spread) - ((config.spread - 1)/2); 
         var leftPos = Math.floor(Math.random() * config.spread) - ((config.spread - 1)/2); 
         var rotate = Math.floor(Math.random() * config.spread - (config.spread - 1)/2); // cheers to [email protected] for the rotation-idea 
         t.css({position: 'relative', left: leftPos +'px', top: topPos +'px', WebkitTransform: 'rotate(' +rotate +'deg)', WebkitTransform: 'rotate(0deg)'}); 
       }; 

       var doVibration = function() { 

         var vibrationInterval = setInterval(vibrate, config.speed); 

         var stopVibration = function() { 
           clearInterval(vibrationInterval); 
           t.css({position: 'static'}); 
         }; 

         setTimeout(stopVibration, config.duration); 
       }; 

       /* 
        Mofication by Kishore - I am commenting out the following line as it calls the vibration function repeatedly. 
        We need to call it only once. So, instead I make a call to doVibration() directly. 
       */ 

       //setInterval(doVibration, config.frequency); 
       doVibration(); 
     }); 
} 

;

编辑:我离开'其他'在PHP空的借口jQuery处理错误?

我无法得到这个工作的JSFIDDLE,但这里是网站的行动(这也是一个测试版本)。

http://hea.getevent.info/

+0

什么哟意思提交按钮不起作用,回报的行为不同。你能解释一下具体的问题吗? –

+0

用正确的密码输入密码可让您进入该网站。用错误的密码输入时显示空白'login.php'。 做同样的事情,然后点击提交只是说,哎呀错误的密码 – lejimmie

回答

2

使用输入类型提交的,而不是按钮

<input type="submit" name="Submit" id="submit_butt" value="Submit" formaction="php/login.php"/> 

否则,您可以使用输入型按钮,使用AJAX提交表单。

+0

真棒 - 这工作,但在网站加载之前,然后一秒我得到了错误信息:( – lejimmie

+0

请正确解释什么错误信息? –

+0

所以我改变了输入类型使提交按钮与他的密码正确工作,并加载该网站,但同时这样做了大约一秒钟,它摇动,'哎呀,错误的密码出现'在进入网站之前 – lejimmie

0

你不提交表单,这就是为什么它没有做认证

你可以只改变从按钮的输入类型提交:

<input type="submit" name="Submit" id="submit_butt" value="Submit" formaction="php/login.php"/> 

或只是提交表单在点击事件:

$("#submit_butt").click(function() { 
    . 
    . 
    . 

    $("#loginform").submit(); 
} 
1

监听器连接到提交按钮相反的,你应该对表单的提交操作注册的监听器。您可以同时捕获提交按钮点击(一旦它已经变成一个提交按钮,其它的问题都说明),有人只是打自己的键盘上输入方式:

$(".login-form").on("submit", function(e) 
{ 

    // Stop the form submitting 
    e.preventDefault(); 

    // Do your checks for errors 

    // If there are none 

    $(this)[0].submit(); 

}); 
+0

我看到你的逻辑!所以这个目标是整个表单,而不仅仅是提交...在e.preventDefault();我写我的if {} else {$(this)[0] .submit(); }? – lejimmie

+0

是的,确切地说。这样你就可以捕获表单的所有提交事件,因为用户可能不会点击提交按钮,而是敲击键盘上的回车键。 – Alex

1

你的服务器返回一个HTTP 302代替HTTP 200。我能够登录,但我确实看到了摇晃,并且基本上是由于逻辑不佳导致的。

Remote Address:212.48.79.185:80 
Request URL:http://hea.getevent.info/php/login.php 
Request Method:POST 
Status Code:302 Moved Temporarily 
Request Headersview source 

而你的逻辑看起来不太正确,除非我失去了一些东西。我们看不到你的ajax,但是下面的语句需要在ajax调用的成功回调中。输入和按钮点击对我来说都是一样的。 在你的ajax调用完成之前,下面的代码被激发得太早了,而且你也不会阻止默认的表单提交,所以它也提交了....两次。

// this is the call we make when the AJAX callback function indicates a login failure 
    $("#login").vibrate(conf); 

    // let's also display a notification 
    if($("#errormsg").text() == "") 
     $("#loginform").append('<p id="errormsg">Oops, wrong password!</p>'); 

而你需要防止默认,这样你可以做你自己提交你确认密码后:

$("#submit_butt").click(function(e) { 
    e.preventDefault(); 
    ....... 
}); 
+0

这对我来说都是新的,再加上它是由各种教程组合而成的,所以绝对是错误的逻辑! :)我会尽力实现这一点,并会更新我的进度 – lejimmie

+0

你会到达那里...... :)我的经验是,我通过尝试更快地学习......而我尝试的越多......越多我犯的错误....我犯的错误越多,我学到的越多。所以这都很好。你在正确的轨道上。 – PeterKA

+0

绝对的,继续练习,直到你知道它的内心!不幸的是,这些天很多教程假设你已经有一些知识,尤其是技术术语:( 我修改了代码与您的修正和摇动是好多了,但正确的密码不再工作:( – lejimmie