Results 1 to 2 of 2

Thread: Random Key for users

  1. #1
    Join Date
    May 2006
    Posts
    266
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Random Key for users

    Hi I am trying to give every user there own access key however some users are getting the same random numbers of other users

    Here is my code if someone can help that would be nice
    Code:
    //random number
    
    function generate_cust_ID()
    
    {
    
    srand ((double)microtime()*1000000000);
    
    $number=rand(1,9999999999);
    
    return $number;
    
    }
    
    
    
    if (!isset($cust_ID))
    
    {
    
    $cust_ID=generate_cust_ID($number);
    
    }
    
    
    $accesskey = $cust_ID;
    
    
    
    	$result = @mysql_query("SELECT usid FROM accesscode WHERE usid= '$usid'");
    
    	if(!$result || !mysql_num_rows($result))
    
        		{                  
    
    
    
    		$query2="INSERT INTO accesscode (usid,date,rand) VALUES ('$usid','$MyTimeStamp','$accesskey')";
    
                                     $results=mysql_query($query2) OR DIE("Your SQL: " . $query2 . "<br/> Produced this Error: " . mysql_error());
    
                               if (mysql_affected_rows() == 1) {
    
                                        echo "<center><font='black'>Access On Facebook<BR/>Your access code is:<br/></font>";
    
                                       }
    
    			$query = "SELECT * FROM accesscode WHERE usid = '$usid'";
    
                                          $result = mysql_query($query);
    
                                          $num = mysql_fetch_array($result);
    
    			$rand = $num[rand];
    
    			print $rand;
    			echo"<br/><a href='http://apps.new.facebook.com/' target='_blank'>Go To Application</a>";
    
     			echo "</center>";
    
    					
    
             
    
    		  } 
    
                 else {
    
                    
    
                                        $query = "SELECT * FROM accesscode WHERE usid = '$usid'";
    
                                          $result = mysql_query($query);
    
                                          $num = mysql_fetch_array($result);
    
    			$rand = $num[rand];
    
    			echo "<center><font='black'>Access On Facebook<BR/>Your access code is:<br/></font>";
    
    			print $rand;
    			echo"<br/><a href='http://apps.new.facebook.com/' target='_blank'>Go To Application</a>";
    
                       	echo "</center>";
    
    }
    The web in one word.

  2. #2
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    Even with a random number generator you will still sometimes get the same number.

    The first thing to do is to test the table to see if that ID has already been allocated, ie SELECT usid FROM accesscode WHERE usid= '$accesskey.

    If there are any records returned then generate another number until it is definitely unique.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •