Search Unity

Webgl with PHP server Setup?

Discussion in 'Multiplayer' started by rotmgdarklyfe, Apr 11, 2020.

  1. rotmgdarklyfe

    rotmgdarklyfe

    Joined:
    Apr 11, 2020
    Posts:
    1
    Hey guys,
    I'm hoping someone has been through this before or knows some tutorials I can use to get this going. I built out an application that saves, and reads string data using a locally hosted php server through command line. I now want to have this be accessible anywhere. I've hosted static websites using Amazon's S3 service, but I'm not sure how to get the PHP saving and loading working. I'm assuming I'll need have a dedicated php server. (Amazons EC2 possibly?) I'm just not sure how to start/set this up. Here is the code that I'm using for saving/loading string values. If anyone has any advice let me know. (I know about PlayerPrefabs, but I need this data to be accessible across computers/browsers)

    Code Snippet
    IEnumerator getTextFromFile()
    {
    bool succesful = true;
    WWWForm form = new WWWForm();

    string myWWW = "http://localhost:9000/getleaf" + leafNumber + ".php";
    WWW www = new WWW(myWWW, form);
    yield return www;

    if (www.error != null)
    {
    succesful = false;
    }
    else
    {
    string formToString = www.text;
    seperateString(formToString);
    succesful = true;
    }
    }

    IEnumerator sendTextToFile(string name, string birthday, string siblings, string bio, string leafNumber)
    {
    bool successful = true;

    WWWForm form = new WWWForm();

    form.AddField("name", name + ",");
    form.AddField("birthday", birthday + ",");
    form.AddField("siblings", siblings + ",");
    form.AddField("bio", bio + ",");

    string myWWW = "http://localhost:9000/leaf" + leafNumber + ".php";
    WWW www = new WWW(myWWW, form);

    yield return www;
    if(www.error != null)
    {
    successful = false;
    }
    else
    {
    Debug.Log(www.text);
    successful = true;
    }

    }


    PHP Code for sending data
    <?php
    $text1 = $_POST["name"];
    $text2 = $_POST["birthday"];
    $text3 = $_POST["siblings"];
    $text4 = $_POST["bio"];

    if($text1 != "")
    {
    echo("Message successfully sent!");
    echo("Field 1:" . $text1);
    echo("Field 2:" . $text2);
    echo("Field 3:" . $text3);
    echo("Field 4:" . $text4);
    $file = fopen("leaf1Data.txt", "w");
    fwrite($file, $text1);
    fwrite($file, $text2);
    fwrite($file, $text3);
    fwrite($file, $text4);
    fclose($file);
    }
    else
    {
    $file = fopen("leaf1Data.txt", "r");
    echo fread($file, filesize("leaf1Data.txt"));
    fclose($file);
    }

    ?>

    PHP Code for receiving data
    <?php
    $file = fopen("leaf1Data.txt", "r");
    echo fread($file, filesize("leaf1Data.txt"));
    fclose($file);
    ?>