I have the following code that lets a user register his/her email on a website with a password, and returns them a unique pin.
<?
if($_POST['srSubmit'] && $_POST['srEmail'] && $_POST['srPass']) {
$conn = mysql_connect('localhost','','');
mysql_select_db("db_test",$conn);
while(1) {
$pin = rand(111111,999999);
$sel = mysql_query("SELECT * FROM formusers WHERE pin = '$pin'");
if(mysql_num_rows($sel) != 0) { continue; }
mysql_query("INSERT INTO formusers(email,password,pin) VALUES('".$_POST['srEmail']."','".$_POST['srPass']."','".$pin."')");
if(mysql_affected_rows()!=-1) {
echo "Pin:" . $pin;
exit;
} else {
echo "Existing email, try again<br />";
}
break;
}
}
?>
<form method="POST" action="">
<input type="email" name="srEmail" value="" placeholder="Email" /><br />
<input type="password" name="srPass" value="" placeholder="Password" /><br />
<input type="submit" name="srSubmit" value="Register" />
</form>
I want to associate a unique URL with each user (server.com/[unique-9-digit-alphanumeric-code], such that I can create a relatively secure admin page that users can access without necessarily needing to know their password.
Is there an easy way to do this with a PHP function (it doesn't have to be 9 digits, just long and random), or do I need to generate my own random alpha-numeric string and then post that to the database? How would you suggest coding this?