Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Multiplayer game using PHP and mysql

  1. Mar 30, 2007 #1
    I am designing a map game, using PHP and MYSQL. I don't know how to make the players who sign-in to the website to see other players who are also connected to the site and be able to chat with one another.

    I want to design the game in such a way that 2 players can play with each other and be able to send messages during the game while others groups are playing at the same time.

    I have designed the map game successfully, but the problem is making the player 1 who log-in to site to see the player 2 who will also log-in and both can get connected to play each other.

    I will appreciate your responses.
  2. jcsd
  3. Apr 2, 2007 #2


    User Avatar
    Science Advisor

    You have many options. For example, for the chat feature you can store all messages in a database. On the client side you would then make AJAX requests every X seconds to a PHP script which checks the database for new, unread messages and returns them, if any.
    You might be able to just store the messages in the application's memory space, rather than in a Database, using Application Scope variables or Session variables.
    If you go with the AJAX option i recommend a "Reverse Ajax" approach to avoid lots of unnecessary traffic.

    Besides a PHP/Ajax implementation you also have the option of using a Java applet or a flash movie. With either of these two you are able to use socket connections. You would make a socket connection to your PHP script (or possibly your own custom server) to obtain the unread messages

    You are also able to use Javascript with IFrames instead of AJAX as well.
  4. Apr 2, 2007 #3
    Job thanks for the response,
    please can you help with a sample code just for illustration.
  5. Apr 2, 2007 #4


    User Avatar
    Science Advisor

    Sorry but i really don't have the time to write sample code for a 3 layer app.
    Why don't you start by creating a MySQL database with a table for storing messages (i.e. the table will need fields for Message Body, Sender, Receiver, DateSent, HasBeenRead, etc).

    Then create a PHP script which captures variables (from the QUERYSTRING or FORM collection). You'll use these variables to tell the PHP script what to do.
    i.e. myscript.php?action=getUnreadMessages&user=fosh
    Which would retrieve the unread messages for user fosh. The PHP script would connect to the database and do a select of all rows in the Messages table which have been sent to user Fosh and which have not yet been read.

    Once you have that set up i can help you more closely with the (forward) AJAX portion. You'll use javascript's setTimeout to perform Ajax requests every x seconds to myscript.php with the necessary querystring variables.

    The php script will return the messages, which are returned from the Ajax request and finally displayed to the user with Javascript.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Multiplayer game using PHP and mysql
  1. Php & mysql search (Replies: 6)

  2. PHP Web-based game (Replies: 3)