Error Handling with PHP

Make Your Imaginations Alive

Error Handling with PHP

April 20, 2016 Programming 1

Error Handling with PHP

Hi Friends,
Hope you are all well. I have been always struggling with PHP session variables or any data when I am passing from one PHP page to other PHP page, so I found this method which is very useful to check if that value is passing or not when you enter in web application.
In simple terms it is nothing but writing logOutput function.
A big thanks to Professor Sebastian Lenczewski who taught me this testing concept.
Let’s understand how to do this.
A complete code can be found here.
To illustrate this methodology let’s consider a simple PHP form application which takes two input field.
Create a new folder and name it anything, I have name mine Error_Handling. Make sure you store it in right place as we are going to right server – side code that is PHP. There are different local server available depending on which OS you are using to name some MAMP, WAMP, LAMP, XAMPP.
Let’s start by creating two php file one with index.php and second with test.php. In the index.php we are going to write a simple Form code I have started my session at a top to store my all input field data in session. Go ahead and copy the below code in index.php .


Before we move to test.php file, let’s understand the main part of this blog which log.php file where I am creating one .txt document.
Create a new file in the PHP project directory and name it log.php and copy the below code.
” . $text . “\n”;
//$logFile = fopen(“serverLog.txt”, “w”) or die(“Unable to open file!”);
$logFile = fopen(“serverLog.txt”, “a”) or die(“Unable to open file!”);
fwrite($logFile, $logFormatedText);
Great, now if you see in the above code all I have done is created a function name logOutput which creates a new serverLog.txt file in the project whenever I call logOutput function in test.php.
Now that we have done this lets go ahead and move to test.php code. Copy this code in test.php file.

In the above code I have used require(log.php) . This will include the log.php in test.php file without wasting lines in test.php. Again it’s good method to use include or require.
If you had written if(isset) before then this would look familiar. It’s nothing but a simple test case which expects the result when i enter Name and Email when I execute index.php.
The main thing to look for here is my logOutput function. It’s pretty straight forward I am just displaying my session variables in text file.
Now we have our backend ready lets go ahead and run this program.


I have run my index.php file and entered Name and Email. After you click on Submit test.php file will run, but it will be blank file, so just go ahead check your folder there will be serverLog.txt file created which will show us the following result.

Boom! Here it goes it shows my Name and Email in serverLog.txt file.
So whenever you are using with passing data from one page to other page with PHP this log file will help you debug the error of passing the data.
Hope this helped you guys.
Thank you

Author- Adesh Shah


One Response

  1. Awesome things here. I am very happy to peer your article.
    Thanks a lot and I am looking forward to touch
    you. Will you please drop me a e-mail?

Leave a Reply

Your email address will not be published. Required fields are marked *