0

I need to generate a unique string in PHP.

Currently I'm using a technique like this

$clipId = base_convert(microtime(), 8, 36);

However, as this is based on time, the ID changes when the page is re-rendered, and I need to to always remain the same.

If there would be a way to feed in the image URL and the post-title as strings to output an alphanumeric ID, that would be perfect, and 'random' enough for what I need to do here. Also if it were possible to get the unix-time the image was uploaded to Wordpress (together with the time the unix-time the post was created), I could use that.

Jack Wild
  • 2,072
  • 6
  • 27
  • 39
  • 4
    So in fact you don't want a **random** string. – tmt Apr 20 '15 at 15:06
  • you can use `$_SESSION` variables if you do not want to regenerate that id. – vaso123 Apr 20 '15 at 15:07
  • 1
    So, how about you explain the *real* problem you're facing? It obviously isn't creating a random string that isn't really random, nor does it do what random strings do. You're solving something, your potential solution is this oddly-non-random string. What is that something? – N.B. Apr 20 '15 at 15:08
  • 2
    I can't understand what you're asking. – sjagr Apr 20 '15 at 15:08
  • I am guessing you want to generate unique ID for each page? you could use something like `md5($_SERVER['REQUEST_URI'] . serialize($_REQUEST))` but its bad and leaky solution. as @N.B. please explain your real problem – Peter Apr 20 '15 at 15:12
  • I need to generate a unique identifier to use as a data-attribute which is then used to save pictures to a clipboard on the site. This will then be added as a class, and on load, a class will be added if the ID has been clipped. I know it's not actually true random... I need it to be unique... but it's really not a life-or-death situation, so if every 1,00,000th time it failed, that's totally fine, wouldn't really break anything! Quick and easy is more important here. – Jack Wild Apr 20 '15 at 15:19
  • @Peter Yes, similar to this. But for each image on each page. And it would need to be the same every time for every user who visits the site, on any computer in any session. – Jack Wild Apr 20 '15 at 15:20

1 Answers1

2

So, you want an algorithm that turns one string into another string. That's not random, that's either an encoding or a hash. An encoding expresses the same value merely in different terms, for example base64_encode. You can convert between the original string and the encoded form back and forth as often as you like.

Alternatively you probably want a hash like SHA1 or MD5 to turn arbitrary input into a fixed-length output. You can not convert a hash back into its original value.

Alternatively you can use an entirely arbitrary random string generated with a pseudo random number generator. These generators need to be seeded with an initial value, and will then return a predictable and repeatable series of seemingly random numbers. If you seed it with the same value, it will return you the same random number sequence. You can use that to produce random numbers which have no direct connection with your string yet are still reproducible when necessary. e.g. mt_rand, mt_srand.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • This is useful, thanks. I'll look more into this. So, is there a quick and easy way to encode / hash a string to a ~10 char alphanumeric string. It's nothing life or death here, so it really isn't a biggie if it's not 100% foolproof. It's more a proof of concept / prototype right now. – Jack Wild Apr 20 '15 at 15:22
  • `substr(sha1($string), 0, 10)` - The biggest issue is the likelihood of a *collision*, i.e. two inputs yielding the same output. The shorter you make the output, the higher the likelihood of a collision. In practice it's probably going to be fine, but if you're serious you need to run the probability math. – deceze Apr 20 '15 at 15:34
  • Thanks, that should be totally fine for what I need, I'll test it out. It won't actually break anything, and it's not going to be used much, it's just a prototype, and won't get that much usage. – Jack Wild Apr 20 '15 at 15:37