Read/Write data to a web site

Incredibly newb question - I’m really not up on web stuff; I’ve been googling all day what I thought would be an easy question, can’t find the answer.
I want to read and write some data to my web server (shared commercial hosting, not my own computer). Obviously reading data is easy, but how does one write data without opening up the file system to hackers? I get that I probably need some server side app or script to make this work, but I can’t find an example.
A basic tutorial on safe read/writing of files to a web server is what I’m looking for.
Help!

What do you want to accomplish?

Reading files is simple. You need the url, you download the url with a socket. Then you have the data from the url.

You could use ftp for instance for uploading. Which can be done with CURL.

I want to write to a file on a web server from within a Xojo program. Just like I’d write to a local file, but it’s sitting on a server on the internet.
Not really looking to upload a file or use ftp, just looking to write a new file or append to it, just like I would locally.
Any suggestions?

You can’t write to the server because this is a security risk. There are ftp, CURL, scp, ssh.

You might be able to use it like a local computer with a VPN (if it is for yourself and trusted users), but Beatrix is right, FTP or Curl are much better suited.

Maybe I’m missing something… Is this not commonly done?
What’s the normal way a piece of software would store and retrieve data from a file stored on a remote server safely?

there’s some kind of server side app providing an api etc

I figured that, but I can’t really seem to figure out how that’s done. Any suggested links?

It could be as simple as a small app that listens on a port
Then you have a desktop app connect to that server on that port and send that data to it to write
Since that app is running on the server when IT writes its writing to some location on the server

But note that a simple app sitting there listening is HORRIBLY insecure

So Beatrix earlier suggestion of a Xojo Web app that handles a custom URL which maybe requires a user id password etc makes sense
Shared hosting may preclude that as it would require binding to a specific port and thats often restricted on shared hosts

1 Like

Ok, turns out this isn’t too hard. Not sure why I had so much trouble finding any “how to’s” on the 'net!

<?php
$file = 'Filewritedata.txt';

// The new data to add to the file
$data = date('Y-m-d H:i:s') . ' var1 = ' . $_GET['var1'];

// Write the contents to the file, 
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $data . "\n", FILE_APPEND | LOCK_EX);
//file_put_contents($file, $data . "\n", LOCK_EX);

// Read back the data and display it
$filedata = file_get_contents($file);
echo nl2br($filedata);
?>

You can go to https://www.checkcheckonetwo.com/state/Filewritetext.php?var1=Hello to try it yourself.
The data file cannot be read directly by the outside world either. (You can test that by going here: https://www.checkcheckonetwo.com/state/Filewritedata.txt

I just have to figure out how to pass more complex data than just a simple string, but that shouldn’t be too hard!

JSON / XML are both “strings” but can represent much more complex structured data

Of course. I plan to use JSON, but I mean passing the data to this script within the URL.
Not sure of another way to pass data to a php file, certainly via URL is the easiest!

ah yes URL’s do have limits

posting more data might require something more complex than JUST a url
Pretty sure you want to “post” data and then the body of the http request can contain the data (xml, json, etc etc etc)

And now we’re back to needing some kind of “server” app as opposed to just a php script

Tim Dietrich gave the Xojo world this
https://aloe.zone
@HalGumbert might be able to give you pointers

Thanks! I expect that’ll need an update when Web 2.0 comes out?

100%
And since Tim has left the Xojo world I have no idea how/if that will happen unless some one else adopts it and takes over stewardship

You can (arguably should) POST larger items like JSON, rather than pass them through the GET parameters of the URL.

Thanks, Tim.
How would I POST the data to my php script instead grabbing it from the URL?
That is to say, how do I pass the data from the client to the php script on the server?

You would use SetRequestContent https://docs.xojo.com/URLConnection.SetRequestContent

Ok, just gotta figure out how to piece this together… Lemme know if I’ve got this correct:

Client:

Var mySocket as New URLConnection
Var json As New JSONItem
json.Value("ID") = 123456
mySocket.SetRequestContent(json.ToString, "application/json")
mySocket.Send("POST", "http://127.0.0.1:8080/GetCustomer")

Server:

<?php
   // collect value of POST data
   $data = $_POST['ID'];
   echo $data;
?>

You can use the $_POST superglobal if you build the form text like in the second example of the Xojo docs. Yes, you do build it just like you would the GET parameters string!

To use JSON like your example we will need to get the whole POST body. I’ll be honest, I had to look up how to do that in PHP, but I found an answer here https://stackoverflow.com/a/8945912/6047977

Using that answer as a guide, this would be the very basic PHP end for the Xojo code you wrote:

<?php
$entityBody = file_get_contents('php://input');
$postData = json_decode($entityBody, true);

echo $postData["ID"];
?>

I urge you to look into how to properly sanitize the input for your own safety!