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. https://i.sstatic.net/YyCPG.png

I will appreciate your responses.

  • \$\begingroup\$ Make tables in database (2) for active players, store their properties; properties which capture necessary gamestate. In PHP get/set data from/to database + needed processing based on requests (2). I would use ajax to do these requests (1), based on player's actions, timers ... js -> php "joe says hi" ... php-> database "add this message" ... php->js "message added" \$\endgroup\$
    – user712092
    Commented Oct 12, 2011 at 2:55
  • 1
    \$\begingroup\$ @user712092 don't write answers in a comment... \$\endgroup\$
    – bummzack
    Commented Oct 12, 2011 at 8:28
  • \$\begingroup\$ How is it possible for a question to have more than 10 thousand views and only two votes? Is this a one-off, or is this the norm for this site? Additionally, would someone with 1k+ rep mind clicking the one and letting me know if there are any other votes canceling each other out? This can't be all of them, can it? \$\endgroup\$
    – Nate T
    Commented Nov 1, 2021 at 7:00

2 Answers 2


Buttons (controlls) and graphics handle on browser. With javascript, there is lot of drawing libraries, well, even game libraries (craftyjs, gameQuery ...).

State a current game has will be on server in a database.

table players {id, name, score, actual_game_id ...}
table games {id, name, player_which_is_on_move_id, ... remaining_time (for example) ... }
table card {id, game_it_belongs_to_id, which_player_has_him_in_hand_id, value ...}
table message {id, game_where_it_was_sent_id, player_who_send_it_id, text, ...} 

I derived this from thinking about entity relations (may contain mistakes):
player can play only one game, game has N players
card belongs to only one player, player can have N cards
card belongs to only one game, each game has its own cards (so they are not mixed btw games)
only one player is allowed to move at a time

Communication between player and server is trough AJAX. Player side is coded in javascript, server side in PHP (or similar) + SQL. One problem is that You have to periodically ask/query (chat made by periodical query in AJAX) server whether something new happened (server can not contact You trough AJAX); there might be a way to let server contact You trough something else than polling with AJAX, but I do not know what it is.

  • A player sends server messages like "can I play?", "my username and password is ...", "I am moving this card", "I am sending this message to a chat window > hi folks, this game rocks!" ...
  • Server determines whether and how to change database (add message to message table, save the move to tables if it is valid).
  • Player periodically queries server "has something new happened", for example when he is waiting for move of a other players.

You have to figure out rules of communication (communication protocol). Also there is a chance, that some of javascript game libraries has methods to help You handle communication.

  • 1
    \$\begingroup\$ You often can get a speed boost by saving the chat in a flat file and only syncing that with the database every few new messages. Or never sync it if you don't need the chat to persist. You can then limit the flat files to ~100 lines each. Have one file per 'room.' \$\endgroup\$
    – DampeS8N
    Commented Oct 12, 2011 at 18:52
  • 1
    \$\begingroup\$ I wish I had 15 reputation to vote you :D Thank you \$\endgroup\$ Commented Oct 12, 2011 at 20:04

In SQL, one method could be:

If a player can be in more than one game at a time, you'll need to use a Games table which houses all of the current users mapped to a game_played identifier

If a player can only be in a single game at a time, make a field for the Player table that states what game_played ID each is currently attending.

Search for game_played ID's to help pair players together and reference the data to each other.

As for game state and messages, you can use tables and simply reference the game_played ID to help sort this out.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .