PHP How to create a online multiplayer games with mysql support using php ?

AI Thread Summary
To create an online multiplayer game with real-time opponent moves using PHP and MySQL, integrating client-side technologies like JavaScript and AJAX is essential for efficient communication without page refreshes. Setting up a MySQL schema involves creating tables for user information and messages to track moves and interactions. Server-side scripts in PHP, such as login and message retrieval scripts, are necessary to manage user sessions and fetch unread messages. For more complex games, especially 3D ones, transitioning away from browser-based solutions to dedicated applications using languages like Java or tools like 3DSMax is recommended. Properly managing server-client communication is crucial for maintaining gameplay fluidity and performance.
ngkamsengpeter
Messages
193
Reaction score
0
I want to create a online multiplayer games with php but I don't know how to let the members know their opponents' move realtime ?
Please help me !Thanks
 
Technology news on Phys.org
events or messages?
 
It won't be very efficient using PHP alone. You should have something on the client side like Javascript, Java, or, ideally, Flash. Flash and Java have socket support which is what you want, to avoid refreshing the page or querying the server every so often.
Javascript doesn't support sockets, but with AJAX you can contact the server without refreshing the page, even if you'll probably find yourself querying the server on some time interval, such as every 4 seconds.
 
-Job- said:
It won't be very efficient using PHP alone. You should have something on the client side like Javascript, Java, or, ideally, Flash. Flash and Java have socket support which is what you want, to avoid refreshing the page or querying the server every so often.
Javascript doesn't support sockets, but with AJAX you can contact the server without refreshing the page, even if you'll probably find yourself querying the server on some time interval, such as every 4 seconds.
I will use javascript and ajax also but I don't know hot to show the opponents' move to the user . For example , I create a multiplayer online chess, and how can I show the move of the opponent to the player ? Please provide me some source code .Thanks
 
store the event of the move int a message queue.
 
neurocomp2003 said:
store the event of the move int a message queue.
how ? please provide me the source code or the tutorial link so that I can learn .
 
There's three phases to what you want to accomplish:
1. Set up the schema in MySQL:
This means creating whatever tables you'll need to store your information. You should have a table for your users, an example is:
Code:
Table 1: UserInformation
ID | UserName        | FirstName | LastName | ... etc
-------------------------------------------
1  | Job             | Bobby     |  ...       etc
2  | ngkamsengpeter  | ...       |  ...       etc

Then you should create a table to store your user events. Here, for example, we'll store user messages:
Code:
Table 2: Messages
ID | FromUser | ToUser | Message   | HasBeenRead
------------------------------------------------
1  | 1        | 2      | Hey there | False
2  | 2        | 1      | Hello     | True

The Column "hasbeenread" is used to identify which messages users have received. In the above table i received your "Hello" and sent you my "Hey There" which you are yet to receive.

2. Set up the server side scripts (in your case PHP)
login.php - You should have a login script which authenticates a user and sets a session variable around with his/her username.
getmessages.php - This script retrieves any unread messages for the logged in user. It does this by using the following SQL statement:
SELECT t2.UserName, t1.Message FROM Messages AS t1
JOIN UserInformation AS t2
ON (t2.FromUser = t1.ID)
WHERE t2.ToUser = <Logged in user's id here> AND t1.HasBeenRead = False
Finally getmessages.php prints the messages on the page with a format that enables javascript to easily parse (you could use XML, but for simple information exchange such as what we're dealing with it's a lot more efficient to use some other format, such as <fromuser>~!~<message>***<fromuser>~!~<message> ... etc)

3. Set up the page that will display the new messages as they come.
This page will make use of Javascript and AJAX to load the getmessages.php script. Basically you need to setup XMLHTTP (the heart of AJAX) then create a function "GetMessages()" that uses your XMLHTTP object to load the getmessages.php script, parse its contents and present the messages to the user in a neat way.
Now you can call your GetMessages() function whenever you want to get the latest messages. You can for example use javascript's SetInterval to check for new messages every X miliseconds.
 
