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:

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

			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.

Wish there was more?

I'm considering writing an ebook - click here.


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
  • Pandora

    Hullo everybody,
    sorry for my English, I'm foreign. Since a
    browsergame is a really complex project I'd recommend to use a flexible
    database class... on other pages related to a users/monsters stats, if
    I'm right, someone mentioned the DRY principles... If u use a class
    there can't be any repetition anymore... ;)
    I always use the same
    class in my projects cause it is very handy and can be recycled for
    everything... here is a shorter version and only a tiny part of it for
    you as an example... I hope I made no mistake cause I only wrote it only
    for this comment and couldn't test it... But you might see which
    direction it gives you...

    class databaseConfiguration {

    protected $dbhost;
    protected $dbuser;
    protected $dbpass;
    protected $dbname;
    protected $connection;
    public $errormessage;

    public function __construct($databasehost = "localhost", $databaseuser =
    "root", $databasepass = "", $databasename = "project") {
    $this->dbhost = $databasehost;
    $this->dbuser = $databaseuser;
    $this->dbpass = $databasepass;
    $this->dbname = $databasename;

    public function db_connection() {
    $this->connection = @mysqli_connect($this->dbhost,$this->dbuser,$this->dbpass);
    $this->selection = @mysqli_select_db($this->dbname);

    if ($this->connection == false) {
    $this->errormessage = "blablabla";

    if ($this->selection == false) {
    $this->errormessage = "blablabla";

    // ... everything else related to the databases and which might be used more than twice could be added as another method,...

    you just need to include it from an external file as Luke figured out and call to the class and the method with...

    $db_conn = new databaseConfiguration("","","","");

    I used optional parameters so you can also use it for every database you like...
    You can make flexible methods for select, update, insert, too... ;)

    Hope this hint was useful....

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

  • 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

  • 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)

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

  • 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

blog comments powered by Disqus


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.


Got Something to Say?

Send an e-mail to, or get in touch through Twitter at