Files Ending in .php.html Unexpectedly Being Processed by the PHP Engine
Apache/2.2.3 (Red Hat), PHP 5.1.6 and phpDocumentor v1.4.3 weren’t getting along. phpDocumentor was generating HTML with filenames of the form script.php.html. Apache’s mod_php wanted to interpret script.php.html as a PHP script and was throwing errors about improper formatting:
PHP Parse error: syntax error, unexpected T_STRING in /var/www/default/docs/out/lib/lib/_php---obj---FFT.php.html on line 1, referer:...
I wanted script.php.html to be treated as an HTML file and not be processed by the php engine. This double-extension was causing me grief. The docs claimed that only files ending in the extension will be handled by the AddHandler command. I was incredulous of this claim, especially in light of Apache’s ability to do content negotiation with those .var language files.
Q: Why was this happening? I first guessed it may have been due to mod_mime, but quickly ruled that out. I then guessed it was the MultiView option… Maybe, this is one mechanism that governs content negotiation… The documentation claims:
# The index.html.var file (a type-map) is used to deliver content- # negotiated documents. The MultiViews Option can be used for the # same purpose, but it is much slower.
This statement lead me to suspect that my problem may actually be caused by an issue with the AddHandler option, since an AddHandler command must be executed to associate type-map with that .var extension.
Ah me, time is money, and in the end, I actually didn’t diagnose the problem, but I did discover (by trial and error) a workaround.
In file /etc/httpd/conf.d/php.conf, I surrounded
AddHandler php5-script .php AddType text/html .php
with
<Files ~ "\.php$"> AddHandler php5-script .php AddType text/html .php </Files>
Now, only files explicitly ending in “.php” will be treated as a php5-script. Take that!
PHP 5.1+ Date vs Strftime Timezone Annoyance
In the old PHP4 days, you could change the default timezone by: putenv("TZ=America/Los_Angeles");. In php5, you can still do this and it’ll work for strtotime, strftime, gmstftime (maybe more). It will not work for methods date and gmdate. For those, starting in version 5.1, you’ll need to call date_default_timezone_set("America/Los_Angeles");. This is just another one of those annoying things that make PHP a dying language.
Together, to be cross-version compliant:
putenv("TZ=America/Los_Angeles"); if( function_exists("date_default_timezone_set") ) { date_default_timezone_set("America/Los_Angeles"); }
We need both to be reverse-compatible with PHP4.
The little known PHP htmlspecialchars
Want to replace only xml entities <, >, &? Don’t use htmlentites or str_replace; use htmlspecialchars.
The only named entities for XML are &, > and <. For all others you need to use the Unicode character code (eg.  ).
Webkit: Entity ‘nbsp’ not defined – Convert HTML entities to XML
PHP 5.3.1 Error Logging and Display of Said Errors
I just installed the PHP 5.3.1 Windows installer bundle [VC9 x86 Non Thread Safe (2009-Nov-19 09:53:39)] on my Windows 7 [Ultimate] machine. I want the PHP error output to get sent to stderr when run from the command line. I don’t want the errors logged to a file because I’m developing cli tools. Since we’re dealing with the cli, I also don’t want html errors. Should be pretty easy… if you know how to edit the settings.
These worked for me:
error_reporting = E_ALL display_errors = stderr display_startup_errors = On log_errors = Off html_errors = Off ;error_log = php-errors.log
Ensure that error_log is not set, otherwise error will not be displayed. This problem may be related, but is obviously not, the date bug mentioned in the PHP bug reports. It may be fixed in the latest snapshot. As a note, the php.ini date.timezone setting is date.timezone = "Pacific/Honolulu", and I still had this problem.
Other things I noticed:
When error_log = syslog, you can find the PHP events in the windows Event Viewer -> Windows Logs -> Application. I mention it in case you want that type of thing… or think error that logging may not be working.
PHP/GD Bug: imageFilledEllipse Centered on Y=0
I was drawing circles using PHP’s GD function imageFilledEllipse. I noticed that when I went to draw a circle having its center at $y=0,there would be a line missing at $y=1.
Here’s the work-around:
if( -1 <= $py && $py <= 1 ) { // Undocumented PHP bug when Y coordinate is zero. imageFilledEllipse($img,$px,$py,$X,$X,$color); imageFilledRectangle($img,$px-$halfX,$py-1,$px+$halfX,$py+1,$color); } else { imageFilledEllipse($img,$px,$py,$X,$X,$color); }
PHP Version:
PHP 4.3.9 (cgi) (built: Apr 1 2009 10:41:42) Copyright (c) 1997-2004 The PHP Group Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies
$ yum search php-gd
php-gd.i386 4.3.9-3.26 installed Matched from: php-gd The php-gd package contains a dynamic shared object that will add support for using the gd graphics library to PHP.