Lennaerts PHP mysql multiplayer tutorial

  • 41 favourites


Attached Files

The following files have been attached to this tutorial:



Download now 501.64 KB


9,544 visits, 23,649 views



This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Preparing PHP

We will first set up the basic pages and configuration, so that we will be able to passs information to our php file and recieve information back from the MySql server later on.

In order to connect, you need your MySql Database name, a user with sufficient access to add and delete information from the database, annd of course the users password.

We will be creating 2 files, a config.php file, and a requests.php file.

What I always do is make a sepperate config file and include it. For sake of simplicity I will leave the current config file in the same directory where the PHP file will reside.

With an eye on security it would be wise to move the config file outside of the webroot, and simply have the PHP file include it. This, so pocking wannabe hackers have even less chance of getting your MySql credential details.

A note: always stack up on security for my MySql material, such as a dedicated user for the database, which only has access to that database. If someone gets those credentials, their actions/damage will be limited.

In the first file, config.php enter the following:

    $sql_host = "localhost";  //leave it on localhost
    $sql_user = "mysqlusername";  //change to your username
    $sql_password = "password"; //change to your mysql user password
    $sql_db = "yourdatabasename"; //change to the name of your database

This is your basic config file, this needs to be uploaded to your server.

Next we will create the beginning of the request.php file.

    //The following is crucial, not having this will block request made by applications which are not hosted on the same server.
    header('Access-Control-Allow-Origin: *');  
    //Include the config file, if you placed it outside your webroot, make sure to point to it, ie: ../config.php  (1 folder down)
    include ('config.php');
    //Establish a connection to the database server
    $db = mysql_connect($sql_host, $sql_user, $sql_password) or die('Unable to connect to the database server.');
    //Connect to the database
    mysql_select_db($sql_db) or die('Error connecting to the database.');  ?>

So, this is the basics, it doesnt do anything yet apart from connecting to the database.

Next step, adding game elements and events.


  • Order by
Want to leave a comment? Login or Register an account!