Lab Notes Six: Installing PHP.

Is CGI easy to program? Is it powerful?

Are there any other things out there, alternatives to it?

Server-side programming. How about PHP?

PHP started out as a CGI script (written in Perl).

It now can be installed as a standalone CGI script and as an Apache module.

The first one is more portable. The second more efficient.

We install it as a standalone CGI script.

Here's how we do it:

1. Get the distribution from

http://www.php.net (bookmark the site)
Shortcut: take /u/dgerman/public/php*.tar.gz from my account.

2. Create a directory

/u/dgerman/php-download
and place the compressed archive there.

3. Uncompress the archive (use gunzip).

4. Unarchive the .tar file.

(Use tar xvf *.tar)

5. Delete the .tar file.

6. Create a directory

/u/dgerman/php-install/bin
where the binary will be installed.

7. Go into the directory where your archive was unpacked.

For me that's /u/dgerman/php-download/php-3.0.18 as expected.

8. Run configure --help | more to see options.

When ready to install for good run as configure as follows:

./configure \
  --prefix=/u/dgerman/php-install \
  --enable-discard-path \
  --with-mysql=/l/mySQL 
9. Check the manual (here) for why we need the second switch.

10. Type make and wait for the compilation and linkeditting to be done.

11. Type make install and you're ready to go (almost).

You now have the PHP interpreter ready to go, but Apache has no idea about it.

12. Add (or change) the following three lines to httpd.conf:

ScriptAlias /php3/                   "/u/dgerman/php-install/bin/"
AddType     application/x-httpd-php3 .php3
Action      application/x-httpd-php3 "/php3/php" 
Note: For each it's easy to find the right section in the .conf file if you read the comments.

13. Restart your server. For example:

/u/dgerman/apache/apache_1.3.14/bin/apachectl graceful
13. Now you need some scripts. Here's the simplest one:

burrowww.cs.indiana.edu% pwd
/nfs/paca/home/user1/dgerman/php-install/bin
burrowww.cs.indiana.edu% ls -ld p1*
-rwxr-xr-x   1 dgerman  faculty        61 Jun  4 13:11 p1.php3
burrowww.cs.indiana.edu% cat p1.php3
#!/u/dgerman/php-install/bin/php
<? echo "Hello World!"; ?>
burrowww.cs.indiana.edu% 
(Note the location, and the contents, as well).

14. Here's a bigger, shorter one:

#!/u/dgerman/php-install/bin/php
<? phpinfo() ?>
15. How do you access them?

Mine are here and here.

16. There are many books on PHP.

Let's look at an example:

burrowww.cs.indiana.edu% cat p3.php3
#!/u/dgerman/php-install/bin/php
<?
 echo "Hello World!";
 echo "How have you been?"; 
?> 

<hr><img src="http://www.cc.columbia.edu/low3.gif"><hr>

<? for ($i = 0; $i < 10; $i++) { ?>

  This is a test. <br> 

<? } ?>
burrowww.cs.indiana.edu% 
Try it here.

17. Here's another one.

#!/u/dgerman/php-install/bin/php
<html>
  <head>
    <title>PHP Example</title>
  </head>
  <body>
    You are using 
    <blockquote>
          <? echo $HTTP_USER_AGENT ?> 
    </blockquote>
    and you are coming from 
    <blockquote>
          <? echo $REMOTE_ADDR ?> 
    </blockquote>
  </body>
</html>
Try it here.

Again, the colors are supposed to help you.

18. Here's another one.

#!/u/dgerman/php-install/bin/php
<html>
  <head>
    <title>Form Example</title>
  </head>
  <body>
    <h1>Form Example</h1>
                            <? 
                               function show_form($first="", $last="", $interests="") { 
                                 $options = array("Sports", "Business", "Travel", "Shopping", "Computers" );
                                 if (empty($interests)) 
                                   $interests = array(-1); 
                            ?> 

    <form action="/php3/p5.php3" method="post">    
      First name: <input type="text" name="first" 
                   value=  "<?echo $first?>"
                  ><br> 
      Last name: <input type="text" name="last" 
                   value=  "<?echo $last?>"
                  ><br>
      Interests: 
      <select multiple name="interests[]">
                            <?
                                 for ($i = 0, reset($interests); $i < count($options); $i++) {
                                   echo "<option"; 
                                   if (current($interests) == $options[$i]) {
                                     echo " selected "; 
                                     next ($interests); 
                                   }                                                           
                                   echo "> $options[$i]";
                                 }
                            ?>
      </select> <br> <input type="submit">
      </form>
                            <?
                               }
                                                       
                               if (!isset($first)) {
                                 show_form(); 
                               } else { 
                                 if (empty($first) || empty($last) || count($interests) == 0) {
                                   echo "You did not fill in all the fields, please try again.<p>"; 
                                   show_form($first, $last, $interests); 
                                 } else {
                                   echo "Thank you, $first $last, you selected " . 
                                        join($interests, " and "), " as your interests.<p>"; 
                                 }
                               }
                            ?>

  </body>
</html>
Here I added a second dimension to the color cues, by shifting the code in page.

Try it here.

19. Finally, see if this one works for you.

#!/u/dgerman/php-install/bin/php
<html><head><title> Calling MySQL </title></head><body>
<? 

  $db = mysql_pconnect("localhost", "a348", "a348AG") 
    or die ("Could not connect"); 

  print "Connected successfully."; 

?> 

</body>
</html>
This one will help us build our first shopping cart.


Last updated on Oct 2, 2001, by Adrian German for A348/A548