Create Virtual Host in XAMPP...

by Lasantha Samarakoon on Friday, December 25, 2009

XMAPP is one of the main easy to install Apache distribution which containing MySQL, PHP, Mercury Mail Transport System, Filezilla and etc. And this package is available for many operating systems such as Microsoft Windows, Linux, Mac OS X and Solaris. And the good thing is it’s a freeware. So you can download it free of charge from here.

If you want to use XAMPP, you need to host your web pages and files in XAMPP web folder, which is known as “htdocs”. You can access this folder throw your browser by typing http://localhost in the address bar. So instead of that today I am going to show you how to create another directory in your hard drive and host it in XAMPP. Steps are as follows.

Create a folder in your HD which is going to use as the root directory. As an example I will create a directory in the D:\Virtual\myroot\. So “myroot” will be my new root directory.

1. Open XAMPP installed directory.

2. Go to apache -> conf -> extra directory.

3. In the extra directory there is a file called “httpd-vhosts.conf”. Open it in text editor such as NOTEPAD.

    DocumentRoot "D:/Virtual/myroot/"
    <Directory D:/Virtual/myroot/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all

4. Save and close the file.

5. Now go to the "Windows" folder. Open System32 -> drivers -> etc folder.

6. In the “etc” directory, there is a file called “hosts”. Open it in a text editor.

7. At the end of the file append following text.

8. Save and close the file. Sometimes your anti-virus program will block saving. In that kind of situation, allow it.

9. Go to “XAMPP Control Panel” and stop and restart the Apache service.

10. Open your preferred browser and type

11. You can see the content of you new virtual host.

PEAR: An Introduction...

by Lasantha Samarakoon on Saturday, November 21, 2009

Today I’m gonna say you about something popular among the PHP developers. So that’s called PEAR. I hope you guys may be heard about PEAR. PEAR is an acronym for PHP Extension and Application Repository. PEAR is a framework or collection of PHP classes which allow us to use them in our solutions. Official web site of PEAR can be found on

As I told you that PEAR is a collection of classes or components. So what are the components available in PEAR?  It has many more components such as Database, File system, Graphics and Image Processing, Mail, Math, Structures and etc. So today I’m going to show you how to use Database component of PEAR.

PEAR Database:

In most of web developing solutions, PHP developers use databases to store data. So the backend database management system can be MySQL, PostgreSQL, SQLite, etc. There are different commands available in PHP to handle each and every database types. What will happen when you want to shift from MySQL to PgSQL? This will result you to change all the PHP MySQL commands to PHP PgSQL commands. In this kind of changing environment, it is advisable to use a framework such as PEAR.

Using PEAR, you can handle many DBMSs without changing the code. It is something like a common API set. The only part you have to change when you want to shift is the connection string.

OK… Let’s consider about the example. In this example, I will connect to a MySQL database, fetch some data and display them on the web page. First you need to create the database as follows.

    uname VARCHAR(20) NOT NULL,
    pword VARCHAR(20) NOT NULL

That’s all. Then the PHP code is as follows. I’ll explain them later.

// peardb_ex.php
require_once 'DB.php';

// connect to the database
$db = DB::connect('mysql://root:rootpass@localhost/test');

// check for errors
if(DB::isError($db)) {
    echo "Unable to connect to the database: " . $db->getMessage();
}else {

    // execute the query
    $sql = "select * from users";
    $res = $db->query($sql);

    // get number of rows affected
    $num_res = $res->numRows();

    echo "<b>$num_res users</b><hr /><hr />";

    // travers the resultset
    while($r = $res->fetchRow(DB_FETCHMODE_ASSOC)) {

        echo "username: " . $r['uname'] . "<br />";
        echo "password: " . $r['pword'] . "<br />";
        echo "<hr />";

    // free resultset

    // close the connection
require_once 'DB.php';

This is the file that has definitons for PEAR database component. It's available in the PEAR installation folder, so you need not to copy it to the local directory.

$db = DB::connect('mysql://root:rootpass@localhost/test');

This is how the connection will be established. The connection string can be specify as, Driver://username:password@host/database. If you want to shift from MySQL to PgSQL, the only thing you need to do is change the connection string to, pgsql://root:rootpass@localhost/test. Other commands will remain unchanged.

This is used to check if any error occurred.
$sql = "select * from users";
$res = $db->query($sql);

This is the command used to execute SQL query against the database. (Same like $res = mysql_query($sql))

$num_res = $res->numRows();

Get number of rows returned by the query.

while($r = $res->fetchRow(DB_FETCHMODE_ASSOC)) {

    echo "username: " . $r['uname'] . "<br />";
    echo "password: " . $r['pword'] . "<br />";
    echo "<hr />";

This loop is used to travers the resultset. It is same as mysql_fetch_assoc($res).


Free the resultset.


Close the connection.

AJAX File Download

by Lasantha Samarakoon on Sunday, November 15, 2009

Good day guys, today I’m going to show you how to download a file without reloading or refreshing the current page. So to do that, I will use my preferred server-side language PHP, HTML and little bit of CSS.

So the problem is, I have a link named “Download” and when I click on that link browser’s file download box should be visible without refreshing or navigating away from the page. So to solve this problem I will use an AJAX technique. Let’s think how we can implement this in AJAX.

My solution is this. I will create a HTML inner frame in my web page. And when I click on the download link, something will be loaded in the inner frame. So this is how I solved that.

I will create 2 web pages, one is pure HTML and other one is PHP. The HTML page contains the inner frame and download button. The PHP file is used to dynamically read the content of the downloadable file and write them to the client.

<!-- index.htm -->  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">  
<html xmlns="">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>AJAX Download</title>


<!-- this inner frame is hidden by css -->
<iframe name="ajax_frame" style="display: none;"></iframe>

<!-- download link -->
<a href="download.php" target="ajax_frame">Download</a>


Here I named inner frame as “ajax_frame” and in the link I set the target to that inner frame. So when I click on the link, the content will be loaded in the inner frame. And I set the display style of the inner frame to “none” to hide the frame in the web page. So the user cannot see the inner work of the web page.

<?php // download.php  
// send http header 'content-type'
header("Content-Type: text/plain");  

// start flushing data  

// echo data  
echo "hello world";  

// get the flushed data size and send http header attribute 'content-length'  
header("Content-Length: " . ob_get_length());  

// send http header information regarding the file attachment  
header("Content-Disposition: attachment; filename=test.txt; modification-date=\"Sun, 15 Nov 2009 12:50:00 +0530\";");  

Above is the technique I used to push the file content to the client. It’s just a PHP echo with some header commands. In this demonstration I did not used any external file, but I send some content using the echo command. This is really same as reading a text file contents “hello world” in it. Let’s consider line by line.

header("Content-Type: text/plain");

I used header command to send HTTP response headers to the client. Here I send “Content-Type” header indicating the incoming data is belongs to plain text. You can use any other Content-Type regarding to the data you are going to send. As an example, for a PDF file you can send “application/pdf”.

Start flushing data to the client.

echo "hello world";

Echo the content of the file.

header("Content-Length: " . ob_get_length());

Get the length of data flushed to the client and send the header “Content-Length” to the client.

header("Content-Disposition: attachment; filename=test.txt; modification-date=\"Sun, 15 Nov 2009 12:50:00 +0530\";");

Here it is again an HTTP response header, indicating that the flushed data belongs to the file named “test.txt” and the modified date of the file.

So this is all about the solution. If you want to send really an external file to the user, you have to replace line # 9 with commands regarding to open the file, read the content, echo the content and close the file. That’s all.