So I have a PHP script that basically emulates rednet over the internet, but it doesn't work like it's supposed to:
<html><body>
<?php
$rednetData = $_POST['rednetData'];
list($senderid, $recipientid, $message) = explode(":", $rednetData);
if ( $recipientid == null ) {
$id = 1;
$files = scandir('/rednetFiles/');
$filecount = count($files);
for ($file = 0; $file >= 0; $filecount) {
if (file_exists($id)) {
$fh = fopen('/rednetFiles/{$id}', 'w');
fwrite($fh, '{[1]={$senderid}, [2]="{$message}"}');
fclose($fh);
$id = $id += 1;
}
}
} else {
$fh = fopen('{$recipientid}.txt', 'w') or die("can't open file");
fwrite($fh, '{[1]={$senderid}, [2]="{$message}"}');
fclose($fh);
}
?>
</body></html>
When it receives a post request, it doesn't do anything - the Apache logs say it received the request, but it never wrote to a file on the server. I'm pretty sure it's not the computer's end, but I'll post it's code just in case:
local tArgs = { ... }
ip = tArgs[1]
rednetDir = tArgs[2]
if #tArgs ~= 2 then
print("Usage: "..shell.getRunningProgram().." <ip> <dir of files>")
error()
end
local file = fs.open("/rednet", "w")
file.writeLine("ip = '"..ip.."'") --Writes ip
file.writeLine("rednetDir = '"..rednetDir.."'") --Writes dir
file.write[[function open( sModem ) --Function basically tells programs that the modem is opened, even if it is not there
return true
end
function close( sModem ) --Function tells programs that the modem are closed
return true
end
function isOpen( sModem ) --Function tells programs that the modem is open
return true
end
function send( nRecipient, sMessage ) --Sends a POST request to the ip
http.post(ip, os.getComputerID()..":"..nRecipient..":"..sMessage)
end
function broadcast( sMessage ) --Does the same as above, but the server will overwrite all messages
http.post(ip, os.getComputerID..":"..sMessage)
end
function receive() --GET the contents of your ID from the server [Having issues here?]
sHandle = http.get(rednetDir, os.getComputerID()..".txt")
print("Printing raw get data...")
print(sHandle)
print("Trying key-value printing...")
for k, v in ipairs(sHandle) do
print(k..", "..v)
end
print("Printing serialized handle...")
local tRednetData = textutils.serialize(sHandle)
local nSenderID, sMessage, nDistance = tRednetData[1], tRednetData[2], 0
os.queueEvent( "rednet_message", nSenderID, sMessage, nDistance )
return nSenderID, sMessage, nDistance
end
function run() --Wut? I do not understand the point of this but I added it from the original API anyway
if bRunning then
error( "rednet is already running" )
end
bRunning = true
receive()
end]]
file.close()
os.unloadAPI("rednet") --Reloads API
os.loadAPI("rednet") --^
print("Rednet address set to "..ip)
What could be the issue with either of the codes?