Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8801773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:55:12+00:00 2026-06-14T00:55:12+00:00

So i have been trying to make my canvas game work in real time

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:55:15+00:00Added an answer on June 14, 2026 at 12:55 am

    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

    <?php
    $varA = "I'm var A";
    echo $varA;
    

    b.php

    <?php
    echo $varA;
    

    to run script a.php you direct the browser to http://example.com/a.php. Output is:

    I’m var A

    However going to http://example.com/b.php will print a notice saying

    Notice: Undefined variable: varA in /path/to/webroot/b.php
    on line 2

    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

    <?php
    include 'a.php';
    echo $varA;
    

    output:

    I’m var AI’m var A

    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

    <?php
    if (isset($_GET['c']) {
        $varC = $_GET['c'];
    } else {
        $varC = 'NONE';
    }
    echo $varD;
    

    Going to http://example.com/c.php will output

    NONE

    Going to http://example.com/c.php?c=something will output

    something

    Passing a variable from d.php to c.php. You can use a GET request.

    d.php

    <?php
    $varD = urlencode("i'm from d");
    echo "<a href=\"http://example.com/c.php?c=$varD\">pass value</a>";
    

    or

    header('Location: http://example.com/c.php?c='.urlencode("i'm from d"));
    

    Going to d.php and clicking pass value will output

    i’m from d

    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)

    <?php
    if (!isset($i)) {
        $i = 0;
    }
    ++$i;
    

    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

    0
    

    e.php

    <?php
    session_start();
    if (!isset($_SESSION['i'])) {
        $_SESSION['i'] = 0;
    }
    ++$_SESSION['i'];
    
    $ii = file_get_contents('ii.txt');
    ++$ii;
    file_put_contents('ii.txt', $ii);
    
    echo "session counter: " . $_SESSION['i'];
    echo '<br/>';
    echo "file counter: " . $ii;
    

    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)

    <?php
    //Our Data
    $dataArray = array('foo' => 'some data', "bar" => 42);
    
    // Data convertion into URL parameters -> foo=some%20data&bar=42
    $data = http_build_query($dataArray);
    
    //extract the parts of the url 
    $url = parse_url("http://server.com/b.php");
    
    $host = $url['host']; //server.com
    $path = $url['path']; //b.php
    
    $fp = fsockopen($host, 80, $errno, $errstr, 30);
    
    if ($fp) {
    
        //HEADERS
        fputs($fp, "POST $path HTTP/1.1\r\n"); //POST method
        fputs($fp, "Host: $host\r\n"); //The host
        fputs($fp, "Referer: myApp\r\n"); //who's the referer
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); //Content type: a form post that is url encoded
        fputs($fp, "Content-length: ". strlen($data) ."\r\n"); //data length (size in chars)
        fputs($fp, "Connection: close\r\n\r\n");
        //DATA
        fputs($fp, $data);
    
        $result = '';
        // Request result
        while(!feof($fp)) {
            $result .= fgets($fp, 128);
        }
    } else { 
        // Something went bad
        echo "ERROR: $errstr ($errno)";
    }
    // Socket close
    fclose($fp);
    
    //SUCCESS
    // split the result header from the content
    $result = explode("\r\n\r\n", $result, 2);
    $header = isset($result[0]) ? $result[0] : '';
    $content = isset($result[1]) ? $result[1] : '';
    
    echo "HEADER: $header<br><br>";
    echo "CONTENT:<br>$content";
    

    b.php (located at server.com)

    <?php
    header('Content-type: text/plain');
    if (isset($_POST)) {
        file_put_contents('data.txt', $_POST, FILE_APPEND);
        file_put_contents('data.txt', PHP_EOL, FILE_APPEND);
        print file_get_contents('data.txt');
    
    } else {
        echo "NOT OK";
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to make this work for a long time now. In
I have been trying to make a Cross-platform 2D Online Game, and my maps
I have been trying to make it work with either fwrite or fputcsv but
I have been trying to make it so that when i right click a
I have been trying to make a updateable grid.Which means i show an icon
I have been trying to make radio buttons without the dot which toggles. I
I have been trying to make something work with JQuery. I have the following
i have been trying to make a bouncing ball animation.I have got everything right
I have been trying to make a decision which approach to take to build
I have been trying to make my Mac application enter fullscreen now for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.