You may need to access a database, perform a calculation, or return status information. The server can perform myriad tasks for the client, provided it has the required software. Because many organizations now rely on cloud-based solutions to just about every programming problem, it’s important to have some idea of the activity that occurs during such processing.
PHP is a good option because many servers support it, the language is platform independent, and you don’t generally need many resources to use it. In fact, many hosted sites provide PHP access as part of their default setup, so your organization may already have the required support installed if you rely on a hosted site.
How to add PHP support to your Apache server
If you installed the Apache server on your system, you likely don’t have PHP support installed on your server and will need to install it before you can proceed. The following procedure works for many platforms:
Download the version of PHP you need.
You may be redirected to a different site from this main page.
Extract the file you’ve downloaded to the PHP directory (folder) on your machine.
When changing the Apache configuration, you must supply the directory that you used to store your copy of PHP.
Locate the php.INI development file in the C:php folder and copy it to php.INI.
You should now have a file named php.INI in the directory. This file contains the settings that configure PHP for use. The file you copied creates a development environment, but you should make a quick change to ensure that the environment runs with as few problems as possible.
Open php.INI in a text editor and locate the line that reads short_open_tag = Off. Change this line to read
short_open_tag = On
The reason for this change is that some commercial scripts don’t provide the full PHP starting tag of <? PHP. They use <? instead, which is the short open tag. If you don’t set this setting to On, your PHP configuration won’t be able to read these commercial files.
Save php.INI and close your text editor.
Open the Apache httpd.CONF file in a text editor.
This file is normally found in the Apache2.2conf directory on your system. You must make sure you open the text editor in administrator mode, especially when working with a Windows system, or you won’t be able to save the file when you’re finished making changes. For example, when working with a Windows system, right-click the Notepad icon in the Start menu and choose Run As Administrator from the context menu.
Locate the section that contains the LoadModule entries. Add a new line and type
LoadModule php5_module "c:/php/php5apache2_2.dll"
This line adds support for your PHP installation.
Locate the section that contains the AddType entries. Add a new line and type
AddType application/x-httpd-php .php
This line tells the server what to do with PHP files you include on the server.
Go to the end of the httpd.CONF file and add a new line. Type
This line tells the server where to find the php.INI file you created earlier in this procedure.
Save the changes to disk and close your text editor.
Restart the Apache server.
This step forces the server to install PHP support.
How to test your PHP configuration
Even if you think you have PHP support installed, it’s a good idea to test PHP support on your server. Otherwise, you won’t know whether your PHP configuration is working. The following procedure helps you test your configuration:
Create a new file with your text editor.
Type <?php phpinfo(); ?> in the file and save it as Test.php to the Apache2.2htdocs directory on your system.
Open your browser and type http://localhost/Test.php in the address bar. Press Enter.
You should see the configuration information for your PHP setup.
How to create the PHP script
To provide something to use for the AJAX, you do need a simple PHP application. The example performs simple addition. You send it two numbers, it adds them together, and then it returns the result. Here’s the code needed to perform the task:
<?php // Get the values from the query. $val1 = $_GET["val1"]; $val2 = $_GET["val2"]; // Perform the math operation. $result = $val1 + $val2; // Output the result. echo $result; ?>
The <?php line is standard in every PHP application. It tells the PHP interpreter to expect some code. If you really want to know more about PHP, try the tutorial.
The first step is to retrieve data from the URL that the client sends to the server. The data is in the form of a query, and it contains two variables, val1 and val2. The example assumes that val1 and val2 are numbers, but if the client sends something else, the result will be a value of 0 because you need two numbers to perform addition.
The next step is to perform the math. The code creates $result, which contains the sum of $val1 and $val2.
The final step is to output $result. The call to echo works pretty simply. When a client calls using AJAX, it receives $result as a response. However, you can also test this script by using your browser:
Save the file as DoMath.php in the Apache2.2htdocs directory on your system.
Open your browser and type http://localhost/DoMath.php?val1=1&val2=2 in the address bar. Press Enter.
You see 3 as the output.
Try other values out and you find that the program always returns the value you expect.
Try values that won’t work, such as the strings Hello and There for the values, and you find that the program outputs a 0 rather than crashing.