So i have been trying to make my canvas game work in real time multiplayer with long polling right now which connects to my mysql database, but I am now trying to switch to web sockets. I am a little confused on where the websockets in storing information and on how it is being organized when it is stored. Do the websockets connect to a mysql server? Does the information stored using websockets reset when the server resets? any help is appreciated. Thanks
Share
DISCLAIMER: This answer is directed to the OPs comments. The scripts presented are, by no means, secure. It’s preferable to use a third party FW like Ratchet
I think you’re misunderstanding how PHP works. Let me give you an example and build some explanation from that…
You have a webserver(example.com) with 2 files: a.php and b.php.
a.php
b.php
to run script a.php you direct the browser to http://example.com/a.php. Output is:
However going to http://example.com/b.php will print a notice saying
Why is that?
It’s because both script are totally independent. They dont’ even know of each other existence.
Now let’s change b.php a bit:
b.php
output:
This basically tells b.php to include a.php, thus “sharing” variables, objects, class and function definitions.
POST and GET
Another way to pass data between scripts is using POST or GET.
c.php
Going to http://example.com/c.php will output
Going to http://example.com/c.php?c=something will output
Passing a variable from d.php to c.php. You can use a GET request.
d.php
or
Going to d.php and clicking pass value will output
Instead of using GET you can do a POST request. (we will cover that later)
$_SESSION
What about between “accesses”?
Each time you access a php file, the script is run from the beginning to the end.
Here’s another file (e.php)
This script tells you that if var $i is not defined, $i=0 and then increments it by one.
Accessing http://example.com/e.php will ALWAYS output 1. No data is stored between accesses.
Unless… you use the $_SESSION variable (or store the data in a persistent media).
ii.txt
e.php
Each time you access e.php, both counters will increase.
BUT… $_SESSION variable is not a persistent media. When the session is destroyed (or expires), session counter will reset. However, the file counter will always increase, so it’s a persistent media. You can, of course, use a database to store the variable. The principle is the same.
Passing variables between different servers:
The principle is the same when passing variables between file in the same server. However, you can’t (normally) include or require a php file located in another server. Moreover, it is best when sending information from two locations to secure the data. Here’s an example using a socket connection.
a.php (located at client.com)
b.php (located at server.com)
when going to http://client.com/a.php a POST request is sent to b.php. If it is successful, b.php stores the data in a file called data.txt and returns the contents of that file.
Hope this helps understanding sockets and PHP.