单击文本标记以生成新的哈希


$num = rand(0, 10000);
$hash = password_hash($num, PASSWORD_BCRYPT, array(
    'cost' => 6
));

以上是哈希

<p class="reg-code">Hash: <span><?php echo $hash; ?></span><i class="fa fa-refresh" aria-hidden="true" id="refresh-code"></i></p>

我希望它,所以当有人点击标签时它会在跨度中生成一个新的哈希值,我怎么会遇到这个?

我不确定我会怎么做,比如作为 PHP 中的函数或其他什么。

它应该是得到的。因为它不会更改服务器状态。只需返回新值。

$('.fa-refresh').click(function(){
   $.get( "./path/to/hashscript.php", function( data ) {
    $( ".reg-code > span" ).html( "Hash:" + data );
   });
});

和哈希脚本.php文件应包含

echo md5(uniqid(rand(), TRUE));

Ajax 允许您在不刷新的情况下将请求发送到另一个页面。 检查 jQuery 的这一部分 执行异步 HTTP (Ajax( 请求

在您的情况下,您可以像这样设置(首先,在页面头部包含jQuery(:

<p class="reg-code">Hash: <span></span>
<i class="fa fa-refresh" aria-hidden="true" id="refresh-code"></i></p>

请注意,将处理响应的<跨度>为空(但可以在开头显示原始哈希。当您单击链接时,它的内容无论如何都会被替换(当我们开始时,并且有一个 ID 附加到刷新链接

然后,你使用 jQuery:

$(document).ready( function() {
    $("#refresh-code").click(function(){ // click on element with ID 'refresh-code'
    $.ajax({ // method default is 'GET'
           url: "hash.exe.php", // the PHP page that will generate new hash
           success: function(html){ // if no PHP error 'html' is the response
                    $(".reg-code > span").html(html); // we append the html in the span
            },
            error: function (request, status, error) { // we handle error
            alert(request.responseText);
            }
          });
    });
});

PHP(hash.exe.php(将如下所示(保持原样处理,添加了echo'd答案(:

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$num = rand(0, 10000);
$hash = password_hash($num, PASSWORD_BCRYPT, array(
'cost' => 6
));
echo"$hash"; // this 'invisible' HTML output is echo'd back to the page who initiated it
?>

示例将所有小提琴放在一起

被称为Ajax:

<p class="reg-code">
  Hash: 
  <span id="hash"><?php echo $hash; ?></span>
  <i id="click" class="fa fa-refresh" aria-hidden="true" id="refresh-code"></i>
</p>
$('#click').click(function(){
  $.post( "./path/to/hashscript.php", function( data ) {
    $( "#hash" ).html( data );
  });
});

您可以使用 JavaScript 事件发出服务器请求,并使用响应更新页面元素。您的 PHP 脚本应该回显$hash

echo password_hash($num, PASSWORD_BCRYPT, ['cost' => 6]);

最新更新