2013-07-30 94 views
0

我已经用我自己的代码在php登录,现在我不那么擅长jquery ajax等等, 我的登录使用ajax jquery类型json,我把所有vals并将它们发布到服务器端的php,它会检查所有的细节,并通过相同的jquery ajax回复答案。使用ajax响应和哈希问题使用ajax响应和哈希问题使用jQuery的jQuery类型json

问题是我添加了nonce标记,在php中以mading形式登录表单,并且每次用户尝试登录nonce更改后,问题都是只有当我刷新登录页面时,nonce才更改为很好的nonce else将保持相同的nonce标记,并将发送不是更新的帖子,因为ajax在登录后没有刷新页面。

所以问题是我如何触发ajax在每次响应后刷新随机标记? nonce token是用php写的。

和有关散列随机数令牌,还有一点它使散列字符串的某个时候:

asdaskjn34kj + SDF/SD =

现在AJAX的jQuery自动删除“+”从哈希字符串,其这里给错误的标记在开机自检 我的散列函数:

public static function RandomBytes($count, $printable=FALSE) 
    { 
     $bytes = ''; 

    // supress warnings when open_basedir restricts access to /dev/urand 
     if(@is_readable('/dev/urandom') && ($hRand = @fopen('/dev/urandom', 'rb')) !== FALSE) 
     { 
      $bytes = fread($hRand, $count); 
      fclose($hRand); 
     } 
    if((strlen($bytes) < $count) && function_exists('mcrypt_create_iv')) 
    { 
     // Use MCRYPT_RAND on Windows hosts with PHP < 5.3.7, otherwise use MCRYPT_DEV_URANDOM 
     // (http://bugs.php.net/55169). 
     if ((version_compare(PHP_VERSION, '5.3.7', '<') && strncasecmp(PHP_OS, 'WIN', 3) == 0)) 
      $bytes = mcrypt_create_iv($count, MCRYPT_RAND); 
     else 
      $bytes = mcrypt_create_iv($count, MCRYPT_DEV_URANDOM); 
    } 
    if((strlen($bytes) < $count) && function_exists('openssl_random_pseudo_bytes')) // OpenSSL slow on Win 
    { 
     $bytes = openssl_random_pseudo_bytes($count); 
    } 
    if ((strlen($bytes) < $count) && @class_exists('COM')) 
    { 
     // Officially deprecated in Windows 7 
     // http://msdn.microsoft.com/en-us/library/aa388182%28v=vs.85%29.aspx 
     try 
     { 
      $CAPI_Util = new COM('CAPICOM.Utilities.1'); 
      if(is_callable(array($CAPI_Util,'GetRandom'))) 
      { 
       $bytes = $CAPI_Util->GetRandom(16,0); 
       $bytes = base64_decode($bytes); 
      } 
     } 
     catch (Exception $ex) 
     { 
     } 
    } 
     if (strlen($bytes) < $count) 
     { 
      // This fallback here based on phpass code 
      $bytes = ''; 
      $random_state = microtime(); 
      if (function_exists('getmypid')) 
       $random_state .= getmypid(); 

      for ($i = 0; $i < $count; $i += 16) { 
       $random_state = 
        md5(microtime() . $random_state); 
       $bytes .= 
        pack('H*', md5($random_state)); 
      } 
      $bytes = substr($bytes, 0, $count); 
     } 

     if ($printable) 
      return base64_encode($bytes); 
     else 
      return $bytes; 
    } 

任何一个知道如何改变这种功能,使没有“+”字符串在hashesh?

回答

0

要改变散列函数,如果只是“+”的问题,你可以保持检查的同时创建的字符串,

next_char = Randomly-created-char; 
if(next_char == '+'){ 
//do nothing 
} else{ 
hash .= next_char; 
} 

这里是HTML和PHP文件应该是怎么样的。

ajax调用显示在.html文件中。

.php第一次加载您的表单。

<!DOCTYPE html> 

<html> 
<head> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    $("#check").click(function(){ 
     $("#keyvalue").text($("#key").val()); 
    }); 
    $("#submit").click(function(){ 
    var text = $("#text").val(); 
    var key = $("#key").val(); 
     $.ajax({ 
      url: 'trial.php', 
      data: {text: text, key:key}, 
      type: 'POST', 
      dataType: 'json', 
      success: function(data) { 
       if(data.status == "fail"){ 
        $("#status").html(data.message); 
       }else{ 
        $("#status").html(data.message); 
        $("#key").val(data.key); 
        $("#keyvalue").text(''); 
       } 
      } 
     }); 
     return false; 
    }); 
}); 
</script> 
</head> 
<body> 
    <form method="post" action="trial.php" onsubmit="return send_form();"> 
     <input type="text" name="text" id="text"/> 
     <input type="hidden" id="key" name="key" value="<?php echo $key?>"/> //Look here. 
     <button id="submit">Send data and get new key</button> 
    </form> 
    <br><br> 
    <div id="status"></div> 
    <br><br> 
    <button id="check">What's current value of key?</button> --------> <span id="keyvalue"></span> 

    <div id="response"></div> 
</body> 

</html> 

.PHP

<?php 

//You get the form contents here. 

$key = isset($_POST['key']) ? $_POST['key'] : "error"; 
$text = isset($_POST['text']) ? $_POST['text'] : "empty"; 

//Check them if it matches with DB's entry, if doesn't you set $key = "error"; 

if($key=="error"){ 
    $status = "fail"; 
    $message = "There was some error processing your form."; 
    exit; 
} else{ 

    //You generate another random key. 
    $random =''; 
    for ($i = 0; $i < 10; $i++) { 
     $random .= chr(mt_rand(33, 126)); 
    } 

    //Now here in steps save it to your DB. So that when next form is submitted you can match it. 
    //And send back response to html file, where ajax will refresh the key. 
    $status = "success"; 
    $message = " 
    Your form was processed succesfully<br> 
    The text you sent was ".$text.", and the key you sent was ".$key.". 
    The new key sent to you can be seen by pressing the button below, has value, ".$random."<br><br> 
    "; 
    } 

    echo json_encode(array("status" => $status, "message" => $message, "key" => $random)); 

?> 

希望这有助于你。

在第一次生成表单时,您必须提供没有任何ajax的密钥和随机数,当使用此后续密钥时将调用ajax函数。

echo "<input type='hidden' id='key' name='key' value='".$key."'>"; 

echo "<input type='hidden' id='nonce' name='nonce' value='".$nonce."'>"; 
+0

在这里,我添加了你可以做的事情,在你的散列键中没有'+'。 –

+0

这里我编辑了答案。 –

0

这是因为我已经遇到了同样的问题,真正有用的 - 我曾考虑为登录的自动刷新页面,但是这是一个真正给用户带来不便 - 我还加了块以便IP和/或用户在5次失败尝试后被阻止