Include Files
Example 11-3
shows the db.inc
file that is included in each of the gift registry scripts. The
include directive allows the variables and
functions in db.inc to be used by each script
without duplicating the code. Note that the code in include files
must always be surrounded by PHP start and end tags.
<?php
// These are the DBMS credentials and the database name
$hostName = "localhost";
$databaseName = "wedding";
$username = "fred";
$password = "shhh";
// Show an error and stop the script
function showerror( )
{
if (mysql_error( ))
die("Error " . mysql_errno() . " : " . mysql_error( ));
else
die("Could not connect to the DBMS");
}
// Secure the user data by escaping characters and shortening the input string
function clean($input, $maxlength)
{
$input = substr($input, 0, $maxlength);
$input = EscapeShellCmd($input);
return ($input);
}
// Check if the user is logged in. If not, send him to the login page
function logincheck( )
{
session_start( );
if (!session_is_registered("user"))
// redirect to the login page
header("Location: index.php");
}
?>The db.inc include file stores the four
variables that are used in connecting to the DBMS and selecting the
database. The showerror( ) function is discussed
in the previous section. The clean( ) function is
discussed below. The logincheck( ) function is
discussed in Section 11.5.
The include file has an .inc extension, which presents a minor security problem. If the user creates a URL ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access