note 54192 added to function.uniqid

From: Date: Mon, 27 Jun 2005 06:42:10 +0000
Subject: note 54192 added to function.uniqid
Groups: php.notes 
Request: Send a blank email to php-notes+get-90989@lists.php.net to get a copy of this message
I use this method to create my unique_id's. I borrowed the getip() function from the getenv() user note by fr600 at hotmail dot com. This adds a best-guess ip address to the unique id and the option of a salt which could be generated using another uniqid call. While not perfect, it's a far jump better than uniqid alone. <?php // Set to null or empty string if you want define('UNIQUE_ID_SEPARATOR', ':'); function getip() { if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) $ip = getenv("HTTP_CLIENT_IP"); else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) $ip = getenv("HTTP_X_FORWARDED_FOR"); else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) $ip = getenv("REMOTE_ADDR"); else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) $ip = $_SERVER['REMOTE_ADDR']; else $ip = "unknown"; return($ip); } function unique_id($salt = null) { if (!defined('UNIQUE_ID_SEPARATOR')) { define('UNIQUE_ID_SEPARATOR', null); } if ($salt != null) { $salt = substr(md5($salt), mt_rand(0,21), 10); } return uniqid( getip() . UNIQUE_ID_SEPARATOR . $salt . UNIQUE_ID_SEPARATOR, true ); } echo unique_id( uniqid(rand(), true) ); ?> ---- Manual Page -- http://www.php.net/manual/en/function.uniqid.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54192 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54192&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54192&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54192&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54192&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54192&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54192&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54192&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#90989) next »