PHP, generate string with format, check against SQL db? -
i'm trying use php way process in webpage, typical language java unfamiliar how done product keys.
this process:
1. generate random string format xx-xxxx-xxxx-xxx mix of numbers , letters. 2. check if exists in sql database 3. if exists, generate 1 , repeat?
so how using php? please explain need , best way of going it.
generate random string following function.
<?php function randomstring() { $alphabet = "abcdefghijklmnopqrstuwxyzabcdefghijklmnopqrstuwxyz0123456789"; $pass = array(); //remember declare $pass array $alphalength = strlen($alphabet) - 1; //put length -1 in cache $array_lengths = array(2,4,4,3); foreach($array_lengths $v){ ($i = 0; $i < $v; $i++) { $n = rand(0, $alphalength); $pass[] = $alphabet[$n]; } $pass[] = '-'; } return rtrim(implode($pass),'-'); //turn array string } echo randomstring(); ?>
sql please create unique key
field , use on duplicate key
query insert/ update data
Comments
Post a Comment