This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Goof's profile picture

Getting $Status = "offline" Or "online" Php

Started by Goof, 28 July 2013 - 03:02 PM
Goof #1
Posted 28 July 2013 - 05:02 PM
Hello!


Im in trouble!

i've made a website which checks if a ComputerCraft server is open / not.

then i've made a CC program as follows:

local chatBox = peripheral.wrap("top")

myUrl = "*hided for security reasons*"

if http then
while true do
  req01 = http.get(myUrl)
  local read01 = req01.readAll()
  local content = read01
  req01.close()
  print(read01)
  for state in string.gmatch(read01, "$status = (.-);" ) do

   if string.lower(state) == "offline" then
	status = "right"
   elseif string.lower(state) == "online" then
	status = "left"
   end
  end
  if status == "right" then
   rs.setOutput(status, true)
   sleep(0.5)
   rs.setOutput(status, false)
  elseif status == "left" then
   chatBox.say("*Server!* is open again! This server will close in 10 seconds! Remember to save your codes!")
   sleep(5)
   chatBox.say("Server close-down in 5 seconds! Save your stuffs!")
   sleep(5)
   chatBox.say("Closing server...")
   rs.setOutput(status, true)
   sleep(0.5)
   rs.setOutput(status, false)
  else
   print("No statement found!")
  end  
  sleep(5)
end
end


but i want it to detect what the :

$status = "online"
variable is… but i dont remember how.


for state in string.gmatch(read01, "$status = (.-);" ) do
how do i detect what the PHP variable is ?


Thanks in Advance :P/>
Engineer #2
Posted 28 July 2013 - 05:49 PM
I would think something like this:

local mode = read01:lower():match( "%$status = (.+);")

But the real problem is that you cannot get the php source code in anyway. Unless you print in the actual, viewable page.
Goof #3
Posted 28 July 2013 - 05:57 PM
Hmm i just changed it to the following ( the output from the server is:

										 							 					
0/24 player(s) online.

<title>Server Status</title>

<h2>IP: *hided*</h2>
<h2>Players:0/24 player(s) online.</h2>
<h2>MOTD: *hided* </h2>
<h2>Status: ONLINE
</h2>			

And updated LUA code:

local chatBox = peripheral.wrap("top")

myUrl = "http://*hided*.php"

if http then
 while true do
  req01 = http.get(myUrl)
  local read01 = req01.readAll()
  local content = read01
  req01.close()
  print(read01)
  local mode = read01:lower():match( "Status: (.+);") -- this should return the "ONLINE" or "OFFLINE"

   if mode == "offline" then
    status = "right"
   elseif mode == "online" then
    status = "left"
   end
  if status == "right" then
   rs.setOutput(status, true)
   sleep(0.5)
   rs.setOutput(status, false)
  elseif status == "left" then
   chatBox.say("*hided* is open again! This server will close in 10 seconds! Remember to save your codes!")
   sleep(5)
   chatBox.say("Server close-down in 5 seconds! Save your stuffs!")
   sleep(5)
   chatBox.say("Closing server...")
   rs.setOutput(status, true)
   sleep(0.5)
   rs.setOutput(status, false)
  else
   print("No statement found!")
  end   
  sleep(5)
 end
end

Engineer #4
Posted 28 July 2013 - 06:05 PM
Then use this:

lcoal status = ({string.find(content, "status = (.+)")})[3]
Goof #5
Posted 28 July 2013 - 06:08 PM
well nothing returned
Goof #6
Posted 28 July 2013 - 06:10 PM
Wait…

It returns

ONLINE<br/><h2/>
Goof #7
Posted 28 July 2013 - 06:19 PM
SOLVED


local hasClosed = false
local chatBox = peripheral.wrap("top")

myUrl = "*hided*"

if http then
 while true do
  req01 = http.get(myUrl)
  local read01 = req01.readAll()
  req01.close()
  local state = string.lower(({string.find(read01, "Status: (.+)")})[3])
   print(state)
   if string.find(state,"offline") then
    status = "right"
   elseif string.find(state,"online") then
    status = "left"
   end
  if status == "right" then
   hasClosed = false
   rs.setOutput(status, true)
   sleep(0.5)
   rs.setOutput(status, false)
  elseif status == "left" and not hasClosed then
   hasClosed = true
   chatBox.say("*server*is open again! This server will close in 10 seconds! Remember to save your codes!")
   sleep(5)
   chatBox.say("Server close-down in 5 seconds! Save your stuffs!")
   sleep(5)
   chatBox.say("Closing server...")
   rs.setOutput(status, true)
   sleep(0.5)
   rs.setOutput(status, false)
  else
   print("No statement found!")
  end   
  sleep(5)
 end
end