-2

I want to know what strategy should i use to generate a random URL for my users uploaded pictures. I dont think str_shuffle is a good option, because it consumes a lot of memory ( am i wrong )? Please give me some tips for this kind of situation.

Thanks.

  • You're looking for a bijective formula - https://github.com/wdalmut/php-bijective – Pedro Lobito May 11 '16 at 00:22
  • 1
    Possible duplicate of [How to create a random string using PHP?](http://stackoverflow.com/questions/853813/how-to-create-a-random-string-using-php) – Peter O. May 11 '16 at 00:41

1 Answers1

-1
$random = uniqid();
$url = "http://www.example.com/?v=$random";
echo $url;

The output will look something like: http://www.example.com/?v=4b3403665fea8

You would then save $random in a database for future use ;)

Webeng
  • 7,050
  • 4
  • 31
  • 59
  • not my dv, but I think you meant `uniqid()` and not `unique()`. – Funk Forty Niner May 11 '16 at 00:26
  • only when I'm 100% sure of my shot ;-) – Funk Forty Niner May 11 '16 at 00:28
  • *Hm....*, looking at the OP's question again, your answer won't work and I'll tell you why. They would need to have existing ids/images to match what `uniqid()` generates, so I think their best bet would be to use a database and run a random function from a SELECT. That's probably why you got the downvote. Read the question again. I had to remove my upvote, sorry (and that isn't my downvote you know). – Funk Forty Niner May 11 '16 at 01:19
  • @Fred-ii- Alright, so If I saved `$random` in a database, would that then be sufficient? I believe uniqid() is unique to the millisecond and will stay unique for many centuries to come, though I could be mistaken in my understanding. – Webeng May 11 '16 at 02:20