note 54192 deleted from function.uniqid by mazzanet
| From: | mazzanet@php.net | Date: | Mon, 27 Jun 2005 07:04:02 +0000 |
| Subject: | note 54192 deleted from function.uniqid by mazzanet | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-90990@lists.php.net to get a copy of this message | ||
Note Submitter: zabmilenko at hotmail dot com
----
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) );
?>