PHP Time Based UUID Function (GUID)
In my desire to find the “right” UUID function to use in my application, I grew to like the MySQL UUID() results produced because they are partially time based. But I didn’t like that I have to make a database call every time I want a new UUID. I didn’t find any similar functionality in PHP or from user based PHP functions, so here’s my efforts.
It clocks in at 100,000 results in about 1.36 seconds on my 2.8Ghz Intel iMac.
function uuid()
{
// Time based PHP Unique ID
$uid = uniqid(NULL, TRUE);
// Random SHA1 hash
$rawid = strtoupper(sha1(uniqid(rand(), true)));
// Produce the results
$result = substr($uid, 6, 8);
$result .= substr($uid, 0, 4);
$result .= substr(sha1(substr($uid, 3, 3)), 0, 4);
$result .= substr(sha1(substr(time(), 3, 4)), 0, 4);
$result .= strtolower(substr($rawid, 10, 12));
// Return the result
return $result;
}
We produce results looking like this:
1f8fec61-4a6f-9bd5-7f8e-e12325c464c3
1f8fed49-4a6f-9bd5-7f8e-d665dedc0297
1f8fee17-4a6f-9bd5-7f8e-29d1ce3e9e30
1f8feef2-4a6f-9bd5-7f8e-8ce03bebdbdf
1f8fefc7-4a6f-9bd5-7f8e-c22fbedb5390
Converting Ticks to a Timestamp
I recently had to deal with a SQLite database that stored the timestamps as ticks. Having to first research what a tick is, I created a simple function that will convert a tick value to a timestamp or MySQL datetime value since I couldn’t find anyone else who had done such a thing yet.
You can download it here.
The only tricky part (aside from figuring out what a tick is) was calculating the number of ticks between 0001-01-01 to 1970-01-01. Also of note: a tick is apparently a popular way to store date/time values for Microsoft.
Google Apps only gives 2GB to Safari users?
I recently had a small need to sign up two new Google Apps accounts and setup calendar sharing between them. So I naturally open up Safari as my second browser of choice and go to the sign-up page, only to notice that rather than the standard 7.2GB of space, I only get 2.7GB when signing up with Safari. Not willing to temp fate, I closed Safari and signed up through Firefox. Interesting!
Bug with Adobe Flash on Mac when uploading files to a redirected URL
Just spent almost the entire day trying to figure out what I was doing wrong when trying out many different flash multi-file uploading widgets. All of them would give me a 302 redirect http error, even when testing locally.
So after much searching and reading and studying… and finally testing this on my Windows machine (which works fine); I’ve decided this is a bug in Flash 9 for Mac.
There’s not a problem when it’s uploaded to a script directly which returns a 200 OK header response first. But when you use a framework that uses mod_rewrite for every URL, it’s not easy to get around that.
If anyone else has had experience with this I’d love to hear from you. Very frustrating! Maybe it’s fixed in the Flash 10 beta? Maybe it’s not a bug; it’s a “feature”, right?
UPDATE: I went ahead and installed Flash10 hoping for the best, but with no luck. I was able to contact someone at Adobe who told me the Flash scripts will have to be recompiled in Flash10 to see if Flash10 would help. So installing Flash10 on my computer won’t make the scripts any different.
Redirect all but used subdomains to primary domain using mod_rewrite and CodeIgniter
I was recently playing with mod_rewrite and wanting a couple of subdomains to act as subdomains, but all others to redirect back to my site without the leading “www”. For example:
www.example.com => example.com
bad.example.com => example.com
api.example.com => api.example.com
Not being a regular expression or mod_rewrite expert (or any resemblance of such), this was a bit of a challenge. But I did it with the help of a mod_rewrite and regular expression cheat sheet, as well as the excellent Apache URL Rewriting Guide.
So here it is for anyone else that could benefit. Just replace the bold parts with your own subdomains (and example with your domain).
Redirect without leading www:
# Redirect deleting leading www to root domain if no specified subdomain is used
RewriteCond %{HTTP_HOST} !^(subdomain1|subdomain2)\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
Redirect with leading www:
# Redirect adding leading www to root domain if no subdomain is specified
RewriteCond %{HTTP_HOST} !^(subdomain1|subdomain2|www)\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
If you’re using CodeIgniter and mod_rewrite for pretty URLs, you’ll need to be sure to add these subdomains to the list of directories that can be accessed directly.
# CodeIgniter writing
RewriteCond $1 !^(index\.php|images|robots\.txt|css|user_files|subdomain1|subdomain2)
RewriteRule ^(.*)$ /index.php/$1 [L]



