Thursday, January 10, 2013

Time me

Here's a quick tidbit to time something in PHP pretty accurately. Jotted down here for future posterity.




$mtime = explode(" ", microtime());
$starttime = $mtime[1] + $mtime[0];

// something worth timing...


$mtime = explode(" ", microtime());
$endtime = $mtime[1] + $mtime[0];
$totaltime = ($endtime - $starttime);


I didn't write it, and can't recall where I found it. But it works well. Enjoy.

Saturday, November 10, 2012

Content-Disposition

Want to download a file instead of rendering in a browser? Append "Content-Disposition:attachment;filename='foo.bar'" to the header. One may also specify "Content-Disposition:inline;filename='foo.bar'" to render the file as usual, yet provide for a default filename in the "Save As" prompt.

This is according to RFC2183 and RFC6266.

Friday, October 26, 2012

img oddities

Something to note today: Make sure to define img.onload before img.src in javascript. Otherwise certain browsers may behave erratically. Apparently there isn't an official spec on this, but it's mentioned over at StackOverflow.

Thursday, October 18, 2012

I see your tail

In Microsoft Windows 7 Professional one may use a MAK (Multiple Activation Key) to activate an install.  Now, if one loses a "normal" key it can be retrieved from the system using a myriad of key viewer programs. Something to note, however, is that as soon as activation completes using an MAK the key is deleted and so there is no way to retrieve it. But, if all you need to know is which key of several was used then the command slmgr –dli will show the last 5 characters of the MAK used to activate.

Monday, September 3, 2012

PHP 5.4 upload progress error

   I discovered something today after several days of baffling code output, obscure error messages, and altogether confusion on what could be the problem. It all started with simple file upload script that would, seemingly randomly, return a 502 response from nginx. A look into the nginx log showed [error] 18646#0: *1225 recv() failed (104: Connection reset by peer) while reading response header from upstream, ..."
which basically equates to it being PHP's my PHP script's fault. Apparently, PHP couldn't process the request, so it just refused it.

   The next logical step seemed to be to take a peak at the PHP log. Which helpfully read "PHP Warning:  Unknown: The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in Unknown on line 0". This warning was confusing to me since I wouldn't expect a "Warning" to cause a connection reset error as above. But I digress.

   Thinking it was a problem with my (very simple) session handler class, I combed through each line looking for anything that could cause an invalid session id. But everything session related had worked just fine until I tried to upload a file with an HTML form. Specifically, multiple files. Also, the problem had risen after some unfinished experimentation with PHP 5.4's "upload progress" feature. Scouring Google finally lead me to a clue from [nihaopaul at gmail dot com's] on php.net mentioning the importance of the field order, simply stating that "otherwise you wont get any updates". It turns out that the required session.upload_progress.name hidden field should probably MUST be BEFORE any file inputs or the file upload begins before the upload session can start.

So if you find yourself with this cryptic log warning, hopefully you've stumbled across this page and found the information helpful.


The mentioned software versions are Nginx 1.2.3 / PHP 5.4.5

Thursday, April 1, 2010