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);