|
|
【程式】C語言判斷User-Agent是否為手機設備的程式 |
一派护法 十九级 |
#include <ctype.h> #include <stdio.h> #include <string.h>
char *device_list[] = {"240x320", "acer", "acoon", "acs-", "abacho", "ahong", "airness", "alcatel", "amoi", "android", "anywhereyougo.com", "applewebkit/525", "applewebkit/532", "asus", "audio", "au-mic", "avantogo", "becker", "benq", "bilbo", "bird", "blackberry", "blazer", "bleu", "cdm-", "compal", "coolpad", "danger", "dbtel", "dopod", "elaine", "eric", "etouch", "fly ", "fly_", "fly-", "go.web", "goodaccess", "gradiente", "grundig", "haier", "hedy", "hitachi", "htc", "huawei", "hutchison", "inno", "ipad", "ipaq", "ipod", "jbrowser", "kddi", "kgt", "kwc", "lenovo", "lg ", "lg2", "lg3", "lg4", "lg5", "lg7", "lg8", "lg9", "lg-", "lge-", "lge9", "longcos", "maemo", "mercator", "meridian", "micromax", "midp", "mini", "mitsu", "mmm", "mmp", "mobi", "mot-", "moto", "nec-", "netfront", "newgen", "nexian", "nf-browser", "nintendo", "nitro", "nokia", "nook", "novarra", "obigo", "palm", "panasonic", "pantech", "philips", "phone", "pg-", "playstation", "pocket", "pt-", "qc-", "qtek", "rover", "sagem", "sama", "samu", "sanyo", "samsung", "sch-", "scooter", "sec-", "sendo", "sgh-", "sharp", "siemens", "sie-", "softbank", "sony", "spice", "sprint", "spv", "symbian", "tablet", "talkabout", "tcl-", "teleca", "telit", "tianyu", "tim-", "toshiba", "tsm", "up.browser", "utec", "utstar", "verykool", "virgin", "vk-", "voda", "voxtel", "vx", "wap", "wellco", "wig browser", "wii", "windows ce", "wireless", "xda", "xde", "zte"};
void strtolower(char* str) { int i; int len = strlen(str); for (i = 0; i < len; i++) str[i] = tolower(str[i]); }
int main(int argc, char *argv[]) { int len = sizeof(device_list) / sizeof(device_list[0]) / sizeof(char); printf("There are %d items.\n", len); char user_agent[200]; memset(user_agent, '\0', 200); printf("Please provide your HTTP_USER_AGENT string: (D = Default) \n"); scanf("%[^\n]", user_agent); if (strcmp(user_agent, "D") == 0) { strcpy(user_agent, "Mozilla/5.0 (Linux; Android 4.4.4; vivo Y29L Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36"); printf("%s\n", user_agent); } int i; strtolower(user_agent); for (i = 0; i < len; i++) { if (strstr(user_agent, device_list[i])) { printf("Keyword ID: %d\n", i + 1); printf("Keyword: %s\n", device_list[i]); return 0; } } printf("No keyword!\n"); return 0; }
|
一派护法 十九级 |
程序運行結果示例: [octopus@pc5 mobile_devices]$ ./check There are 148 items. Please provide your HTTP_USER_AGENT string: (D = Default)
No keyword! [octopus@pc5 mobile_devices]$ ./check There are 148 items. Please provide your HTTP_USER_AGENT string: (D = Default) D Mozilla/5.0 (Linux; Android 4.4.4; vivo Y29L Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36 Keyword ID: 10 Keyword: android [octopus@pc5 mobile_devices]$ ./check There are 148 items. Please provide your HTTP_USER_AGENT string: (D = Default) Mozilla/5.0 (Linux; Android 4.4.4; vivo Y29L Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36 Keyword ID: 10 Keyword: android [octopus@pc5 mobile_devices]$ ./check There are 148 items. Please provide your HTTP_USER_AGENT string: (D = Default) HTTP_USER_AGENT: Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 No keyword! [octopus@pc5 mobile_devices]$
|
一派护法 十九级 |
編譯出的linux程序大小:14.3 kB (14,328 bytes)
|
一派护法 十九级 |
最新關鍵詞庫移除了tablet這個關鍵詞。 char *device_list[] = {"240x320", "acer", "acoon", "acs-", "abacho", "ahong", "airness", "alcatel", "amoi", "android", "anywhereyougo.com", "applewebkit/525", "applewebkit/532", "asus", "audio", "au-mic", "avantogo", "becker", "benq", "bilbo", "bird", "blackberry", "blazer", "bleu", "cdm-", "compal", "coolpad", "danger", "dbtel", "dopod", "elaine", "eric", "etouch", "fly ", "fly_", "fly-", "go.web", "goodaccess", "gradiente", "grundig", "haier", "hedy", "hitachi", "htc", "huawei", "hutchison", "inno", "ipad", "ipaq", "ipod", "jbrowser", "kddi", "kgt", "kwc", "lenovo", "lg ", "lg2", "lg3", "lg4", "lg5", "lg7", "lg8", "lg9", "lg-", "lge-", "lge9", "longcos", "maemo", "mercator", "meridian", "micromax", "midp", "mini", "mitsu", "mmm", "mmp", "mobi", "mot-", "moto", "nec-", "netfront", "newgen", "nexian", "nf-browser", "nintendo", "nitro", "nokia", "nook", "novarra", "obigo", "palm", "panasonic", "pantech", "philips", "phone", "pg-", "playstation", "pocket", "pt-", "qc-", "qtek", "rover", "sagem", "sama", "samu", "sanyo", "samsung", "sch-", "scooter", "sec-", "sendo", "sgh-", "sharp", "siemens", "sie-", "softbank", "sony", "spice", "sprint", "spv", "symbian", "talkabout", "tcl-", "teleca", "telit", "tianyu", "tim-", "toshiba", "tsm", "up.browser", "utec", "utstar", "verykool", "virgin", "vk-", "voda", "voxtel", "vx", "wap", "wellco", "wig browser", "wii", "windows ce", "wireless", "xda", "xde", "zte"};
|
一派护法 十九级 |
scanf("%[^\n]", user_agent); 这条语句相对于%s来说,可以支持空格输入。%s遇到空格就停止处理了。
|
|
本帖信息
评论数: |
? |
作者:巨大八爪鱼 |
最后回复:巨大八爪鱼 |
最后回复时间:2015-12-12 12:26 |
|
|