Checking Unique Code Generated using My PHP Script

69 views
Skip to first unread message

Pavan PHP

unread,
Nov 8, 2012, 10:52:46 PM11/8/12
to php...@googlegroups.com
I have written a php program to create unique alphanumeric strings of 8 alphabets.

To test and make sure that my program creates unique values always, I have written another php program to write all these unique alphanumeric string (5 million) in a seperate line by creating a text file and it worked.

Since file size of the text file is so huge that I can not open the text file in text editor because of RAM memory limitation so I am looking for any tool to open the text file to check whether these codes are really unique or duplicate.

I request you to help me in this regard.

Regards
Pavan PHP

Amit Patil

unread,
Nov 9, 2012, 2:15:08 AM11/9/12
to php...@googlegroups.com
If you are fine increasing the 8 digit limit then timestamp can be used as a unique number string or may be u can md5(time()), this will also create a unique value everytime but the number of characters will be 32 i guess

Thanks and Regards,
Amit Patil
http://www.amitpatil.me






Pavan PHP

--
On Twitter ==> http://twitter.com/phpcamp
On Facebook ==> http://www.facebook.com/campersfanpage
 
 
 
You received this message because you are subscribed to the Google
Groups "phpcamp" group.
To post to this group, send email to php...@googlegroups.com

Pavan PHP

unread,
Nov 9, 2012, 4:06:03 AM11/9/12
to php...@googlegroups.com
Hi Amit,

As per your suggestion, I have written following code:

for($i=0; $i<=100; $i++)
{
    echo md5(time());
    echo '<br>';
}

and got the following output:

6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
6bdddbdb60a7c44e6c14841f5cefc780
which means If my program is creating two uniquecode for two seperate user at the same time, these two seperate code for both user will be the same (duplicate) and not unique.

Also I have to limit my code to 8 characters as per the requirement so I created and simple algo and it does that. I got 50 million codes. Only the problems is that I want to make sure and confirm those code are really really unique but because of RAM limitation, I can not open the file in text editor. Instead I must write another program to check the uniqueness of all the codes but It does not look practical since If I need to write another program, to check the uniqueness, again I need to load all the code in ram first, so in that case also there will be limitation of ram.

So finally, it seems that is there any tool to check the uniqueness of the code of the file size more than 500 MB where all the unique code is stored in seperate line.

Regards
Pavan PHP

Amit Patil

unread,
Nov 9, 2012, 6:16:11 AM11/9/12
to php...@googlegroups.com

You need to make slight change in the code, because all the iteration are performing in a time less than single second so all the MD5 strings are same.

for($i=0; $i<=30; $i++)
{
    sleep(1);
    echo md5(time());
    echo '<br>';
}

As you said if 2 users resister at same time within same second then this will create 2 same number, chances are rare that this condition will satisfy that 2 users will register at the same time but still its good to not take a chance . there is another solution

use time() + mysql_insert_id()

mysql_insert_id() will return diff id each time so even if 2 users resister at same time the generated id will be slight different

This will give you 10 digit number so u have to use only last 8 digits, think on it if there are any chances of duplication ??



Thanks and Regards,
Amit Patil
http://www.amitpatil.me





Raxit Sheth

unread,
Nov 9, 2012, 6:59:12 AM11/9/12
to php...@googlegroups.com
If we assume each alphabet = 2 byte, your file size should be in range of 80-85MB <approx>
If we assume each alphabet = 1 byte, your file size should be in range of 40-45MB <approx>

Why you are storing in file ? why not in some temp db?

normally using c/php any prog, you should able to open+display 100 MB text file data.  <if your editors are failing>


Raxit

On Fri, Nov 9, 2012 at 9:22 AM, Pavan PHP <pava...@gmail.com> wrote:

Pavan PHP

--

Hari K T

unread,
Nov 9, 2012, 8:13:58 AM11/9/12
to php...@googlegroups.com
I am not sure whether this will work out .

But what about saving it in different files?

Say your count exceeds 500 start with file1, file2 etc .
On Fri, Nov 9, 2012 at 9:22 AM, Pavan PHP <pava...@gmail.com> wrote:

Pavan PHP

--

Anshu Prateek

unread,
Nov 9, 2012, 10:16:53 AM11/9/12
to php...@googlegroups.com

Cat yourfile | sort|uniq -c |cut -f 1 -d ' ' | uniq -c should give only 1 in right hand side. Then all your codes are unique. Not sure about memory implications here though.
Sent from mobile.

Pavan PHP

unread,
Nov 9, 2012, 10:51:38 AM11/9/12
to php...@googlegroups.com
Hi Anshu,

I did not understood the procedure you told. Can you explain little bit more.

I am not sure but may be you are giving me a linux command. I use windows 7.

Regards
Pavan

Hari K T

unread,
Nov 9, 2012, 11:05:49 AM11/9/12
to php...@googlegroups.com
Yes @Pavan that is *nix terminal  which make use of pipes.

Nice one Anshu.
Reply all
Reply to author
Forward
0 new messages