We’ve had a few questions about the purpose of
db_credentials.php in the online source code. In most instances where the online listings don’t match those presented in the book the inclusion of these two files are the only signicant difference. So here’s a quick explanation…
I’ll start with the
apikey.php since it appears in Listing 2-2 just as things start to get interesting.
In chapter 2, after we examine the sample map from Google, the very first thing we do is separate the code from the content. This is a very pragmatic thing to do, and this example (listing 2-2) serves as the foundation of everything we do for the rest of the book.
In a few early examples in the book we included the API key for our development environment to show an example of what the code would look like. However, a few people have tried to run the code verbatim and got errors from Google saying the API key was invalid. This is both true and false. It is invalid for your website, but not for ours.
To correct the problem we moved the API key into a separate file with a single variable declaration. We placed this file in the root folder of our website in
$api_key = "Your API Key";
We then included this file using php and embedded the variable into the API line of listing 2-2.
We use the super global variable
$_SERVER to obtain the root of our website so that we can include the file for any arbitrary directory regardless of its depth in the document tree. This allows us to have a single file for our entire site instead of one for each example or map.
This method has the added benefit of making the transition from a development environment to a production environment easier. When moving your map up to the production server you’ll only need to edit the one file rather than many. Moreover, when you later make changes you can simply exclude the apikey.php file all together when updating the production server with the new code.
We start using the
db_credentials.php file in Chapter 5 in listing 5-3. This modification was made only a few days before the book went to print, and I mistakenly forgot to describe it’s purpose.
Below is the structure of the file as we have it on our server. You can see that it is simply four lines of code, one for each variable declaration.
$db_host = "localhost"; // The server hosting the database $db_name = "googlemapsbook"; // Our username for the database server $db_pass = "longandrandompassword"; // The password for our account $db_db = "googlemapsbook"; // The database name for our account (unused)
The idea behind the
db_credentials.php file is very similar to that of the
apikey.php file. By abstracting the username, password, server and database information into a separate file we do not need to edit multiple files when we move our code into a production environment or from server to server.
We also have the added benefit of keeping this sensitive information more private. For those of you with administrator level access on a unix-like server you can change the ownership of the file to the user or group that runs the webserver, and subsequently remove the world-readable permissions. This means that other people with shell accounts on the server will no longer be able to read this file, but the webserver can still read it and allow your php scripts to use the information inside.
The last note that I’d like to make is that we hard coded the name of the database into our scripts using
mysql_select_db("googlemapsbook",$conn). If you don’t have control over the name of your database, then it would be wise to use the
$db_db variable from the credentials file instead. This is something we’ll change in future editions of the book.
That’s it, I hope this clears things up.