如何生成随机密码

时间:2013-02-19 06:20:18

标签: php

我只想从学生的所有查询结果中随机输入密码。在我的情况下,我试图生成但它只返回1个生成的密码给所有被查询的学生(约45名学生)。我该怎么做一个随机的。帮助.. 继承我的代码

   <?php
   if(isset($_POST['generate'])){
   $charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()_+';
    $generated_pass = substr(str_shuffle($charset), 0, 12);
    $generate = $_POST['generate'];
    }
    ?>
    <form method="post" action='enroll_student.php' name ='register'>

     <?php
     $yr = date("Y");
     if ($result = $mysqli->query("SELECT
     tbl_studentreg.studId,
     tbl_studentreg.fname,
     tbl_studentreg.lname,
     tbl_studentreg.mname,
     tbl_studentreg.dob,
     tbl_studentreg.address,
     tbl_department.departmentName,
     tbl_studentreg.sy
     FROM tbl_studentreg
     Inner Join tbl_department ON tbl_studentreg.departmentId = tbl_department.departmentId WHERE tbl_studentreg.sy =  '$yr' "))
                    {
       if ($result->num_rows > 0)
       {
                echo "<table width= '1000'>";
                echo "<tr><th>StudentID</th><th>Name</th><th>Date of Birth</th><th>Address</th><th>Department</th><th>School Year</th><th>Password</th></tr>";

         while ($row = $result->fetch_object())
          {
           echo "<tr>";

        echo "<td align='center'>" . $row->studId . "</td>";  
        echo "<td align='center'>" . $row->fname . " ". $row->mname ." ". $row->lname ." </td>";
    echo "<td align='center'>".$row->dob."</td>";
    echo "<td align='center'>" . $row->address. "</td>";
    echo "<td align='center'>".$row->departmentName."</td>";
    echo "<td align='center'>".$row->sy."</td>";

   if(isset($generated_pass)) {
        //how could i make this one generate random password for every students..?
    for($i=0; $i <= $row->studId; $i++){              
    echo "<td>$generated_pass</td>";
        }
        }
  echo "</tr>";
        }
echo "</table>";
                }
              else
                {
              echo "No Results.";
                }
                }
              else
                {
              echo "Error: " . $mysqli->error;
                 }
            $mysqli->close();
            echo '<br>';
    include 'count.php'; //this one will give the total no. of results, just ignore.
            ?>

               <br />        

          <tr><td></td></tr><tr><td><input type='submit' name='generate' value='Generate'/></td></tr>
        </table>
          </form>

1 个答案:

答案 0 :(得分:1)

function genpass(){
$charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()_+';
    return substr(str_shuffle($charset), 0, 12);
}

// INSIDE THE LOOP
$generated_pass = genpass();
echo "<td>$generated_pass</td>";

类似的东西。