PHP using gzip’d output buffers and sending HTTP content-length

Recently, while developing for the ActiveWAFL Framework, I came across a unique situation in PHP. I am explicitly using gzip’d output buffering using the following code:

if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
   ob_start("ob_gzhandler");
}

I am also using:

  • Smarty3, which uses ob_start() and related functions generously
  • custom php error and exception handlers

The problem arises when, while in the middle of output buffering, an error or exception occurs and thus calls my custom handlers.  My custom handlers try to display error information using Smarty3, and if that fails it prints text output.  In either case, I could not send the content-length header without getting the dreaded “headers already sent” error.

I have seen all sorts of suggested workarounds on php.net, such as this one: http://us2.php.net/manual/en/function.ob-get-length.php#59294 but most of them either don’t work at all or they disregard the possibility of numerous nested buffers, some gzipe’d and some not.  PHP’s ob_get_level came in handy for this purpose.  Here is the code I used.

//$content has been previously populated with some information, in my case, an error message
if (ob_get_level() > 0)
{
   print $content; //buffering is on, so nothing is actually sent to the browser yet
   $bufferContents="";
   $bufferSize=0;
   while (ob_get_level() > 0)
   {
      $bufferSize += ob_get_length();
      $bufferContents .= ob_get_clean();
   }
   header("Content-Length: $bufferSize");
   print $bufferContents;
} else {
   $filesize = strlen($content); //don't try to use mb_ functions or iconv or anything weird. We want the number of bytes, which strlen gives us
   header("Content-Length: $filesize");
   print $content;
}

phpMyAdmin SQL Statement error import.php: Missing parameter: import_type, import.php: Missing parameter: format

This problem and it’s solutions have been well documented.  Unfortunately, none of those solutions worked for me.

Problem / Possible Symptoms

  • Search doesn’t work or returns no results
  • SQL statements and imports produce an error that says something like “import.php: Missing parameter: import_type,  import.php: Missing parameter: format”
  • In framed mode, the “main” frame gets confused and frames open in their own tabs

Solutions

  • Most of the known solutions are here: http://wiki.phpmyadmin.net/pma/FAQ_2.8
  • My solution: do not use subdomain “www” in browser when accessing phpMyAdmin (instead of http://www.dlcware.com/phpMyAdmin/ I used http://dlcware.com/phpMyAdmin)

This might be a no-brainer to some of you but it had me stumped forever.  Hopefully this can help someone as ignorant as me.

 

SAP Crystal Reports Install Error 1066: could not access network location \ASP.NET Client files

I Googled around and read several solutions.  The answer for me was found at: http://jbickford.com/2011/02/16/crystal-reports-runtime-64-bit-error-1606/

I had already thought to run with extended priveleges.  However the right-click menu on the msi file did not have a “run as administrator” option. 

The solution was to run a shell (cmd.exe) as an administrator and then execute the msi from the command line.

Thanks to jbickford.

Enable SSH / SFTP option for WordPress Upgrades and Plugin Installs

If you have a server that does not support FTP and want to use the WordPress Auto-Update features you’ll need to do a few extra things to get it all working.

Out-of-the-box WordPress offers something called “FTPS”.  I have no idea what FTPS is.  But my brain, expecting to see “SFTP” went temporarily dyslexic and chose to see what it wanted to see, “SFTP”.

I tried to append the port number 22 to the url (because wordpress complained it couldn’t connect to FTPS on port 21).  That didn’t work.  And I thought to myself “why in the hell is WordPress trying to do SSH over port 21!?” without realizing it was in fact using FTPS.

Once I finally realized that it said “FTPS” and not “SFTP” I could finally start Googling for the correct solution.

It turns out that WordPress does indeed offer a SSH/SFTP option.  However, in order for it to be enabled, you need to install the SSH2 PECL extension for PHP.

Here is a step-by-step guide for CentOS6 running Apache 2 and PHP 5.3

  1. # yum install gcc php-devel php-pear libssh2 libssh2-devel
  2. # pecl install ssh2 channel://pecl.php.net/ssh2-0.11.3
  3. When it asks for the ssh2 prefix, accept the default (by pressing <enter>)
  4. # touch /etc/php.d/ssh2.ini
  5. # echo extension=ssh2.so > /etc/php.d/ssh2.ini
  6. # service httpd restart
  7. Now when you try to update wordpress or install a plugin, you’ll see a new option for SSH2

Note: If you’re not using CentOS or Yum you can achieve the same with your package manager or by downloading libssh2 and compiling it on your machine.  (I’m not sure what the libssh2-devel has that lbssh2 does not have.  But I do know that you need the -devel version for the PECL extension to work.).  Once you have libssh2, you just instal the PECL extension.

I have never been able to use the WordPress auto-update and install features before as all of my servers have a strict no-ftp rule.  Now that I am finally able to use it, I cannot believe I waited so long to figure this out.  It saves so much time and hassle; I should have fixed it long ago.  Hopefully it’ll help you.

 

References:

  • http://blog.dynamichosting.biz/2011/01/10/installing-ssh2-extension-for-php-on-centos-5/
  • php.net/manual/en/book.ssh2.php
  • http://stackoverflow.com/questions/561024/install-pecl-ssh2-extension-for-php

AZ PHP Usergroup February 2012 – Object Oriented PHP – An Acedemic Discussion

I had the privilege of speaking at the February 2012 edition of the Arizona PHP Usergroup at Walt’s TV.  I decided to discuss Object Oriented Development in general, as well as relative to PHP.

I have spoken in public many times.  But I have never had the opportunity to review my performance on video.  This time, I had that privelege horror.  I had three major issues with the discussion

  1. I speak too fast
  2. I make several inaccurate statements
  3. I am unnecessarily over-dramatic at time

I tried to edit the video to trim out the fat.  I think I succeeded in that.  However, some splices are choppy and/or otherwise sloppy.

In addition to my shortcomings, the recording is not very good at all.  I chose to present with my back to the camera.  Enjoy looking at my bald head for an hour.

Anyways, aside from all of that, I think there is some useful information in here for the PHP developer.  We talk a lot about design patterns, OO, and OO for PHP.

I tried to annotate mistakes.  Feel free to add further corrections in the comments.  I know I noticed at least one or two additional mistakes that I failed to annotate.

Without further ado:

http://www.dlcware.com/Resources/Videos/OOPhpPresMed.wmv

Sorry I couldn’t embed it with YouTube or something.

www.youtube.com/watch?v=id91EWRyCeo

My First AZ Php Usergroup Conference

I just had a blast meeting with the AZ PHP group at one of their monthly conferences.  I felt comfortable and welcome, despite telling at least two terrible jokes, and feel like I have a new information resource at my disposal.  I encourage any PHP developers in the Phx area to check it out.  They have meetings monthly at Walt’s TV in Tempe.  AzPhp.org

MAY UPDATE: AZ PHP Meetings are now held at the Microsoft Store at Scottsdale Fashion Square