Wednesday, April 01, 2009

PHP error_reporting Code/Cypher

When developing code, a web site, in PHP it's handy to turn on some of the error_reporting functionality during debugging. To determin the correct code(s) to use try this simple Google Docs spreadsheet.

The principle is best explained in the PHP manual (available online and only a Google away), but this may help you understand what's being enabled and disabled and with the code required.

Examples (Lifted from the manual for your understanding):
// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors
error_reporting(E_ALL);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

?>
PHP is one of the most powerful languages out there for web development, frankly there's not much it can't do when you add the right classes (which is like a DLL or Component on the ASP side) but the advantage is the cost... ZERO. If you want a development environment for your own personal machine, try out VirtualBox or VMWare Server and build one (Ubuntu or Ubuntu Server are great platforms, though OpenBSD, Debian, Fedora and the like are also popular and just as worthy). VMWare's VMTN has ready-to-use VMs you can simply plug in and fire up. I might pre-fab a couple of installations for VirtualBox over the next little while, no promises but if you're stuck let me know and I'll do what I can.

Thursday, July 03, 2008

TiddlyWiki - a reusable non-linear personal web notebook

This is NOT my technical trick, but definately worth a gander. Excellent work by Jeremy and the TiddlyWiki contributors!

TiddlyWiki - a reusable non-linear personal web notebook: "TiddlyWiki, a popular free MicroContent WikiWikiWeb created by JeremyRuston and a busy Community of independent developers. It's written in HTML, CSS and JavaScript to run on any modern browser without needing any ServerSide logic. It allows anyone to create personal SelfContained hypertext documents that can be posted to a WebServer, sent by email or kept on a USB thumb drive to make a WikiOnAStick."

Wednesday, March 19, 2008

Simplifying Rollouts: Server-Sensitive Code

I maintain a Linux/Apache/MySQL/PHP site as part of my role and keeping the sites sync and problem free between Development, Stage, and Production can be a little tricky if you've got things to worry about like outbound e-mails, scheduled jobs, and credit card clearing that you really do NOT want going awry when it hits production.

There are many method of detecting which manner of operation your various sites have, the simplest of which is a variable that states the mode in a common file. This one file can become a hazard because you need to remember not to sync that file to the production server.

I was looking for a manner by which to determine which server the code was executing on with a high-degree of reliability and this seems to work. I created the function called 'serverIPAddress()' which returns the obvious, but it's the how that is interesting. I must say, I am unsure whether this will work 100% for all of you as-is, as there many be slight differences in the output of the Linux command 'ifconfig' that need to be accounted for. In my implementation there are but two adapters, local (lo) and Ethernet (eth0), eth0 is listed first and I'm not sure this can change but I'll accept that it could. So, that said... the output of ifconfig looks like this:
eth0 Link encap:Ethernet HWaddr 00:0C:29:41:18:C4
inet addr:192.168.1.3 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe41:18c4/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:26995 errors:0 dropped:0 overruns:0 frame:0
TX packets:20748 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3324252 (3.1 MiB) TX bytes:6947293 (6.6 MiB)
Interrupt:177 Base address:0x1400

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
The code to extract the 'inet addr' from this response looks like this:
function serverIpAddress(){
exec("/sbin/ifconfig",$ifconfig);
$target=explode(" ",$ifconfig[1]);
$bustoutip=explode(":",$target[5]);
$serverIP=$bustoutip[1];
return $serverIP;
}
The path for ifconfig is explicit due my use of this in a host of scheduled tasks (cron jobs) on the server. I'll be apply in this to my web site as well as the method for detection is based on 'SERVER_NAME' and a little less reliable/safe in my books.

