Detect phonegap user agent on the server side and forbid non-phonegap downloads

1,027 views
Skip to first unread message

Gavriel Fleischer

unread,
May 7, 2012, 4:14:24 PM5/7/12
to phon...@googlegroups.com
I am building a mobile app with PhoneGap and it will download a file from my server, where I have php. I am looking for a way to differentiate between my phonegap app and "every other browser" from the php. I know how to look for user agent string in php, my question is if there's a string (like "PhoneGap") that I'll have in the user agent on every platform (android, iphone, etc) or if there's a way to tell PhoneGap to use my custom user agent string.

Optionally if there's an idea what could I do in order to serve these files when the PhoneGap FileTransfer asks for them, but not when another browser does, is welcome. I don't want to force the users to create an account if there's another way.

Kerri Shotts

unread,
May 7, 2012, 7:14:33 PM5/7/12
to phon...@googlegroups.com
By default, the user agent string is the one generated by the webview, so I doubt you'll be able to see a difference. However, I do know that iOS browsers do permit changing agent strings, but I don't know if they do it by going out of the webview somehow (and then putting the contents inside the webview) or if they somehow convince the webview to change its agent string. Either way, you're probably going to have to write a plugin or do some ObjC for iOS. If I had to guess, I'd say the same would tend to apply for any platform.

Jonhnatha Trigueiro

unread,
May 7, 2012, 5:54:59 PM5/7/12
to phon...@googlegroups.com, phon...@googlegroups.com
Hi there!

Did you try to make the request to your php using a kind of token? For example, you could, into device, get the page sending a post var named token with a code. Your php would recognize it and grant access.

Regards,

Jonhnatha J. R. Trigueiro

+55 84 8888 4224
+55 83 9634 4224


@joepreludian

From iPhone

On 07/05/2012, at 05:14 PM, Gavriel Fleischer <gavr...@gmail.com> wrote:

I am building a mobile app with PhoneGap and it will download a file from my server, where I have php. I am looking for a way to differentiate between my phonegap app and "every other browser" from the php. I know how to look for user agent string in php, my question is if there's a string (like "PhoneGap") that I'll have in the user agent on every platform (android, iphone, etc) or if there's a way to tell PhoneGap to use my custom user agent string.

Optionally if there's an idea what could I do in order to serve these files when the PhoneGap FileTransfer asks for them, but not when another browser does, is welcome. I don't want to force the users to create an account if there's another way.

--
You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
 
For more info on PhoneGap or to download the code go to www.phonegap.com

Shane MacPhillamy

unread,
May 10, 2012, 7:32:23 PM5/10/12
to phon...@googlegroups.com
In CDVViewController -- this works for me:

+ (void)initialize {

    NSString *systemVersionString = [[[UIDevice currentDevice] systemVersion] stringByReplacingOccurrencesOfString:@"." withString:@"_"];

    NSDictionary *webkitDictionary = [NSDictionary dictionaryWithObjectsAndKeys:

                                      @"533.17.9", @"4_3_2"

                                      @"534.46", @"5_0",

                                      @"534.46", @"5_0_1",

                                      nil, nil];

    NSString *webkitVersionString = nil;

    if ((webkitVersionString = [webkitDictionary objectForKey:systemVersionString]) == nil) {

        webkitVersionString = [webkitDictionary objectForKey:@"4_3_2"];

    }

    

    NSString *uaString = [NSString stringWithFormat:@"Mozilla/5.0 (%@; CPU OS %@ like Mac OS X) AppleWebKit/%@ (KHTML, like Gecko) Mobile/9A334 MyString/%@",

                          [[UIDevice currentDevice] model],

                          systemVersionString,

                          webkitVersionString,

                          SYMBOL_TO_NSSTRING(PG_VERSION)];

    

    NSLog(@"UA = %@", uaString);

    // Set user agent (the only problem is that we can't modify the User-Agent later in the program)

    NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:uaString, @"UserAgent", nil];

    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

    [dictionary release];

Reply all
Reply to author
Forward
0 new messages