• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to test a PHP script

by admin

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

Filed Under: Linux, PHP

Some more articles you might also be interested in …

  1. CentOS / RHEL : How to migrate storage (LVM) with pvmove Command
  2. Can’t start X11 applications after “su” or “su -” to another user
  3. How to Install and configure telnet in RHEL / CentOS 5,6
  4. How to Permanently set the ethtool settings in CentOS/RHEL 6
  5. Introduction to sed (Stream Editor) : Useful sed Command Examples
  6. How to Setup VNC Server for New User in CentOS/RHEL 5
  7. What are Shell Scripts? How to Create Shell Scripts?
  8. understanding “yum history” command output
  9. List of SELinux Utilities
  10. How to Re-Create the Yum Cache and/or Force a Fetch of the Package List of the Enabled Repositories

You May Also Like

Primary Sidebar

Recent Posts

  • What are /dev/zero and /dev/null files in Linux
  • grpck command – Remove corrupt or duplicate entries in the /etc/group and /etc/gshadow files.
  • xxd command – Expressed in hexadecimal form
  • sesearch: command not found

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright