Saturday, February 03, 2007

Trip to Shimla

Here it comes finally... Yes, the long awaiting pictures of my Shimla trip. I went to Shimla with my friend Arun in the month of October,06. It was a very refreshing and wonderful trip. Also it gave me a first hand knowledge of North India. That was my first trip in North India and travelled through Kurushektra, Ambala, Chandigarh and Kalka.

I got into train at 10Pm at Nizammudin Railway Station, South Delhi. Next day morning we got down in Chandigarh Railway Station at 7AM. From the moment I stepped out of that station I felt, the city Chandigarh has lots in offer to me. From the Chandigarh station I travelled to bus station to get the bus to Shimla. As I expected, It was really a beatifull and wonderful city. Straight roads, more importantly roads were very clean. Really enjoyed the short travel in the city. By the time we reached bus station we are too hungry and wanted to refresh. So from bus station we went to near by Sector 17 market by walk. You will definitely say WoW once you get into that market. It was huge, clean and tidy market. Those old age look buildings lined up at both sides with big space between them added more beauty. After a some search, we found
a age old restaurant. Felt that I was moving back into Gandhian era. That restaurant was named after Gandhi and people worked were all wearing the Nehru style dresses which typically has white Kurda and a type of cap. Luckily they had South Indian items and more surprising fact is they menu have only South Indian items. We had hot and tasty Dosa and Idly with different North Indian cooking flavor.

Here are the pictures of my first stop over on the way to Shimla


Chandigarh Railway Station Entrance

Chandigarh Railway Station

Me at Chandigarh Railway Station

Another View of Chandigarh Railway Station

Beautiful Sector 17 Market

Another View of Market

Friday, February 02, 2007

Trapping Fatal Errors in PHP

Generally most of PHP programmers feels error handling in PHP is cumbersome or they think its just not very programmer friendly and impossible to handle fatal errors. In PHP 4.X.X versions or you can say PHP versions less that PHP 5, its possible to trap the errors with functions like set_error_handler() and trigger_error(). But the possible errors that can be handled by PHP 4 are just Warnings and Notices.

Many programmers have tried to find out ways to handle fatal errors that occur in a program, but never had patience or time to find out a viable solution. Fortunately I encountered a problem in that I must figure out some ways to handle fatal errors in PHP 4 in order to fix the issue. When I started working on this task, I looked all around globe(net) for some clues about how to handle fatal errors, but mostly all of them are either incomplete or not just straight forward that can be understandable instead thay used all sort of tech jerks to make the mud more murkier.

So here I am going to give a clear picture about how to handle fatal errors in PHP 4 with complete lab tested code that will actually work fine in all type of systems.

The main concept behind the logic I used to trap the fatal errors are output buffering. Output buffering is itself a important and big topic to discuss, and if we get into that then we will miss out from the track of our actual destination of trapping errors. So we can move on with first hand information about the output buffering.

The Output Control functions in PHP like ob_start() allows you to control when output is sent from the script. This can be used to control when a script output should be send back to browser.

Okay now I like to go straight into code and later on explain the basics.

FileName1: ErrorHandlerLib.php

<?php
//program to handle fatal errors in PHP 4

//set the error handler to catch unexpected errors
set_error_handler('LowCategoryErrors');
//start the buffering to catch the fatal error msgs
ob_start('FatalErrors');



/**
* function to handle different error types raised by PHP
*
* @return void
*/
function LowCategoryErrors($argErrNr, $argErrMsg, $argFileName, $argLineNr)
{
//initialize local variables
$szErrType = null;
$aErrorType = array(
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parser Error',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
);

//check we have value or not, else set default
if(array_key_exists($argErrNr, $aErrorType))
$szErrType = $aErrorType[$argErrNr];
else
$szErrType = "Unknown";

//form the error message string
$argErrMsg = "Caught PHP Error. $argErrMsg in $argFileName at $argLineNr.";

echo "<br>$szErrType: $argErrMsg\n";
}

