Building Browsergames: Using Configuration Files (PHP)

One of the things that you will encounter while building a Browsergame is the fact that you need to connect to a database. There’s just no way around it – if you plan to store the amount of data that a browsergame needs, you will need a database or some sort of storage layer in order to keep it all in one central location.

And each time you connect to the database, no matter how you do it, you will need to pass in the settings to connect to the database.

At the moment, we only have a basic registration page set up, so that’s not really a big deal. But what happens when you have a sprawling browsergame, spread out accross 75+ pages, and you suddenly need to change your database password?

It becomes a bit of a pain, that’s what. And that’s why you can benefit from using what’s known as a configuration file to store any information like database settings that you’ll need to refer to from multiple pieces of code.

If you want to use a configuration file in PHP, one of the easiest ways is to just create a file that sets some variables, and then just use the require_once() function to include it into your script. Here’s what a typical PHP configuration file would look like:

1
2
3
4
5
6
<?php
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'password';
$dbname = 'db_name';
?>

And then to use the above configuration file inside your PHP code, you would save the file as config.php, and then add the line below to any code you wanted to have use that configuration file:

require_once 'config.php';

And then, within your main code(whatever it might be), you could refer to $dbhost and all the other configuration variables as if you had set them within your code already. Here’s what it might look like in our registration page code:

8
9
10
			require_once 'config.php';
			$conn = mysql_connect($dbhost,$dbuser,$dbpass)
				or die ('Error connecting to mysql');

And that’s all there is to it! Now your database settings will be stored in just the one central area, and updating them will be a breeze.

Luke is the primary editor of Building Browsergames, and has written a large portion of the articles that you read here. He generally has no idea what to say when asked to write about himself in the third person.

Tuesday, April 22nd, 2008 buildingbrowsergames, code, design, php
  • I find PHP's parse_ini_file an efficient and very handy method to use for storing configuration as you can split settings up into multiple sections and such as you add more settings.

    ~ Adam
  • Hi Adam,

    Thanks for pointing out parse_ini_file - seeing as we've already done all of our configuration work for now, we probably won't be using it in this project - but it's on my list of things to use for the next one.
  • You have really written a wonderful tutorial, which contains just what I need! Thanks for linking every php code to the right page, this time it helped me easily figure the difference between require and require once, and require and include! Which I both did not know before (the differences)
    Thanks!
  • Sean
    What happens when the engine fails and someone sees all the information on this page?
  • Hi Sean,
    What do you mean by 'fails'? As long as you've written an .htaccess rule to
    disallow users to view config.php, it should be virtually impossible for a
    user who doesn't have access to the web server to read your configuration
    file.
  • ricardokb
    I have already created the "login.php" and "register.php" : both woring 100%
    but I still can“t understand the "config.php" file.

    I dont really get what shall I type on the "config.php" file.

    Thanks, and great tutorial!
  • You use config.php to store configuration values that both scripts
    will need to use - for now we're just storing database settings, but
    as your game grows you may find yourself needing to store more and
    more values related to configuring your game - config.php is how we
    solve that problem.
blog comments powered by Disqus

About

Building Browsergames is a blog about browsergames(also known as PBBG's). It's geared towards the beginner to intermediate developer who has an interest in building their own browsergame.
WooThemes - WordPress themes for everyone Dreamhost

Got Something to Say?

Follow Building Browsergames on Twitter!