phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PHP - Number: Generating Unique Identifiers

Tag: number, unique identifiers   Category: PHP Basic
post: 16 Mar 2008 read: 64


PHP Number Tips - Part 14: If you want to generate a unique, random numeric identifier that cannot be easily guessed, you can use a combination of PHP's uniqid( ), md5( ), and rand( ) functions:

<?php
// generate a random, unique ID
// result: "5542ec0a1928b99ef90cb87503094fe4" (example)
$id = md5(uniqid(rand(), true));
echo $id;
?>

PHP's uniqid( ) function returns an alphanumeric string based on the current time in microseconds and because the identifier is based in a time value, so there is a very slight possibility of two identical identifiers being generated at the same instant. There are to reduce this possibility and add a random element to the procedure by combining the call to uniqid( ) with a call to rand( ) and md5( ).




| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting