How to connect from PHP to an Oracle database using OID (Oracle Internet Directory):
OID is like a lookup index that contains connection strings for connecting to various databases. Without OID, a database connection string would be stored directly in the code settings and used by the PHP code to connect to a database. With OID, a lookup can be made to the OID LDAP to acquire the database connection string. Then the PHP code will use the acquired database connection string to connect to the database as before.
OID allows a DBA to manage/change which database server that an application uses without having to change any database settings in the PHP application itself or on the application server.
Here are the basic steps for PHP to connect to a database via OID:
- The DBA should provide you the connection information for the OID LDAP as well as the username/password for the database connection.
- Connect to the OID LDAP using the provided information
- Search for the appropriate LDAP record
- Get the connection string data from the record attribute, "orclnetdescstring"
- Close the LDAP connection
- Use the acquired connection string data to connect to the database as usual using the provided database username/password.
Here is basic sample code to do this:
// Get connection string from OID LDAP
$ds=ldap_connect($servername,$serverport); // Connect to ldap
$r=ldap_bind($ds); // Bind to ldap
$sr = ldap_search($ds, "cn=OracleContext,dc=___,dc=___,dc=___", "cn=$sid"); // Run query
$info = ldap_get_entries($ds, $sr); // Get entries
ldap_close($ds); // Close connection
$dbconnectstring = $info[0]["orclnetdescstring"][0]; // Extract db connect string from ldap search result array
// Connect to database using acquired connection string from OID
$dbconnection = oci_connect ($username,$password,$dbconnectstring);
Изисквания
You will need the Oracle client libraries to use this extension. Windows users will need libraries with version at least 10 to use the php_oci8.dll.
Забележка: This extension does not support Oracle 8 client libraries anymore. Though you still can connect to Oracle 8 servers as long as the client library (v.9+) supports this.
The most convenient way to install all the required files is to use Oracle Instant Client, which is available from here: » http://www.oracle.com/technology/tech/oci/instantclient/instantclient.html. To work with OCI8 module "basic" version of Oracle Instant Client is enough. Instant Client does not need ORACLE_SID or ORACLE_HOME environment variables set. You still may need to set LD_LIBRARY_PATH and NLS_LANG, though.
Before using this extension, make sure that you have set up your Oracle environment variables properly for the Oracle user, as well as your web daemon user. These variables should be set up before you start your web-server. The variables you might need to set are as follows:
- ORACLE_HOME
- ORACLE_SID
- LD_PRELOAD
- LD_LIBRARY_PATH
- NLS_LANG
For less frequently used Oracle environment variables such as TNS_ADMIN, TWO_TASK, ORA_TZFILE, and the various Oracle globalization settings like ORA_NLS33, ORA_NLS10 and the NLS_* variables refer to Oracle documentation.
After setting up the environment variables for your web server user, be sure to also add the web server user (nobody, www) to the oracle group.
Забележка: If your web server doesn't start or crashes at startup
Check that Apache is linked with the pthread library:
# ldd /www/apache/bin/httpd libpthread.so.0 => /lib/libpthread.so.0 (0x4001c000) libm.so.6 => /lib/libm.so.6 (0x4002f000) libcrypt.so.1 => /lib/libcrypt.so.1 (0x4004c000) libdl.so.2 => /lib/libdl.so.2 (0x4007a000) libc.so.6 => /lib/libc.so.6 (0x4007e000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
If the libpthread is not listed you have to reinstall Apache:
# cd /usr/src/apache_1.3.xx # make clean # LIBS=-lpthread ./config.status # make # make install
Please note that on some systems, like UnixWare it is libthread instead of libpthread. PHP and Apache have to be configured with EXTRA_LIBS=-lthread.
If you plan to use Oracle Internet Directory with LDAPS, you NEED to compile the php-ldap library with Oracle Support.
Also, with OID 10g I had to create a folder, reference it in the env variable $ORACLE_HOME, and copy in it three ldap message files found in my OID installation.
At the end, my $ORACLE_HOME looked like this :
ldap
mesg
ldapf.msb
ldapus.msb
ldapus.msg
(Note that the ldapf.msb is here because I have a french installation of OID)
To use ldaps, you will need to use ldap_connect this way :
ldap_connect(host,port,"file://path/to/wallet",walletpassword,sslauth)
Your wallet only needs to contain the CA (secure) certificate.
with sslauth being :
1 for no ssl authentication
32 for ssl server authentication
64 for ssl mutual authentication
