Link to home
Start Free TrialLog in
Avatar of araskurd
araskurd

asked on

i have an issue in Auto detect mobile browser

0
down vote
favorite
      

i have an issue in Auto detect mobile browser , when from my phone the posts and subjects i have sent via my website to facebook after you you click on the post then it will not enter to the fully of the article in mobile version. for example this link http://tendrustikurd.net/Topic_80_11

if i open it using Computer then its fine also in mobile version its fine too http://tendrustikurd.net/Mobile/Topic_80_11 , but if i send this link to facebook and then i open it via mobile phone then it will not show the post but only show the main page of the website i mean it will redirect you to the home page .. that is big issue for me please i need solution for this...

i used this code:
protected void Page_Load(object sender, EventArgs e)
{
    if (isMobileBrowser() == true)
    {
        Response.Redirect("~/Mobile/");
    }
 }


public static bool isMobileBrowser()
{

    HttpContext context = HttpContext.Current;


    if (context.Request.Browser.IsMobileDevice)
    {
        return true;
    }
    //THEN TRY CHECKING FOR THE HTTP_X_WAP_PROFILE HEADER
    if (context.Request.ServerVariables["HTTP_X_WAP_PROFILE"] != null)
    {
        return true;
    }
    //THEN TRY CHECKING THAT HTTP_ACCEPT EXISTS AND CONTAINS WAP
    if (context.Request.ServerVariables["HTTP_ACCEPT"] != null &&
        context.Request.ServerVariables["HTTP_ACCEPT"].ToLower().Contains("wap"))
    {
        return true;
    }
    //AND FINALLY CHECK THE HTTP_USER_AGENT
    //HEADER VARIABLE FOR ANY ONE OF THE FOLLOWING
    if (context.Request.ServerVariables["HTTP_USER_AGENT"] != null)
    {
        //Create a list of all mobile types
        string[] mobiles =
            new[]
            {
                "midp", "j2me", "avant", "docomo",
                "novarra", "palmos", "palmsource",
                "240x320", "opwv", "chtml",
                "pda", "windows ce", "mmp/",
                "blackberry", "mib/", "symbian",
                "wireless", "nokia", "hand", "mobi",
                "phone", "cdm", "up.b", "audio",
                "SIE-", "SEC-", "samsung", "HTC",
                "mot-", "mitsu", "sagem", "sony"
                , "alcatel", "lg", "eric", "vx",
                "NEC", "philips", "mmm", "xx",
                "panasonic", "sharp", "wap", "sch",
                "rover", "pocket", "benq", "java",
                "pt", "pg", "vox", "amoi",
                "bird", "compal", "kg", "voda",
                "sany", "kdd", "dbt", "sendo",
                "sgh", "gradi", "jb", "dddi",
                "moto", "iphone"
            };

        //Loop through each item in the list created above
        //and check if the header contains that text
        foreach (string s in mobiles)
        {
            if (context.Request.ServerVariables["HTTP_USER_AGENT"].
                                                ToLower().Contains(s.ToLower()))
            {
                return true;
            }
        }
    }

    return false;
}
Avatar of Mark Bullock
Mark Bullock
Flag of United States of America image

You will get better results if you use a 3rd party database like this one. You'll need to update it on some schedule.
https://www.nuget.org/packages/WURFL_Official_API
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of araskurd
araskurd

ASKER

hi ste5an,

where i put this code ?? please give me instruction or details.

Thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this resolve my issue ? do you understood my issue ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial