redstrike
redstrike

Reputation: 11

Generate a UNIQUE ID like Youtube's Playlist - PHP

I have read this tutorial many times, finally, i finished to create my own unique id like Youtube.

But i can't find out how to create this kind of unique ID:

http://www.youtube.com/playlist?list=PL124C2FA58C814231

Some examples:
E5DBC5AD85E952BF
F26D47E15E785137
4DE9165CE24633D0
26B44580D7ECDCD3
--- with prefixes PL --
PL3F16C0AE0309BB56
PL124C2FA58C814231

How can i create that kind of unique id using PHP? My database used auto incremental PRIMARY KEY.

Thanks.

Upvotes: 1

Views: 1471

Answers (2)

Michael Berkowski
Michael Berkowski

Reputation: 270677

PHP has a uniqid() function for this purpose. Pass the prefix to it as its first parameter:

$id = uniqid("PL");

// PL4e2b26588bec0

If this is needed for security purposes, there are better options discussed on the PHP document page.

Upvotes: 3

93196.93
93196.93

Reputation: 2781

Try this:

echo 'PL' . strtoupper(md5(time()));

Then obviously check your datasource to see if the value is unique.

Upvotes: 0

Related Questions