Running Linux, 5th Edition - Matthias Kalle Dalheimer [456]
LoadModule php4_module libexec/libphp4.so
AddModule mod_php4.c
AddHandler application/x-httpd-php .php
You may also want to find your existing DirectoryIndex line and change it to allow PHP files to be used as default pages:
DirectoryIndex index.html index.php
Now restart Apache:
owl# apachectl restart
(The command apachectl may be called differently on your distribution; try rcapache.) Once the server is restarted, you should test whether the PHP4 module can be loaded correctly. You can do this by writing a small PHP program, such as the following:
phpinfo();
?>
Save this file as phpinfo.php in the htdocs directory of your Apache installation (often /usr/local/httpd/htdocs). Now you should be able to browse this file with your web browser by accessing http://localhost/phpinfo.php. If everything is OK, you should see the configuration of the PHP4 module.
The LAMP Server in Action
Now you have all the components for your LAMP server in place; it is time to run a few examples.
If you haven't done so already while following the last section, we suggest that you test your setup now with a very simple PHP file. Save the PHP that you have seen in the last listing in the previous section into a file called info.php.
Now place this file in the directory where your Apache web server is looking for its contents files. Often, this is /usr/local/httpd/htdocs, and it may already contain the files that your distribution has installed for you during installation (at least if you have installed Apache from the installation media). If this doesn't work for you, you should look for the Apache configuration file httpd.conf. Often, this file is in the /etc/httpd/ directory, but if this is not the case on your system, you can search for it with
locate httpd.conf
In this file, look for the line starting with DocumentRoot. You should find a directory listed here, and a subdirectory named htdocs should be under that directory; put the file info.php here. Now you can use any web browser to access the URL http://localhost/info.php. This will give you some information about the setup of your PHP module.
PHP comes with a number of built-in functions that manipulate and manage the data stored in MySQL (and other databases).
A relational database consists of a number of tables. If you have sufficient access rights, PHP can query and manipulate data in these tables. We can now write a few PHP scripts to use the database tables. We assume here that you have created the database test_database and the table comment_table, as well as the user olof as described earlier.
Use your favorite text editor and enter the following code, which creates a small HTML page that lets you add data to this table by means of an HTML form:
if (isset($_REQUEST["comment"])) {
$conn = mysql_connect("localhost", "olof", "secret")
or die("Could not connect to MySQL as olof");
mysql_select_db("test_database", $conn)
or die("could not select the test_database");
if (get_magic_quotes_gpc()) {
$comment = stripslashes($_REQUEST["comment"]);
} else {
$comment = $_REQUEST["comment"];
}
$query = "INSERT INTO comment_table VALUES ('0', '"
. mysql_real_escape_string($comment) . "')";
mysql_query($query)
or die(mysql_error());
}
?>
When you work with a database, you must take precautions not to allow user input to manipulate your SQL queries. If you don't do this, a malicious user could simply hijack your database. You can make yourself safe by transforming the input data before using it to construct SQL queries. Normally, it is enough to put user input through the mysql_real_escape_string() function. In some situations, you may need to apply the stripslashes() function first. This is because of the special PHP feature called magic_quotes_gpc, which was meant to make all input data safe for the database automatically. Although the idea was noble, the feature does not