What are apikey.php and db_credentials.php?

We’ve had a few questions about the purpose of apikey.php and 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.

apikey.php

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 /apikey.php

$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.

db_credentials.php

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.


7 Responses to “What are apikey.php and db_credentials.php?”  

  1. 1 Remco

    Hi all, Im reading your book, and I’m trying to make sense of your explanation of the use the apikey.php. Im sorry to say, but I dont understand it.

    Two questions:
    1) what does the complete code of the apikey.php look like?
    2) how does this $_SERVER[’DOCUMENT_ROOT’] thing work? Do I have to edit it?

    Thanks in advance.

  2. 2 Cam

    Hi Remco,

    Before I answer your questions, your comment led me to find a mistake in my post. I had incorrectly stated that apikey.php contained a single variable declaration of $apikey. I missed the underscore. It should have been $api_key and is now corrected above.

    Your questions:
    1) The complete apikey.php file is the single line of code from above plus the PHP enclosures <?PHP and ?> at the start and end of the file respectively.

    2) $_SERVER is a super global and is created by PHP at the start of your script. It is pre-populated with various server related information like which operating system and web server software PHP is running under. It also contains the full path to the start of your web document root relative to the start of the web server’s file system. This array element is called ‘DOCUMENT_ROOT’ and does not need to be changed. It should work in all cases.

    I hope this helps.

  3. 3 Matt

    Hi,

    I had a little different problem getting this to work, partly because I’m new to PHP and partly because I’m running under IIS.

    I had to change the syntax of the URL in listing 2-2 from this
    key=

    …to this:
    key=

    Also, when including the apikey.php file, IIS didn’t know what DOCUMENT_ROOT was, so I had to use a different path.

    Just thought I’d pass along what I’d learned in case someone else encounters the problems I did.

    -Matt

  4. 4 Matt

    Hi,

    I had a little different problem getting this to work, partly because I’m new to PHP and partly because I’m running under IIS.

    I had to change the syntax of the URL in listing 2-2 from this
    “key=”

    …to this:
    “key=”

    Also, when including the apikey.php file, IIS didn’t know what DOCUMENT_ROOT was, so I had to use a different path.

    Just thought I’d pass along what I’d learned in case someone else encounters the problems I did.

    -Matt

  5. 5 Kevin

    What is the fourth line of db_credentials.php script?

    This line is missing from the image you pasted above. Please let us know or correct that above image.

    Thank you very much,

    Kevin.

  6. 6 Cam

    Hi Kevin,

    The fourth line is the one defining $db_db (the database name for the MySQL database). However, most of our code has a hard coded value instead of this paramter for this — which was an oversight in the rush to publish.

    Also there is no image above. It’s a code listing with CSS styling. The code is in there for you to see, maybe your browser is doing something silly to cause you to not see the 4th line?

    Cam

  7. 7 Ken

    I’m having trouble with the dbcredentials.php. It’s simply displaying the entire text of the database credentials, rather than connecting to the database. Any ideas why this is happening?


Buy Our Books!

(Here's Why) PHP book Rails book DOM book mashups book