/**
* function to handle Fatal error type raised by PHP. This works based on output buffering.
*
* @return string
*/
function FatalErrors($argBuffer = null)
{
//intialize local variables
$aMatches = array();
$argErrMsg = $retValue = $szErrType = null;
$tRes = true;

//parse the buffer to check whether there is any error or not
$tRes = preg_match_all("/<b>(.+?)\serror<\/b>:(.*?)<br/i", $argBuffer, $aMatches,PREG_SET_ORDER);

//diff the error and 0 return value
if($tRes===false)
{
$retValue = "<br>Fatal: Failed to evaluate regex pattern while validing the output buffer.\n";
}
elseif($tRes == 0)
{
//if we dont find any error, return the buffer content as it is
$retValue = $argBuffer;
}
else
{
//loop through the reg-ex patterns that is passed through arguments
for($iCount = 0; $iCount < count($aMatches); $iCount++)
{

//form the error message string
$argErrMsg = "Caught PHP {$aMatches[$iCount][1]} Error. Error Message: {$aMatches[$iCount]['2']}.";

echo "<br>Unrecoverable Error: $argErrMsg";

$retValue = "<br>$argErrMsg";
}
}

return $retValue;
}
?>


The function set_error_handler() here is used to register the error handler for warnings and notices. This will take care of lower category.

The second function ob_start() is the real king here. This registers the PHP buffer output handler and through this we are implementing the fatal error handling logic. This function FatalErrors() will called is there is any explicit call to end the buffer or else it will called whenever the PHP script is going to complete its final processing. Remember here, this function is also called whenever PHP going to end its execution. This makes us possible to trap fatal errors and do last minute processing.

FileName2: ErrorHandlerUsage.php

<?php

//program to demostrate the usage of fatal error handler

//include the error handler library
include "ErrorHandlerLib.php";

//error_reporting should be set to show all in order to catch fatal errors.
error_reporting(E_ALL);


echo "<br>Hello world";


/******************************************************************
Section1 : this makes notices and handled nicely by a handler
******************************************************************/

echo "<br>Im going to make some notices";

//the following statement will make the notice
$TestVar = $UndefinedVariable;

//End of Section1

/******************************************************************
Section2 : this makes warnings and handled nicely by a handler
******************************************************************/
echo "<br>Im going to make some warnings";

//the following statement will make the warning
implode('test');

//End of Section2

/*

//Uncomment this Section 3 only if you want to test the Fatal error handler, to test
//other type of error, just run the program.

//*****************************************************************
//Section3 : this makes warnings and handled nicely by a handler
//******************************************************************
echo "<br>Im going to make fatal error by calling undefined function";

//the following call to undefined functions makes fatal error
SomeJunk();

//End of Section3

*/
?>


The above code example is pretty much self explanatory and clearly spells out how trapping fatal errors are easy in PHP.

All this are just an example of single usage. You can customize outputting and last minute actions according to your needs and logics. You can implement this using object oriented programming logic in different way with other set of restrictions like freezing of object attributes by PHP.

If you like to clarify anything about this or just wanted to know one oe other about PHP you can trap me at kramchel-inet +at+ yahoo +dot+ co +dot+ in(replace the words +at+ and +dot+ with @ and . characters respectively). This is secondary mail ID I use to fight against spam. You can get my permanent ID after sending a first mail.

Happy Coding.

Monday, January 29, 2007

Javascript Gimmicks

Gimmick 1: Edit any webpage with any content...

Go to any web page, clear the address bar, paste the following JS code snippet in that and hit enter...

javascript:document.body.contentEditable='true'; document.designMode='on'; void(0);

OoooHoooo!!! you can now edit whatever you want in that page now....

Gimmick 2: Dancing Images

Go to any web page, clear the address bar, and paste this…

javascript:R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300; y4=200; x5=300; y5=200; DI=document.images; DIL=DI.length; function A(){for(i=0; i-DIL; i++){DIS=DI[ i ].style; DIS.position='absolute'; DIS.left=(Math.sin(R*x1+i*x2+x3)*x4+x5)+"px"; DIS.top=(Math.cos(R*y1+i*y2+y3)*y4+y5)+"px"}R++}setInterval('A()',5); void(0);

and hit enter…

And... Here it goes... Enjoy the Image Dance…

Note: If you are in other part of webpage such as scrolled down to the end of page, probably you cannot see the image dancing... so scroll to the top of web page you are currently browsing to see the image dance.

