Welcome Guest, Not a member yet? Register   Sign In
Does this small piece of code seem ok?
#12

@iamthwee
Yes, you are right the probabilities are astounding. But it can also happen on the first day, or every day for a year. Unlikely but it can. And knowing me, as soon as the first paying customer tries it, it will fail and the customer will think 'This is rubbish, it does not work.'.

@Narf
That is a fabulous idea. Why I didn't think of that I don't know. Basing the code on the time plus a random string is brilliant.
Also, the way you have done it is just fantastic. I have spent ages on php fiddle breaking it all out and trying to work out how it works.

Code:
$guest_code = bin2hex(pack('N', microtime(TRUE))).bin2hex(get_instance()->security->get_random_bytes(4));

Microtime is time with microseconds since epoch.
Micortime = 1446237483.42

Packing it with 'N' makes sure it is 32 bits long, or four bytes, or 4 characters.
bin2hex makes it all 'normal' characters and 8 in length, 2 characters per digit for hex encoding.
Joined with random 4 bytes, hex encoded to 8 in length, making 16 in total.

Simply Brilliant :-)

I also had great fun trying to work all that out.

Thanks everyone. I learned tons of stuff and that small bit of code that was bugging me I have finally put to rest :-)

Best wishes,

Paul.
Reply


Messages In This Thread
Does this small piece of code seem ok? - by PaulD - 10-30-2015, 09:02 AM
RE: Does this small piece of code seem ok? - by PaulD - 10-30-2015, 02:01 PM



Theme © iAndrew 2016 - Forum software by © MyBB