I want to create a less than or equal to 10 character unique string for an input string which could be a url
http://stackoverflow.com/questions/ask
OR an alpha numeric string
programming124
but the result should be unique for every input...Is their any function or class that you use for your projects in php... Thanks...
If you want a unique and random string, you can use the following function to create a random string:
function randString($length) {
$charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$str = '';
while ($length-- > 0) {
$str .= $charset[rand() % 62];
}
return $str;
}
After you have generated a new string, look up your database if that string already exists. If so, repeat that step until you’ve generated a unique string. Then store that new string in the database:
do {
$randString = randString(10);
// look up your database if $randString already exists and store the result in $exists
} while ($exists);
// store new random string in database
The simplest function available in php is uniqid. It is a little longer that you had mentioned, and wont work well with load balancing, but if you are doing something super simple, it should work alright.
Related
I just thinking about if it can be possible to search for encrypted fields in database what is starts with or include some characters or string.
Example:
Encrypted text in db: "7724bd4ae7cba2c8d182980c7889258b07be344a9c8892de3c303ae03677771c"
Decrypted text: "Jackey"
Search for all encrypted fields in db what is starts with or include: "Jac"
Using sodium to encrypt / decrypt values with a fixed nonce (using in the example)
<?php
define("VALUE_ENCRYPTION_KEY", hex2bin("01abff4e1bbb9104e8e053bcc0492ad114ee7cbdc8597e4e5296e86c44a66bf0"));
define("VALUE_ENCRYPTION_NONCE", hex2bin('c1126da4358e7e4173f2ccc621dd1801a5949ae9f1896e43'));
define("VALUE_ENCRYPTION_BLOCK_SIZE", 16);
function EncryptValue($value)
{
if (!empty($value)) {
$padded_value = sodium_pad($value, VALUE_ENCRYPTION_BLOCK_SIZE);
$encrypted_value = sodium_crypto_secretbox($padded_value, VALUE_ENCRYPTION_NONCE, VALUE_ENCRYPTION_KEY);
return bin2hex($encrypted_value);
} else {
return null;
}
}
function DecryptValue($value)
{
if (!empty($value)) {
$decrypted_padded_value = sodium_crypto_secretbox_open(hex2bin($value), VALUE_ENCRYPTION_NONCE, VALUE_ENCRYPTION_KEY);
$decrypted_value = sodium_unpad($decrypted_padded_value, VALUE_ENCRYPTION_BLOCK_SIZE);
return $decrypted_value;
} else {
return null;
}
}
If you fetched all values, decrypted them, and filtered them afterwards: no problem, go for it.
If you want to outload all this to the database: forget it. One of the most important principles of proper encryption is to avoid especially that setting: if I now some part of the plaintext, encrypt it and check for other parts in the database that contain it / start with it. Just try to encrypt a string char by char to see how the encrypted string changes not only in tiny bits, but completely.
But you could try the following: if you always want to search for a prefix of a given, constant length (as in: always the first three, four chars of a string), encode that seperately or hash it, and store it in a seperate column. That would make it possible to use this column for filtering
I am creating short 5 character hashes to make unique classrooms for my students... a typical hash will look like AJ678.
I am generating the hash like this:
public function generateToken($length = 5)
{
return strtoupper(substr(md5(rand()), 0, $length));
}
I am expecting many thousands of classrooms to be generated over the lifetime of the app... so eventually there will be a clash... I want to know how to make sure that every hash will be unique.
I have make the token field a unique field.
I would think that the best way to do this would be to generate the hash, then check if the hash already exists in my database, if it does then generate a new hash, else use the hash.
Is that the correct way to go about this?
EDIT: I am a bit insecure about implementing a function that calls itself... does this look right?
public function generateToken($length = 5)
{
$token = strtoupper(substr(md5(rand()), 0, $length));
if ($this->tokenExistsAlready($token)) {
$this->generateToken();
} else {
return $token;
}
}
public function tokenExistsAlready()
{
$this->db->where('token', $token);
$query = $this->db->get('classes');
if ($query->num_rows() > 0) {
return true;
} else {
return false;
}
}
First, define "unique". Unique in our terms is a string of any length, that does not yet exist in your database.
This pretty much answers your question. You can never be sure, that your string is unique, unless you check it against your database. The longer the string, the slimmer the chance. So in your case, I would have created a while loop checking the database. Starting with the second string you save in the database, you might (and probably will later down the timeline) hit two randomly generated strings in a row. So checking the uniqueness in a loop until you find the "unique" one is a good idea. Something abstract like this:
$token = generateToken();
while(tokenExists($token))
{
$token = generateToken();
}
Keep in mind, that nothing guarantees true uniqueness of a string. You may use the MySQL UUID() or UUID_SHORT(), PHP uniqid() or anything else, that generates a random string. But it still does not guarantee the said uniqueness unless you check it against the existing database.
use uniqid() function
public function generateToken($length = 5)
{
return strtoupper(substr(uniqid(md5(rand()), 0, $length)));
}
Can the token be simply a unique integer? That would be easy to generate with a table with a single, AUTO_INCREMENT, column.
This question already has answers here:
How to generate a random, unique, alphanumeric string?
(31 answers)
Closed 7 years ago.
I am new to laravel 5. I am working on a project where I want to assign some random-readable unique string to each application. I have knowledge of the each application id which may be use as a seed. Since the app is going to be use within the company I don't worry much about security. I expect the table size to grow so my goal is to achieve uniqueness as much as possible because the field in DB is unique. A code like (EN1A20, EN12ZOV etc). If the function can allow me to pass the length of the string I want to return, that would be really awesome.
Edit
Shown below is my attempt to the problem
private function generate_app_code($application_id) {
$token = $this->getToken(6, $application_id);
$code = 'EN'. $token . substr(strftime("%Y", time()),2);
return $code;
}
private function getToken($length, $seed){
$token = "";
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$codeAlphabet.= "0123456789";
mt_srand($seed); // Call once. Good since $application_id is unique.
for($i=0;$i<$length;$i++){
$token .= $codeAlphabet[mt_rand(0,strlen($codeAlphabet)-1)];
}
return $token;
}
Can the code above do the trick?
Edit
Actually I borrowed ideas from this post PHP: How to generate a random, unique, alphanumeric string? to come out with the methods above but the post does not entirely address my issues. My goal is to generate a string of length say 6 to 8 (Alphanumeric and readable). This string would be use by my admin for query purposes. In my function I have mt_srand($seed) to seed the random number generator where seed is my application_id. It is possible to get duplicate $token.
Appreciate help.
You can use :
sha1(time())
Explanation: sha1 is hash function, and most important characteristic of hash function is that they never produce the same hash of different string, so as time() is always unique in theory sha1(time()) will always give you unique string with fixed width.
EDITED:
You can use you function but before giving token you can connect to database and check if token exists, if exists generate new token, if not exists give hin this token. This mechanism will give you unique tokens.
With your attempt to the problem you could apply the following to ensure a unique code:
do
{
$token = $this->getToken(6, $application_id);
$code = 'EN'. $token . substr(strftime("%Y", time()),2);
$user_code = User::where('user_code', $code)->get();
}
while(!empty($user_code));
Edit
To avoid an infinite loop in laravel, use
do
{
$token = $this->getToken(6, $application_id);
$code = 'EN'. $token . substr(strftime("%Y", time()),2);
$user_code = User::where('user_code', $code)->get();
}
while(!$user_code->isEmpty());
http://laravel.com/api/5.0/Illuminate/Support/Collection.html#method_isEmpty
or go with
do
{
$token = $this->getToken(6, $application_id);
$code = 'EN'. $token . substr(strftime("%Y", time()),2);
$user_code = User::where('user_code', $code)->first();
}
while(!empty($user_code));
Instead of get(), use first(). $user_code is probably unique so we can conveniently pull out the first result.
You could use the built in helper function:
str_random(int);
The documentation can be found: Laravel 5.1 Docs
To ensure it is unique you could always check that the name doesn't already exist and if it does rerun the function to generate a new string.
Hope that helps.
say if I wanted to give every user that registered on my site a unique id. It seems to me that if I wanted to do this I would have to: Create a random number for the id, check to see if that id already exists in the database, if it does exist then create another random number and send yet another query to see if that exists, and so on...
This could go on for ages. Apart from having an incrementing id, is there any decent way to do this?
The best way to do this is via the auto increment function, if you really don't want to use a function like so you could use uniqid();
Basically you it generates an unique id based on milliseconds, if you put in a kinda unique prefix in the function it will generate a very unique id.
echo uniqid('prefix');
This way you won't have to check after generating an id, if it already exists or not. You can be sure it is unique.
For more information check this url http://php.net/uniqid!
First of all, I agree with the comments. It's all overhead code, and if you're using it to make it look interesting you should really reconsider your priorities.
But, if you still need it; here's a little something:
function uid() {
mt_srand((double)microtime()*1000000);
$token = mt_rand(1, mt_getrandmax());
$uid = uniqid(md5($token), true);
if($uid != false && $uid != '' && $uid != NULL) {
$out = sha1($uid);
return $out;
} else {
return false;
}
}
Basically, it does a lot of random number generating to create a token for uniqueid, and then is sha's that. Probably overhead, but you can be sure that you never generate a double uid.
Fabian.
You can use the rand() function. It will generate a random number between two.
rand(0000,9999)
It will generate a number between 0 and 9999.
To check if it already exist:
$id = rand(0000,9999);
/* CREATE YOUR MYSQL CONNECTION */
$user_list = mysql_query("SELECT * FROM users");
while ($user = mysql_fetch_array($user_list))
{
if ($id == $user['id'])
{
echo('Already exist.');
}
else
{
/* YOUR CODE */
}
}
It's the way I did it...
If you have a string of 15 numbers you are looking at up to 999 trillion, I doubt it will run for "ages" considering there's almost 7 billion people on the planet.
Does the ID need to be numeric? By switching to alphabetic characters you will get a lot more entropy. A 6 digit number is 1,000,000 posibilities, a 6 character alphanumeric string is 2,176,782,336 possibilities. Make it mixed case alphanumeric and that jumps to 15,625,000,000.
Here's how I usually generate unique strings that are as short as possible:
$chars = 'abcdefghijklmnopqrstuvwrxyzABCDEFGHIJKLMNOPQRSTUVWRXYZ0123456789';
mt_srand((double)microtime()*1000000);
$id = '';
do {
$id .= $chars[mt_rand(0, strlen($chars) - 1)];
} while (isIdTaken($id));
var_dump($id);
You have to create a lot of items with this style of id, before you'll get to more than 3 or 4 characters.
I know it's late for this answer but the easiest solution is to generate random number and sure it will be unique 100% is
$uid = uniqid().date("Ymdhhis");
I am building a gallery in WordPress and I'm trying to grab a specific part of my URL to echo into the id of a div.
This is my URL:
http://www.url.com/gallery/truck-gallery-1
I want to isolate the id of the gallery which will always be a number(in this case its 1). Then I would like to have a way to print it somewhere, maybe in the form of a function.
You should better use $_SERVER['REQUEST_URI']. Since it is the last string in your URL, you can use the following function:
function getIdFromUrl($url) {
return str_replace('/', '', array_pop(explode('-', $url)));
}
#Kristian 's solution will only return numbers from 0-9, but this function will return the id with any length given, as long as your ID is separated with a - sign and the last element.
So, when you call
echo getIdFromUrl($_SERVER['REQUEST_URI']);
it will echo, in your case, 1.
If the ID will not always be the same number of digits (if you have any ID's greater than 9) then you'll need something robust like preg_match() or using string functions to trim off everything prior to the last "-" character. I would probably do:
<?php
$parts = parse_url($_SERVER['REQUEST_URI']);
if (preg_match("/truck-gallery-(\d+)/", $parts['path'], $match)) {
$id = $match[1];
} else {
// no ID found! Error handling or recovery here.
}
?>
Use the $_SERVER['REQUEST_URI'] variable to get the path (Note that this is not the same as the host variable, which returns something like http://www.yoursite.com).
Then break that up into a string and return the final character.
$path = $_SERVER['REQUEST_URI'];
$ID = $path[strlen($path)-1];
Of course you can do other types of string manipulation to get the final character of a string. But this works.