Check the URL for a variable passed GET LUA

Asked

Viewed 145 times

2

I want to check through the URL if I have any variables passed to via GET. For example at this url: www.exemplo.htm?teste=teste

if url then -- se na barra de endereço tem um get
  return true
 else
  return false
 end

I have a variable passed, how can I check this in language LUA?

  • Assuming the URL is: www.exemplo.htm?chave1=valor1&chave2=valor2, you want to know if the valor1 is present in it?

  • Yes that’s right, I want to know if the url has any past parameters. If yes return true and if there is nothing, that is, the url being just www.exemplo.htm return false.

1 answer

2


One way to do this is to use the function string.gmatch:

function checkURL(url, parametro)
    for chave, valor in string.gmatch(url, "(%w+)=(%w+)") do
        if valor == parametro then
            return true
        end
    end
    return false
end

Use the function checkURL thus:

if checkURL("www.exemplo.htm?chave1=valor1&chave2=valor2", "valor1") then
    print ("valor1 está presente na URL")
else
    print ("valor1 NÃO foi encontrado na URL")
end

See demonstração

Another alternative is to use the function string.find:

url = "www.exemplo.htm?chave1=valor1&chave2=valor2"

if string.find(url, "valor1") then
    print ("valor1 está presente na URL")
else
    print ("valor1 NÃO foi encontrado na URL")
end

See demonstração

  • Thanks for the answer, but this I have to pass the url by paramentro in the function. There is no way to go "fetch" automatically?

  • @akm I updated the answer, see if this is it.

  • right but the url remains a variable, I wanted it to be fetched directly from the browser’s address bar. And then pass the condition.

  • @akm Ai will depend on how you implement this, you can take this address and put it in the variable and make the comparison, which you use to make the requests? That one?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.