Create a unique ID
Created: Last updated:
Ever since I wrote an article about how to get a unique ID in Apache I have traffic from Google with questions about how to get or create a unique ID in PHP or the Zend Framework and even Wordpress, Drupal or Joomla.
If you need a unique ID in your web application written with PHP, which includes my specialty the Zend Framework, or other frameworks like CodeIgniter, Drupal, Yii or CMS systems like Wordpress and Joomla, then you have several choices.
First we need to find an answer to the question about how unique this ID has to be. I guess it all depends on the requirement for this ID.
You may need a unique ID for a log file to identify each single web request, i.e. you like to see all log events resulting from the same request. If you have Apache you are lucky because your Apache web server can provide you with an ID for each single web request. How you can use this can be found in my other document I mentioned: Unique ID per request.
If you need a unique ID to identify more than just one single web request you could still use this unique ID from the Apache module. Catch it with the first request and keep using it.
PHP function: uniqid
If that previous method does not work for you because you maybe use Microsoft's IIS you have another option which is a PHP function named uniqid. See the PHP documentation uniqid for how to use it.
Because we have this PHP function there is in fact no need to handle or find it in your Zend Framework. And actually WordPress, Joomla, Drupal, Cake or any other framework.
Apache's module way should also work for other web languages like Perl, ASP, Ruby or whatever language you are using.