【问题标题】:password generation using html and php使用 html 和 php 生成密码
【发布时间】:2019-05-02 02:42:03
【问题描述】:

单击 UI 中的文本字段时,我需要在文本字段中显示一个随机密码。

<div class="form-group col-md-2">
    <label>Username</label>
    <input type="text" class="form-control"  placeholder="Please Enter Details " name="username" id="username" value="<?php echo $value['id'] ?>">        
    <!--<input class="form-control" type="text" autocomplete="off" placeholder="Search here" onkeyup="drop_down(this.value);" name="val_text" id="val_text" onclick="drop_down_show('block');" onmouseout="drop_down_show('none');">-->
</div>

<div class="form-group col-md-2">
    <label>Password</label>    
    <input type="text"  class="form-control" onblur="randomPassword()" readonly="" name="password" id="password" value="<?php echo $value['password']?>">                        
</div>

【问题讨论】:

  • 到目前为止您尝试过什么?您希望密码采用什么格式?
  • 所以我猜你有一个javascript函数randomPassword(),你能提供它的代码吗?
  • 如果所有逻辑都发生在前端,那么将有更好的方法从 JavaScript 生成密码。您可以查看此解决方案Generate random string/characters in JavaScript
  • 函数 randomPassword() { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?"; $length = rand(10, PHP_INT_MAX);警报($长度); $password = substr( str_shuffle(sha1(rand() . time()) . $chars ), 0, $length );警报();返回$密码; }

标签: javascript php html twitter-bootstrap css


【解决方案1】:

首先生成密码,然后在您的输入中使用

function randomPassword ($length = 8) 
{ 
  $genpassword = ""; 
  $possible = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";  
  $i = 0;  
  while ($i < $length) {  
    $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); 
    if (!strstr($genpassword, $char)) {  
      $genpassword .= $char; 
      $i++; 
    } 
  } 
  return $genpassword; 
} 

<input type="text" class="form-control"  value="<?php echo randomPassword(); ?>">

【讨论】:

    【解决方案2】:

    问题是您在 PHP 内联代码块中的 PHP 语句之后缺少 ;

    &lt;?php echo $value['whatever'] ?&gt;

    ...应该变成...

    &lt;?php echo $value['whatever']; ?&gt;

    但是,如果您希望仅在用户选择 input 时显示 PHP 数据,您需要使用以下代码:

    onfocus='this.value = <?php echo $value["whatever"]; ?>'
    

    ...或...

    onclick='this.value = <?php echo $value["whatever"]; ?>'
    

    ...或者如果您只想在 input 为空或没有用户输入时显示它...

    onfocus='this.placeholder = <?php echo $value["whatever"]; ?>'
    onblur='this.placeholder = ""'
    

    【讨论】:

      猜你喜欢
      • 2015-05-12
      • 2014-12-27
      • 2019-05-25
      • 2019-01-14
      • 2014-03-27
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      相关资源
      最近更新 更多