php - Create Unique LicenseIDs and Password for each loop -
i'm making webpage admin on page can generate specific amount of new users (depending on order). right function add new users, 1-many, working. although when create new members, not have unique "random"-licenses. users created under same generate have same.
admin.php (the code form , functions)
<head> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <meta charset="utf-8"> <title>generera licenser</title> <meta http-equiv="content-type" content="text/html" charset="iso-8859-1" /> </head> <?php include('template.php'); if(isset($_post['email'])) //inkluderar template och hämtar inlogg// { $characters = "0123456789abcdefghijklmnopqrstuvwxyz"; function genrandomstring($length = 10) { $characters ='0123456789abcdefghijklmnopqrstuvwxyz'; $string = ''; ($p = 0; $p < $length; $p++) { $string .= $characters[mt_rand(0, strlen($characters))]; //funktion för slumpmässig kod// } return $string; } $courseid = '1'; $antal = $_post['antal']; $role = '3'; //detta gör att användaren som skapas får rollvärdet 3//* $password = genrandomstring();//genererar slumpmässig kod för licens och lösenord// $licenseid = genrandomstring(); mysql_query('set character_set_results=utf8'); mysql_query('set names=utf8'); mysql_query('set character_set_client=utf8'); mysql_query('set character_set_connection=utf8'); //dessa löser åäö inmatningar till databasen.(kan vara redundanta). mysql_query('set character_set_results=utf8'); mysql_query('set collation_connection=utf8_swedish_ci'); for( $tmp = 1; $tmp <= $antal; $tmp++){ $query = <<<end insert user(email, company, courseid, password, licenseid, role) values('{$_post['email']}','{$_post['company']}', '$courseid', '$password','$licenseid', '$role'); end; //för in data från formuläret till databasen via poster.// $mysqli->query($query) or die(mysql_error()); } header('location:bekraftat.php'); //när genereringen är genomförd skickas man till en bekräftelse sida. } $content = <<<end <div class="row"> <div class="container"> <div class="jumbotronadmin"> <div class="jumbotron"> <div class="container"> <h2>generera licenser</h2> <form action="admin.php" method="post"> <div class="form-group"> <input type="text" class="form-control" aria-describedby="basic-addon1" name="email" placeholder="email" maxlength="40" pattern="[a-za-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" title="ej en legitim emailadress" required> </div> <div class="form-group"> <input type="text" class="form-control" aria-describedby="basic-addon1" name="company" placeholder="företag" maxlength="40" patter="[a-za-z0-9]+$" required> </div> <div class="form-group"> <input type="text" class="form-control" aria-describedby="basic-addon1" name="antal" placeholder="antal licenser" maxlength="3" pattern="[0-9]+$" title="obs! endast siffror" required> </div> <div class="form-group"> <div class="checkbox"> <label> <input type="checkbox" aria-describedby="basic-addon1" name="courseid"><p>webbutbildningen brandskyddskunskap</p> </label> </div> </div> <br> <input type="submit" class="btn btn-default" value="generera"> </form> </div><!-- stänger jumbotronen --> </div><!-- stänger jumbotronadmin --> </div><!-- stänger container --> </div><!-- stänger row --> end; //innehållet på admin.php med formulär// echo $navigation_admin; echo $content; echo $header; // hämtar navigationsmenyn för admin, innehåll och logotyper.// ?>
so, question, have idea how make loop creates unique licenses , passwords?
as per current code,fix ,you need call genrandomstring
function inside of loop.
for ($tmp = 1; $tmp <= $antal; $tmp++) { $password = genrandomstring(); $licenseid = genrandomstring(); $query = <<<end insert user(email, company, courseid, password, licenseid, role) values('{$_post['email']}','{$_post['company']}', '$courseid', '$password','$licenseid', '$role'); end; //för in data från formuläret till databasen via poster.// $mysqli->query($query) or die(mysql_error()); }
and if want ever non repitive string try
function genrandomstring($length = 10) { $string = md5(microtime()); return $string; }
Comments
Post a Comment