Tuesday, September 23, 2008

Integrating PHP with Microsoft IIS

PHP always identic with Apache web server. But, do you know that the other web servers can be used together with PHP as well as Apache? This short tutorial will show you how to use PHP alongside with Microsoft Internet Information Services (IIS).

Prepare the software

First, you have to prepare these software:
  1. Microsoft IIS from Microsoft Windows CD
  2. PHP (zip version). You can find it at www.php.net
Installation
If you have IIS and PHP installed on you computer, you can skip this part.
  1. Install IIS
  • Insert the Microsoft Windows CD.
  • Open Control Panel and choose Add or Remove Programs.
  • Click on Add/Remove Windows Components.
  • Give a check mark on Internet Information Services.
  • Click Next and follow the installation instruction until finish.

  1. Install PHP
  • Extract the zipped file to C:\php5.
  • Copy php.ini-dist to C:\Windows then rename it to php.ini.
  • Edit your php.ini and find this following line : extension_dir="./". Change it to extension_dir="C:\php5\ext\".
Configure your IIS
Let's configure IIS.
  1. Open Windows Control Panel. Click Administrative Tools, then click Internet Information Services.
  2. Click the "+" sign beside your computer name, then click on the same sign beside the Web Sites folder.
  3. You will find the Default Web Site and you have to right-click on it and choose properties to start configuring you IIS.
  4. Click the Home Directory tab. Find the Configuration button and click it.
  5. On the Mapping tab, click the Add button.
  6. Now, you are about adding the .php extension so the PHP file can be processed by IIS. In the executable textbox you have to point to the full path of the php5isapi.dll file location for parsing PHP file. You can click browse to find it. In this example, the location of php5isapi.dll is in C:\php5.
  7. In the extension textbox, you have to fill it with the PHP file extension which is .php. Click OK.
  8. Finish? Not yet. Back to the Default Web Site's properties window, click on Documents tab. Now you are about configuring the default page for IIS. Click Add and type index.php for the default document name. Click OK.
Testing
Create the phpinfo.php file which has simple content like this.

<?php phpinfo(); ?>

Save it in your web server directory (C:\Inetpub\wwwroot) and call it from your web browser.

More Options
If you want to use a database server, you have to activate the PHP extension to the related database server. For example if you want to use MySQL, you have to uncomment the line like this ;extension=php_mysql.dll. After that, don't forget to copy the libmysql.dll file from C:\php5 to C:\Windows\System32.

The same way can be applied if you want to use Oracle database server. First, you have to uncomment the line that says ;extension=php_oci8.dll. The next step is a little bit different. You have to copy oci.dll file from your Oracle Instant Client directory to C:\Windows\System32. You also have to set some environment variables such as PATH and NLS_LANG. You can see my previous tutorial about Connecting To Oracle 8 With PHP for further information.

Wednesday, September 17, 2008

Connecting To Oracle 8 With PHP

Connecting to Oracle database with PHP is not as easy as connecting to MySQL. There are few steps to do before your PHP can connect to Oracle database.

In this tutorial, I try to connect to Oracle with PHP from a Windows machine.

The Software
These are the lists of software I used to test this tutorial:
  1. XAMPP 1.6.7
  2. Oracle Instant Client 10.1 for Windows
  3. Oracle 8i Database Server (remote or local)
Preparing The System
These are the steps to preparing your system before you can connect to Oracle using PHP.
  1. Install XAMPP.
  2. Install Oracle if you want to use a local database server.
  3. Extract the Oracle Instant Client to C:
  4. Copy oci.dll file to C:\xampp\apache\bin.
  5. Add the Oracle Instant Client path (C:\instantclient10_1)  to your system PATH.
  6. Add NLS_LANG as a new system variable. Fill it's value with american_america.we8iso8859p1.
  7. Restart your system to make the changes take effect.
Edit Your php.ini
php.ini file for XAMPP can be found in C:\xampp\apache\bin. Open that file with your text editor and find this following line:

;extension=php_oci8.dll

Uncomment that line so it will be looked like this:

extension=php_oci8.dll

Save that file and then restart your Apache web server. If no error message appear that mean the Oracle module for PHP is loaded properly.

Test It!
Now, prepare the PHP script to test the connection with Oracle. I made the script like this:

$host='10.234.2.12';
$user='USER';
$passwd='123456';
$service_name='TEST';

$db='(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)
(HOST='.$host.')(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME = '.$service_name.')))';

$con=oci_connect($user,$passwd,$db);

if($con){
     echo 'Connected to Oracle.';
}
else{
     echo 'Cannot connect to Oracle.';
}

oci_close($con);


Run the script and see if it works.