<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bogdan Lungu &#187; PHP</title>
	<atom:link href="http://www.bogdan-net.com/archives/category/php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.bogdan-net.com</link>
	<description>Internet and Web Development</description>
	<lastBuildDate>Sun, 06 Sep 2009 16:37:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Generate a unique string in PHP</title>
		<link>http://www.bogdan-net.com/archives/36</link>
		<comments>http://www.bogdan-net.com/archives/36#comments</comments>
		<pubDate>Wed, 12 Dec 2007 20:29:53 +0000</pubDate>
		<dc:creator>Lungu Bogdan</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.bogdan-net.com/archives/36</guid>
		<description><![CDATA[Here is an example of generating a unique string in PHP very useful in some cases especially when you work with large amounts of rows in a db table and you want unique and "hard to find" keys for every row.
My usual method to do this was using the  strtotime() function, with "now" as [...]]]></description>
			<content:encoded><![CDATA[<p>Here is an example of generating a unique string in PHP very useful in some cases especially when you work with large amounts of rows in a db table and you want unique and "hard to find" keys for every row.<br />
My usual method to do this was using the  strtotime() function, with "now" as the argument and to the result I was applying the md5() to get  a random unique id. The result is fine until you run the script more then 1 time in a second when you get the same string. Here is the new way I do to get a real unique ID not depending on time: </p>
<div class="igBar"><span id="lcode-2"><a href="#" onclick="javascript:showCodeTxt('code-2'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-2">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">function unique_id<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$better_token = md5<span style="color:#006600; font-weight:bold;">&#40;</span>uniqid<span style="color:#006600; font-weight:bold;">&#40;</span>rand<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, true<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$unique_code = substr<span style="color:#006600; font-weight:bold;">&#40;</span>$better_token, <span style="color:#800000;color:#800000;">16</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$uniqueid = $unique_code;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">return $uniqueid;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$id = unique_id<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">echo $id; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
This time I use the rand() and uniqid() functions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bogdan-net.com/archives/36/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