Note: To get out of the mess created by the above, just hit the browser back button and refresh the page.

Modern Morale Stories

A junior manager, a senior manager and their boss are on their way to a meeting.

On their way through a park, they come across a wonder lamp. They rub the lamp and a ghost appears !

The ghost says," Normally, one is granted three wishes but as you are three, I will allow one wish each" So the eager senior manager shouted, I want the first wish. I want to be in the Bahamas, on a fast boat and have no worries.

Pfufffff, and he was gone. Now the junior manager could not keep quiet and shouted "I want to be in Florida with beautiful girls, plenty of food and cocktails."Pfufffff, and he was also gone. The boss calmly said," I want these two idiots back in the office after lunch at 12.35pm"


Morale of the Story : "Always allow the bosses to speak first"



Standing in front of a paper shredding machine with a piece of paper in his hand.

"Listen," said the CEO, "this is a very sensitive and important document, and my secretary has left. Can you make this thing work?" "Certainly," said the young executive. He turned the machine on, inserted the paper, and pressed the start button. "Excellent, excellent!" said the CEO as his paper disappeared inside the shredder machine. "I just need one copy."

Morale of the Story : "Never, never assume that your BOSS knows everything"



An American and a Japanese were sitting on the plane on the way to LA when the American turned to the Japanese and asked, "What kind of -ese are you?" The Japanese confused, replied, "Sorry but I don't understand what you mean." The American repeated, What kind of -ese are you?" Again, the Japanese was confused over the question. The American, now irritated, then yelled, "What kind of -ese are you .. Are you a Chinese, Japanese, Vietnamese!, etc......???" The Japanese then replied, "Oh, I am a Japanese." A while later the Japanese turned to the American and asked: What kind of 'kee' was he. The American, frustrated, yelled, "What do you mean what kind of '-kee' am I?!" The Japanese said, "Are you a Yankee, donkee, or monkee?"

Morale of the Story : "Never insult anyone"



There were these 4 guys, a Russian, a German, an American and a French, who found this small genie bottle. When they rubbed the bottle, a genie appeared. Thankful that the 4 guys had released him out of the bottle, He said, "Next to you all are 4 swimming pools, I will give each of you a wish. When you run towards the pool and jump, you shout what you want the pool of water to become, then your wish will come true." The French wanted to start. He ran towards the pool, jumped and shouted "WINE". The pool immediately changed into a pool of wine. The Frenchman was so happy swimming and drinking from the pool. Next is the Russian's turn, he did the same and shouted, "VODKA" and immersed himself into a pool of vodka. The German was next and he jumped and shouted, "BEER". He was so contented with his beer pool. The last is the American. He was running towards the pool when suddenly he steps on a banana peel. He slipped towards the pool and shouted, SH*T!!!!!!!........."

Morale of the Story : "Think twice before you say something, because sometimes accidents do happen"

Monday, January 22, 2007

Weekend Visit to DT Mall

From Left: Amit, Me, Abhinav and Gaurav


From Left to Right: Amit, Mani, Abhinav and Gaurav

"Backstreet Boys"
Clockwise from 6PM position: Amit, Gaurav, Myself(Rising Star) and Abhinav

DT Theaters
Looks very tidy in contrast to the rainbow colored theater walls('caz of unbearable spitting habit of some people) back home.

Friday, January 19, 2007

2007 New Year Party (Rocking Party)


Gaurav(in red shirt) rocking there in the name of dance

Bad place



a corner at NYX, 32nd Milestone Complex at Gurgaon(our party venue)

Dance floor armed with everything... right from DJ to Drinks

Edits: blue-pencilled some pictures & texts on 8th July 2010 4:02 PM PST. Request code: frfbmk(I know its weird code, I will forgot what it stands for later but still what is life without fun/mystery/confusion)

2007 New Year cum Welcome Party at 32nd Milestone, Gurgaon



Myself

Mani Shanker Goswami and Abhishek Vats


Me and Abhinav

Edits: blue-pencilled some pictures & texts on 8th July 2010 4:00 PM PST. Request code: frfbmk(I know its weird code, I will forgot what it stands for later but still what is life without fun/mystery/confusion)