Last edited:
-Job- said:
There's three phases to what you want to accomplish:
1. Set up the schema in MySQL:
This means creating whatever tables you'll need to store your information. You should have a table for your users, an example is:
Code:
Table 1: UserInformation
ID | UserName        | FirstName | LastName | ... etc
-------------------------------------------
1  | Job             | Bobby     |  ...       etc
2  | ngkamsengpeter  | ...       |  ...       etc

Then you should create a table to store your user events. Here, for example, we'll store user messages:
Code:
Table 2: Messages
ID | FromUser | ToUser | Message   | HasBeenRead
------------------------------------------------
1  | 1        | 2      | Hey there | False
2  | 2        | 1      | Hello     | True

The Column "hasbeenread" is used to identify which messages users have received. In the above table i received your "Hello" and sent you my "Hey There" which you are yet to receive.

2. Set up the server side scripts (in your case PHP)
login.php - You should have a login script which authenticates a user and sets a session variable around with his/her username.
getmessages.php - This script retrieves any unread messages for the logged in user. It does this by using the following SQL statement:

Finally getmessages.php prints the messages on the page with a format that enables javascript to easily parse (you could use XML, but for simple information exchange such as what we're dealing with it's a lot more efficient to use some other format, such as <fromuser>~!~<message>***<fromuser>~!~<message> ... etc)

3. Set up the page that will display the new messages as they come.
This page will make use of Javascript and AJAX to load the getmessages.php script. Basically you need to setup XMLHTTP (the heart of AJAX) then create a function "GetMessages()" that uses your XMLHTTP object to load the getmessages.php script, parse its contents and present the messages to the user in a neat way.
Now you can call your GetMessages() function whenever you want to get the latest messages. You can for example use javascript's SetInterval to check for new messages every X miliseconds.

That means that I want to store every player moves in the sql database and retrieve it .If my games is very long , that will use a lot of sql database space . Is it any other way of doing this ?Or can you suggest other programming language which is more suitable ?
Besides , if I want to create a 3D online games , what programming language should I use . Can I use Maya ?
 
Naturally you can delete the read messages as they are read. The biggest problem is not the database size but the traffic the game will generate through the AJAX calls. For making web based games you should use Java, Flash or Director. Director ihas a browser plugin from Macromedia (now Adobe) just like Flash but much more powerful (it supports 3D graphics).
But in my opinion, if you want a 3D online multiplayer game, then you should drop the browser and just code it from the ground up. This way you can use much more powerful programs like 3DSMAX. The disadvantage is that you'll need to code both a game server and a game client (which is a lot of fun nonetheless).
 
  • #10
-Job- said:
Naturally you can delete the read messages as they are read. The biggest problem is not the database size but the traffic the game will generate through the AJAX calls. For making web based games you should use Java, Flash or Director. Director ihas a browser plugin from Macromedia (now Adobe) just like Flash but much more powerful (it supports 3D graphics).
But in my opinion, if you want a 3D online multiplayer game, then you should drop the browser and just code it from the ground up. This way you can use much more powerful programs like 3DSMAX. The disadvantage is that you'll need to code both a game server and a game client (which is a lot of fun nonetheless).
Then , how to code the game server and game client ?
 
  • #11
You would code your game server & client the same way you would any other application. You pick a port number for your server/client communication. For the server you build an app that listens on a port for connections. For the client you build an app that connects to your server.
The server has to keep track of connections and route/broadcast messages to users. The key step is to have all this network communication in place with a nice interface so that your game isn't concerned with the underlying communication details. In Java, or any other Object Oriented language, i would build a class "CommManager" that exposes an interface with methods such as "Connect", "Send Message" etc. Finally you would build the game on top of this framework.
I'm not really the best person to tell you about 3D graphics, I've used 3DSMax in the past only superficially.
 
Last edited:

Similar threads

Replies
21
Views
6K
Replies
13
Views
2K
Replies
2
Views
25K
Replies
7
Views
7K
Replies
3
Views
23K
Replies
12
Views
2K
Back
Top