How to test a PHP script

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. PHP runs on all major operating systems, from Unix variants including Linux, FreeBSD, Ubuntu, Debian, and Solaris to Windows and Mac OS X. It can be used with all leading web servers, including Apache, Nginx, OpenBSD servers to name a few; even cloud environments like Azure and Amazon are on the rise.

Below are some of the ways in which a PHP script can be tested.

Testing Simple PHP Script

1. Create a file with the following contents. Give the file a name such as myphpInfo.php:

<?php

phpinfo();

?>

2. Copy the file to the your webservers DocumentRoot directory, for example – /var/www/html. You may have a different DocumentRoot directory depending on which webserver you are using and the configuration done for it.

3. Change the permissions to 755 (Linux only):

# chmod 755 myphpInfo.php

4. Call the file from a browser:

http://Fully-Qualified-Hostname:PORT#/phpinfo.php

Testing a PHP Script that uses Database Connections

1. Create a file with the following contents. Give the file a name such as phpdbchk.php:

<html> 
<head> 
<title>PHP Database Connection Test</title> 
</head> 
<body> 

<?php 

$username = 'scott'; 
$password = 'password'; 
$database_hostname = 'host.domain'; 
$database_port = 'port'; 
$database_sid = 'sid';
$database_srvc = 'servicename';

$easy_connect_syntax = '//'.$database_hostname.':'.$database_port.'/'.$database_srvc; 


// If Oracle 10g libraries are used by PHP try the new Easy Connect syntax. 
// No long connection string is needed. No tnsnames.ora file is required. 
// This does not work with standalone HTTP Server installations

// $conn = OCILogon($username, $password, $easy_connect_syntax); 


// Use this line if TNS is setup properly in $ORACLE_HOME/network/admin 
$conn = OCILogon($username, $password, $database_sid); 


if (!$conn) { 
  $e = ocierror(); 
  print htmlentities($e['message']); 
  exit; 
} 

$query = 'SELECT SYSDATE FROM DUAL';
$stmt = ociparse($conn, $query);
ociexecute($stmt, OCI_DEFAULT);

print 'Checking for the Date and Database Connectivity<br>'; 

$success = 0;
while (ocifetch($stmt)) {
  print "Date: " . ociresult($stmt, "SYSDATE") . "<br>\n";
  $success = 1;
}

if ($success) { print 'Success.<p>'; } 
else { print 'Failed to retrieve the date.<p>\n'; }

OCILogoff($conn); 


print 'PHP Configuration<br>'; 
print '======================<p>'; 

phpinfo(); 

?> 

</body> 
</html>

2. Set ORACLE_HOME and TNS_ADMIN to the proper values.

3. Copy the file to the DocumentRoot directory.

4. Modify the variables $username, $password, $database_hostname, $database_port, $database_sid and $database_srvc as necessary for the test system

5. Change the permissions to 755 (Linux only):

chmod 755 phpdbchk.php

6. Call the file from a browser:

http://Fully-Qualified-Hostname:PORT#/phpdbchk.php

The following error occurs if ORACLE_HOME\network\admin\tnsnames.ora is not set up correctly or missing. If it is missing, the one from the database can be copied over and used as is.

Warning: ocilogon(): _oci_open_server: 
ORA-12560: TNS:protocol adapter error in [oracle_home]\apache\apache\htdocs\phpdbchk.php on line 25
ORA-12560: TNS:protocol adapter error

Running PHP Script to another directory outside of htdocs

For example, if you want to place php scripts to $ORACLE_HOME/Apache/Apache/phpsrc and run them from there via browser e.g http:FQHN:[port]/php/info.php, then do the following:

1. make directory $ORACLE_HOME/Apache/Apache/phpsrc

2. Copy info.php script to $ORACLE_HOME/Apache/Apache/phpsrc

3. Edit httpd.conf and add this line:

Alias /php/ $ORACLE_HOME/Apache/Apache/phpsrc

4. Restart http server and now it should work:

http:FQHN:[port]/php/info.php
Note: The php script info.php was used as an example, you can use any name you choose for your php scripts
Related Post