12 julho 2009

Como identificar um navegador de celular (mobile)

Recentemente tive a necessidade de criar um portal Wap para a Empresa que trabalho, porém sem saber muito de WAP ou WML, fui atrás do padrão aceito que é o xHTML.

Encontrei em algum lugar na net um fonte em PHP que faz isso aqui, porém traduzi para o ASP para usar lá na empresa.

Fica ai para quem quiser aproveitar o fonte.

'** Caso a flag F=Full seja passada direciona para o XXXXXXXXXXXxxxxxs
'** Url para redir
Const WML_Redir = "http://www.XXXXXXXXXXXxxxxxs.com.br/wap2.0/"
Const HTTP_Redir = "http://www.XXXXXXXXXXXxxxxxs.com.br/v9/"
Const PSP_Redir = "http://www.XXXXXXXXXXXxxxxxs.com.br/wap2.0/"
'--------------------------------------------------------------------------------
FwdMobile = request("s")
if FwdMobile = "1" then
response.redirect HTTP_Redir
response.end
end if
'--------------------------------------------------------------------------------
'** Dados do Client
Dim Client    :  Client    =  Lcase(Request.ServerVariables("HTTP_USER_AGENT"))
Dim Protocolo  :  Protocolo  =  Lcase(Request.ServerVariables("HTTP_ACCEPT"))
'** Lista de navegadores conhecidos
WapBrowsers  =  array("wapbrowser","up.browser","up/4","mib","cellphone","go.web","nokia","panasonic","wap","wml-browser","wml","iemobile","iphone")
PcBrowsers  =  array("mozilla","gecko","opera","omniweb","msie","konqueror","safari","netpositive" ,"lynx" ,"elinks" ,"links" ,"w3m" ,"webtv" ,"amaya" ,    "dillo" ,"ibrowse" ,"icab" ,"crazy browser" ,"internet explorer" )
PsPBrowsers  =  array("PlayStation Portable")

Private Function VerifyProtocol()
'Dim sFound    :  sFound    =  0
if instr(Protocolo,"wml") > 0 then
sFound = 0
else
if CheckBrowser(WapBrowsers,Client) then  sFound = 0 : Found = true
if CheckBrowser(PcBrowsers,Client) and  Found = false then    sFound = 1 : Found = true
if CheckBrowser(PsPBrowsers,Client) and  Found = false then  sFound = 2 : Found = true
end if
if request.querystring("s") = "0" then sFound = request.querystring("s")
Call DoRedir(sFound)
End Function

Private Function CheckBrowser(List,User)
sReturn = false
for i=0 to Ubound(List)
if Instr(user,List(i)) > 0  then
sReturn = true
exit for
end if
next
CheckBrowser = sReturn
End Function

Private Function DoRedir(sFound)
Select   Case sFound
Case  0  : url = WML_Redir
Case  1  : url = HTTP_Redir
Case  2  : url = PSP_Redir
End Select

response.redirect(url&"?c="& session.sessionid)
response.end
'  response.write ""& url &" "& Client &"
"& Protocolo
End Function
if request.querystring("c") = "" then Call VerifyProtocol()


Veja a atualização deste post clicando aqui.

Nenhum comentário: