Jump to content




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


  • You cannot reply to this topic
6 replies to this topic

#1 Goof

  • Members
  • 751 posts

Posted 28 July 2013 - 04: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

#2 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 28 July 2013 - 04: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.

#3 Goof

  • Members
  • 751 posts

Posted 28 July 2013 - 04: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



#4 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 28 July 2013 - 05:05 PM

Then use this:
lcoal status = ({string.find(content, "status = (.+)")})[3]


#5 Goof

  • Members
  • 751 posts

Posted 28 July 2013 - 05:08 PM

well nothing returned

#6 Goof

  • Members
  • 751 posts

Posted 28 July 2013 - 05:10 PM

Wait...

It returns
ONLINE<br/><h2/>


#7 Goof

  • Members
  • 751 posts

Posted 28 July 2013 - 05: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







1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users