Unique random salt

I have the below function in a model and I want to add code to check that is unique and if not to generate another salt. What do i need to do?

private function randomSalt($length)


	{


        $chars = "abcdefghijkmnopqrstuvwxyz023456789";


        srand((double)microtime()*1000000);


        $i = 0;


        $salt = '' ;





        while ($i <= $length)


        {


            $num = rand() % 33;


            $tmp = substr($chars, $num, 1);


            $salt .= $tmp;


            $i++;


        }


        return $salt;


    }

You want to compare it to the database and see if you already have it?

Correct. If string is unique return true otherwise try again until function returns true



public function isUnique($key) {


 $condition = "key = :key";


 $params = array();


 $params['key'] = $key;


 $result = YourModel::model()->find($condition, $params);


 if ($result == null) {


   return true


 }


 return false;


}


Looks good !

I cant get it too work.

Could you please write a complete example. Thanks in advance

What does the error message say?

The function above should be in a Model and 'YourModel' should be replaced accordingly.