Prashant Ingale

Juice of PHP, MySQL, Apache, Linux, Actionscript, Flex

Archive for August 23rd, 2008

PHP Simple Error Handling

leave a comment

This is simple example of error handing in PHP using .htaccess. Let’s create .htaccess file first open your favorite editor and write below code and save it.

ErrorDocument 400 /error/index.php?error=400
ErrorDocument 401 /error/index.php?error=401
ErrorDocument 403 /error/index.php?error=403
ErrorDocument 404 /error/index.php?error=404
ErrorDocument 500 /error/index.php?error=500

Please refer Apache Docs for getting information on ErrorDocument and error codes. After saving .htaccess file place it in the root folder. Create new file and put below code in that file for handling errors.


$homepage = “mywebpage.com”;
if (isset($_GET['error'])) {
    if ($_GET['error'] == 400) {
        $title = ‘Error 400 - BAD REQUEST!’;
        $message = ‘The server didn\’t understand the request.’;
    } elseif ($_GET['error'] == 401) {
        $title = ‘Error 401 - UNAUTHoRIZED!’;
        $message = ‘You are not unauthorized to view the page requested.’;
    } elseif ($_GET['error'] == 403) {
        $title = ‘Error 403 - ACCESS FORBIDDEN!’;
        $message = ‘Access for this request was denied.’;
    } elseif ($_GET['error'] == 404) {
        $title = ‘Error 404 - PAGE NOT FOUND!’;
        $message = ‘File not found! ‘;
    } elseif ($_GET['error'] == 406) {
        $title = ‘Error 404 - NOT ACCEPTABLE!’;
        $message = ‘The resource cannot be displayed! ‘;
    } elseif ($_GET['error'] == 500) {
        $title = ‘Error 500 - INTERNAL SERVER ERROR!’;
        $message = ‘The server encountered an unexpected condition which prevented it from fulfilling the request.’;
    } elseif ($_GET['error'] == 502) {
        $title = ‘Error 502 - BAD GATEWAY!’;
        $message = ‘The server, while acting as a gateway or proxy, received an invalid response from the upstream server.’;
    } elseif ($_GET['error'] == 504) {
        $title = ‘Error 504 - GATEWAY TIMEOUT!’;
        $message = ‘The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server.’;
    }
}

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HEAD>
<TITLE>[<? echo $title ?>]</TITLE>
<META http-equiv=Content-Type content=”text/html; charset=iso-8859-1″>
<META HTTP-EQUIV=Refresh CONTENT=”30; URL=http://<? echo $homepage ?>”>
<STYLE>
BODY {
	FONT-SIZE: 10px;
 BACKGROUND: url(http://<? echo $homepage ?>/error/bg.gif);
	COLOR: #333333;
	LINE-HEIGHT: 180%;
	FONT-FAMILY: tahoma, arial;
}
A {
	PADDING-RIGHT: 1px;
	PADDING-LEFT: 1px;
	PADDING-BOTTOM: 1px;
	COLOR: #ff6600;
	PADDING-TOP: 1px;
	TEXT-DECORATION: none
}
A:hover {
	PADDING-RIGHT: 1px;
	PADDING-LEFT: 1px;
	PADDING-BOTTOM: 1px;
	COLOR: #ff6600;
	PADDING-TOP: 1px;
	TEXT-DECORATION: underline;
}
TD {
	FONT-SIZE: 10px;
	COLOR: #333333;
	LINE-HEIGHT: 180%;
	FONT-FAMILY: tahoma, arial;
}
.box {
	BORDER-RIGHT: #e3e3e3 1px solid;
	BORDER-TOP: #e3e3e3 1px solid;
	BACKGROUND: #ffffff;
	BORDER-LEFT: #e3e3e3 1px solid;
	WIDTH: 400px;
	BORDER-BOTTOM: #e3e3e3 1px solid;
}
</STYLE>
<SCRIPT language=”JavaScript”>
<!–
 function getgoing()
  {
    top.location=”http://someplace.com”;
   }
   if (top.frames.length==0)
    {
     alert(”You will be redirected to our main page in 10 seconds!”);
     setTimeout(’getgoing()’,100000);
     }
//–>
</SCRIPT>
</HEAD>
<BODY onLoad=”getgoing();”>
<CENTER>
  <BR>
  <BR>
  <BR>
  <BR>
  <DIV class=box> <BR>
    <BR>
    <H3><? echo $title; ?><br>
      <? echo $message; ?></H3>
    <br>
    <DL>
      <DD> You will be automatically redirected to  www.<? echo $homepage ?> in 10 seconds.<br>
        If u don’t wish to wait click <a href=”http://<? echo $homepage ?>”>here</a>. </DD>
    </DL>
  </DIV>
</CENTER>
</BODY>
</HTML>

PHP if conditions will trap error codes and assign respective title and message for the code. And below presentation code will display an appropriate message to the user for 10 seconds and then it will redirect page to the provided URL in $homepage variable
Now create directory in the root folder called “error” and save this file with the name index.php and your are done.

Written by admin

August 23rd, 2008 at 12:17 pm

How to setup XAMPP

leave a comment

If you are new PHP developer then this article will guide you to setup XAMPP on Windows.

First of all download XAMPP exe from  www.apachefriends.org and start installation follow instructions.

At the end of installation installer will ask to install some servers as service. It depends on choice if you are much lazy to start servers every time, let the OS do this for you. It is always preferable not to install all the servers as service. At the finish of installation it will start servers. Test if you can able to access http server by opening these URLs http://localhost or http://127.0.0.1

For securing your XAMPP installation type this URL in browser http://127.0.0.1/security now you will be able to change default passwords of MySQL, PhpMyAdmin and FileZilla. After securing your development area you can start using it.

Now for publishing web contents find default www document (htdocs) folder in xampp installation directory. Create new html document test.html and put some content and try accessing it in browser using http://localhost/test.html same procedure follow for creating PHP file. You can create subfolders (new) in your document root (htdocs) and copy test.html file there. Then enter the URL http://localhost/new/test.html to view this in your browser.

For sending request to default page ex. index.html, index.php you will have to configure httpd.conf file before changing this file make sure you have created backup file. Now search for “DirectoryIndex” directive in the file. You can find httpd.conf file in apache/conf folder for your xmapp install folder.


DirectoryIndex index.php index.php4 index.php3 index.cgi index.pl index.html index.htm index.shtml index.phtml

Now save httpd.conf file and restart apache server. Create index file with any extension you have included in DirectoryIndex directive and save it. Type http://localhost in the browser and enter you will see default page you created just now. Enjoy!!!

Written by admin

August 23rd, 2008 at 11:24 am

Posted in XAMPP/LAMP