PHP Multiplayer game using PHP and mysql

  • Thread starter Thread starter fosh
  • Start date Start date
  • Tags Tags
    Game Mysql Php
Click For Summary
The discussion focuses on developing a map game using PHP and MySQL, specifically addressing the challenge of enabling players to see and chat with each other while playing. Key points include the need for a chat feature that allows real-time messaging between players. Suggestions for implementation include storing messages in a database and using AJAX to periodically check for new messages. An alternative approach involves using application memory or session variables to manage messages. Additionally, options such as Java applets or Flash for socket connections are mentioned, although AJAX is recommended for its efficiency. The conversation also emphasizes the importance of setting up a MySQL database to store messages with relevant fields and creating a PHP script to handle message retrieval based on user queries. The use of JavaScript's setTimeout for regular AJAX requests to fetch unread messages is highlighted as a crucial part of the implementation.
fosh
Messages
2
Reaction score
0
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.
 
Technology news on Phys.org
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.
 
Job thanks for the response,
please can you help with a sample code just for illustration.
 
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
13
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
10
Views
21K
Replies
4
Views
3K
  • · Replies 33 ·
2
Replies
33
Views
7K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
12
Views
2K