[PHP]
<? PHP
/*
Role: obtain client information
Parameters:
Return: specified data
Usage:
$ Code = new client;
1. Browser: $ STR = $ code-> getbrowse ();
2. IP Address: $ STR = $ code-> getip ();
3. IP address information: $ STR = $ code-> getipaddres ();
4. Operating System: $ STR = $ code-> getos ();
*/
Class Client
{
Function getbrowse ()
{
$ Agent = $ _ server ['HTTP _ user_agent '];
$ Browser = '';
$ Browserver = '';
$ Browser = array ('lynx ', 'mosaic', 'aol ', 'Opera', 'java', 'macweb', 'webexplorer ', 'omniweb ');
For ($ I = 0; $ I <= 7; $ I ++ ){
If (strpos ($ agent, $ browsers [$ I]) {
$ Browser = $ browsers [$ I];
$ Browserver = '';
}
}
If (ereg ('mozilla ', $ agent )&&! Ereg ('msie ', $ agent )){
$ Temp = explode (', $ agent );
$ Part = $ temp [0];
$ Temp = explode ('/', $ part );
$ Browserver = $ temp [1];
$ Temp = explode ('', $ browserver );
$ Browserver = $ temp [0];
$ Browserver = preg_replace ('/([/D/.] +)/', '// 1', $ browserver );
$ Browserver = $ browserver;
$ Browser = 'netscape navigator ';
}
If (ereg ('mozilla ', $ agent) & ereg ('Opera', $ agent )){
$ Temp = explode (', $ agent );
$ Part = $ temp [1];
$ Temp = explode (')', $ part );
$ Browserver = $ temp [1];
$ Temp = explode ('', $ browserver );
$ Browserver = $ temp [2];
$ Browserver = preg_replace ('/([/D/.] +)/', '// 1', $ browserver );
$ Browserver = $ browserver;
$ Browser = 'Opera ';
}
If (ereg ('mozilla ', $ agent) & ereg ('msie', $ agent )){
$ Temp = explode (', $ agent );
$ Part = $ temp [1];
$ Temp = explode (';', $ part );
$ Part = $ temp [1];
$ Temp = explode ('', $ part );
$ Browserver = $ temp [2];
$ Browserver = preg_replace ('/([/D/.] +)/', '// 1', $ browserver );
$ Browserver = $ browserver;
$ Browser = 'Internet explorer ';
}
If ($ browser! = ''){
$ Browseinfo = $ browser. ''. $ browserver;
} Else {
$ Browseinfo = false;
}
Return $ browseinfo;
}
Function getip ()
{
If (getenv ('HTTP _ client_ip ')){
$ IP = getenv ('HTTP _ client_ip ');
} Else if (getenv ('HTTP _ x_forwarded_for ')){
$ IP = getenv ('HTTP _ x_forwarded_for ');
} Else if (getenv ('remote _ ADDR ')){
$ IP = getenv ('remote _ ADDR ');
} Else {
$ IP = $ _ server ['remote _ ADDR '];
}
Return $ IP;
}
Function getipaddres ($ IP = '')
{
If ($ IP = ''){
$ IP = $ this-> getip ();
}
If ($ IP = '2014. 0.0.1 '){
Return $ ip_addres = 'local machine ';
}
$ Recordlength = 17 + 22 + 13 + 47 + 12 + 1;
$ Ret = ereg ('^ ([0-9] + )/. ([0-9] + )/. ([0-9] + )/. ([0-9] +) $ ', $ IP, $ ipsection );
If ($ ret = false) {$ ip_addres = 'IP address error ';}
For ($ I = 1; $ I <= 4; $ I ++ ){
If ($ ipsection [$ I]> 255 ){
$ Ip_addres = 'IP address error ';
} Else {
$ Thisip. = sprintf ('% 03.0f', $ ipsection [$ I]). ($ I <4 )? '.':'');
}
}
$ Fp = fopen ($ this-> OBJ ['rootdir']. 'data/ip.txt ', 'rb ');
If ($ fp = NULL ){
$ Ip_addres = 'IP record file opening error ';
}
Fseek ($ FP, 0, seek_end );
$ Datafieldbegin = 0xc2;
$ Recordcount = floor (ftell ($ FP)-$ datafieldbegin)/$ recordlength );
If ($ recordcount <= 1 ){
$ Ip_addres = 'IP record file opening error ';
} Else {
$ Rangb = 0;
$ Range = $ recordcount;
While ($ rangb <$ range-1 ){
$ Recno = floor ($ rangb + $ range)/2 );
Fseek ($ FP, $ recno * $ recordlength + $ datafieldbegin, seek_set );
$ Buf = fread ($ FP, $ recordlength );
If (strlen ($ BUF) = 0 ){
Return false;
}
$ Startip = (substr ($ Buf, 0, 17 ));
$ Endip = trim (substr ($ Buf, 17, 22 ));
$ Country = trim (substr ($ Buf, 17 + 22, 13 ));
$ Local = trim (substr ($ Buf, 17 + 22 + 13, 47 ));
If (strcmp ($ thisip, $ startip) >=0 & strcmp ($ thisip, $ endip) <= 0 ){
Break;
}
If (strcmp ($ thisip, $ startip)> 0 ){
$ Rangb = $ recno;
} Else {
$ Range = $ recno;
}
}
If (! ($ Rangb <$ range-1 )){
$ Ip_addres = 'unknown address! ';
} Else {
$ Ip_addres = $ country;
$ Ip_addres. = $ local;
}
}
Fclose ($ FP );
Return $ ip_addres;
}
Function getos ()
{
$ Agent = $ _ server ['HTTP _ user_agent '];
$ OS = false;
If (eregi ('win', $ agent) & strpos ($ agent, '95 ')){
$ OS = 'windows 95 ';
}
Else if (eregi ('win 9x', $ agent) & strpos ($ agent, '4. 90 ')){
$ OS = 'windows Me ';
}
Else if (eregi ('win', $ agent) & ereg ('98 ', $ agent )){
$ OS = 'windows 98 ';
}
Else if (eregi ('win', $ agent) & eregi ('nt 5.1 ', $ agent )){
$ OS = 'windows XP ';
}
Else if (eregi ('win', $ agent) & eregi ('nt 5', $ agent )){
$ OS = 'windows 2000 ';
}
Else if (eregi ('win', $ agent) & eregi ('nt ', $ agent )){
$ OS = 'windows nt ';
}
Else if (eregi ('win', $ agent) & ereg ('32', $ agent )){
$ OS = 'windows 32 ';
}
Else if (eregi ('linux ', $ agent )){
$ OS = 'linux ';
}
Else if (eregi ('unix ', $ agent )){
$ OS = 'unix ';
}
Else if (eregi ('sun', $ agent) & eregi ('OS', $ agent )){
$ OS = 'sunos ';
}
Else if (eregi ('ibm ', $ agent) & eregi (' OS ', $ agent )){
$ OS = 'ibm OS/2 ';
}
Else if (eregi ('mac', $ agent) & eregi ('pc', $ agent )){
$ OS = 'macintosh ';
}
Else if (eregi ('powerpc', $ agent )){
$ OS = 'powerpc ';
}
Else if (eregi ('Aix ', $ agent )){
$ OS = 'Aix ';
}
Else if (eregi ('hpux ', $ agent )){
$ OS = 'hpux ';
}
Else if (eregi ('netsd', $ agent )){
$ OS = 'netsd ';
}
Else if (eregi ('bsd', $ agent )){
$ OS = 'bsd ';
}
Else if (ereg ('oss1', $ agent )){
$ OS = 'oss1 ';
}
Else if (ereg ('irix ', $ agent )){
$ OS = 'irix ';
}
Else if (eregi ('freebsd ', $ agent )){
$ OS = 'freebsd ';
}
Else if (eregi ('teleport ', $ agent )){
$ OS = 'teleport ';
}
Else if (eregi ('flashget', $ agent )){
$ OS = 'flashget ';
}
Else if (eregi ('webzip ', $ agent )){
$ OS = 'webzip ';
}
Else if (eregi ('offline', $ agent )){
$ OS = 'offline ';
}
Else {
$ OS = 'unknown ';
}
Return $ OS;
}
}
?>
[/PHP]