Once I have the IP Address I can move on to making server specific code that understands what to do dependant upon the server that it is executing on. The following code takes the resulting IP Address and sets variables, etc. to ensure that code transitioned from development to stage/production can understand and behave reliably. There's nothing worse during service recovery when there are code differences, especially if the code differences are not well documented.
switch(serverIPAddress()){
case '192.168.1.3': // PRODUCTION
$enable_say = 1;
define( "DEBUG", false);
$SMTPSmartHost = "192.168.1.7"; // Kerio Server
define("K_ServerMode","P");
break;
case '192.168.1.9': // STAGE
$enable_say = 1;
define( "DEBUG", false); //DEV
$SMTPSmartHost = "192.168.1.2"; // Cameron's DEV VM
define("K_ServerMode","S");
break;
case '192.168.1.45': // DEV
default:
$enable_say = 1;
define( "DEBUG", true); //DEV
$SMTPSmartHost = "192.168.1.2"; // Cameron's DEV VM
define("K_ServerMode","D");
break;
}

Wednesday, May 16, 2007

FTP a file and rename it to today's date.

This isn't difficult, but it can be useful. This routine will grab a file (one or more actually but be careful) and rename the received file to today's date. You could go further and exercise the %TIME% variable, but that's up to you.

getfile.cmd:
@echo off
if exist dlfilespec del /f /q dlfilespec
ftp -s:getfile.ftp -v
if exist destfilespec_%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%.zip ~SPLIT~
del /f /q destfilespec_%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%.zip
ren dlfilespec ~SPLIT~
destfilespec_%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%.zip
getfile.ftp:
open host
username
password
binary
prompt
mget dlfilespec
ls
close
bye

Friday, May 04, 2007

Alerting when a process is a memory pig...

Now this example uses putty.exe right now, but the original purpose was to alert when a jrun.exe was getting too big for it's britches. Run as a scheduled task (AT job) and customize to suit:
@echo off
for /F "usebackq tokens=1,2,3,4,5,6 delims=, " %%a IN (`tasklist /NH /FI "imagename eq putty.exe"`) do CALL :CHECKMU %%e%%f
goto :eof

:CHECKMU
SET /A MEMUSAGE=0+%~1
IF /I %MEMUSAGE% GTR 1400 echo Memory Usage Warning: %MEMUSAGE%

GOTO :eof
The echo for 'Memory Usage Warning' will only execute if the memory usage exceeds 1400 (in this example) This could be a call, note the use of subroutines in the batch file (CHECKMU). If there are many processes running the PID could be passed using other variables (%%a, %%b, etc.) but it will execute for each instance that meets the criteria.

Friday, January 19, 2007

Running into The Man in Blue

I recently went hunting for information on detecting browser information using JavaScript and through no random chance, found myself reading the blog of The Man in Blue.

While the information was valuable, what impressed me, and inspires me, is the design of his portfolio site. It's clean, usable, and incredibly simple. The interface is poetically enjoyable and intuitive. I would hope no one would copy this site, that would be wrong, but instead find inspiration in his design.

Please have a look: The Man In Blue

Wednesday, December 13, 2006

Feeling Regular and the use of Fibre.

No, I'm feeling fine, though a tad tired.

I'm finally getting around to exploring Regular Expressions (RegEx) and coming to realize the value of the beast and the simplicity of the design. I picked the brain of my friend and mentor, Brent Ashley today and he showed me the RegEx Coach and introduced me to some of the logic. I found more in my O'Reilly book, "JavaScript: The Definative Guide" (I have the 3rd edition) tonight and I can say one thing about RegEx.

LEARN IT! Come to understand it and you'll be SO far ahead of the game. It is far from being the ONLY thing you need to know, but it will make you that much better at doing things the right way.

Now, I also asked Brent to do a code an informal code-review on a little VBScript thing I was working on. Frankly, it wasn't going well because I was just a little more rusty that I enjoy being, but I asked for the review to get an idea of where I need to improve. I found out.

It's tough to hear that you're not an ace, but you NEED to hear it. Efficiency is everything in development and my design was weak. There's much more I need to learn, but his manner of showing me where I can improve was skillful and conscientious.

There is no individual ownership when you are part of a team, it's the sum of the parts that makes you the RESILIENT team you need to be...