3

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?

1
  • 2
    use a hashing algorithm function like hash(),md5(),sha1() to generate the string Commented Jul 22, 2013 at 18:11

2 Answers 2

1

A lot of times, when I need a unique string I use this mysql query: select HIGH_PRIORITY UNIX_TIMESTAMP(); to get a unique 10 digit number like 1374517262 and then append or prepend characters.

Sign up to request clarification or add additional context in comments.

Comments

1

http://snipplr.com/view/5444/

This is a nice function that will generate random pronounceable passwords. This may help you get close to what you are trying to accomplish.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.