Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Sunday, April 12, 2009

Detecting IP Address Behind The Proxy

This is the short tutorial for detecting the IP address of website's visitors. If you familiar with PHP, you can use the $_SERVER['REMOTE_ADDR'] superglobal variable to detect the visitor's IP address. What if the visitor is browsing from behind the proxy. Of course you will only get the proxy IP address not the real IP address of the visitor.

There is one more superglobal variable to detect the real IP address of the visitor. It is the $_SERVER['HTTP_X_FORWARDED_FOR'].

This simple program will demonstrate the detection of IP address and the IP address that "hidding" behind the proxy.

<?php
echo "<p>You came from <b>".$_SERVER['REMOTE_ADDR']."</b>.</p>";

if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  if (preg_match("/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/i", $_SERVER['HTTP_X_FORWARDED_FOR'])) {
    echo "But, your real IP address is <b>".$_SERVER['HTTP_X_FORWARDED_FOR']."</b>.";
  }
  else{
    echo "But, your real IP address is unknown.";
  }
}
?>

This is the screenshot while the script was running.


Monday, October 6, 2008

How To Make NuSOAP Work With PHP 5

NuSOAP is a very usefull SOAP Toolkit for developing web service with PHP. You won't have any problem when using it with PHP 4. But, if you are using PHP 5, you might find a little problem. PHP 5 has its own SOAP extension, so if that SOAP extension is loaded it will conflict with NuSOAP and you will get an error message like this:

Fatal error: Cannot redeclare class soapclient in C:\xampp\htdocs\lib\nusoap.php on line 7240

There are two alternatives to solve this problem.

First Alternative

You can de-activated PHP SOAP extension by editting the php.ini file. Give a semicolon (;)  in the front of the line says extension=php_soap.dll. So, that line should be look like this:

;extension=php_soap.dll

Restart your web server to make the change take effect.


Second Alternative

You can also modify the nusoap.php file to rename the soapclient class to another name like soap_client.  You can use your favorite text editor and replace all soapclient text with soap_client. So, if you want to make an instance from NuSOAP client you have to use the syntax like this:

$client=new soap_client($url);

This way is good if you want to use NuSOAP but you still want to retain the PHP SOAP extension.


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.