php - Key Generator without storing in database -
my code creates random key
, stores in database
on button click,keygen
button name, if
user/customer has key gets key
database in text box on click else
generates new key
, stores in database
. there way can achieve without storing key
in database , same key particular user/customer each time.
code:
<?php include('session.php'); $result = mysql_query("select * customer customer_no = '$customer_no'"); $row = mysql_fetch_array($result); if (isset($_post['keygen'])){ $customer_no = $_post['customer_no']; $customer_no = mysql_real_escape_string($customer_no); $result = mysql_query("select * customer customer_no = '$customer_no'"); while ($row = mysql_fetch_assoc($result)) { $keystring = $row['key']; if($keystring == ""){ $keystring = mysql_real_escape_string($keystring); $query = mysql_query("update customer set `key` = '$keystring' customer_no = '$customer_no'"); } else{ $keystring = $row['key']; }}} function generaterandomstring($length = 10) { $characters = '23456789abcdefghjkmnpqrstuvwxyz'; $randomstring = ''; ($i = 0; $i < $length; $i++) { $randomstring .= $characters[rand(0, strlen($characters) - 1)]; } return $randomstring; } ?>
my html is,
<div id="content" class="box2"> <div class="login"> <form action="" method="post" style="margin:12px;"> <table class="nostyle"> <tr> <td align="center"> <label style="font-size:16px;"><strong>customer id: </strong></label> <select name="customer_no"> <?php $result_customer= mysql_query('select customer_no customer order customer_no'); ?> <?php while($row_customer= mysql_fetch_assoc($result_customer)) { ?> <option <?php if ($row_customer['customer_no']=='') { ?> selected="selected"<?php } ?>> <?php echo htmlspecialchars($row_customer['customer_no']); ?> </option> <?php } ?> </select> </td> </tr> <tr> <td align="center"><label style="font-size:16px;"><br /><strong>register key: </strong> </label> <input type="text" id="key" class="input-text" name="key" size="20" align="middle" value = " <?=$row["key"];?>"></td> </tr> <td align="center"><br /><input type="submit" id="keygen" class="input-submit" name="keygen" value="generate" onclick=""/> </td> </tr> </table> </form> </div> </div>
please help,i'm newbie.
one way create hash 1 (or more) customer properties. hash function creates string source string. same source string allways generate same hash.
but have take care of 2 problems hashs: can't generate source string hash (because hashing algorithm non reversible) , have take care of possible hash collisions (two differents strings generating same hash). quite unlikely given hash has enough length (md5 has 32 characters, sha1 40 long...).
if need 2 way encryption (getting string username, generating username string), you'll need @ encryption techniques.
have @ mcrypt library provide many differents encryption/hashing algorithms. i'm sure you'll find 1 suit needs.
Comments
Post a Comment