Browser type and OS of Server ::CLASS:

Information for PHP. Hypertext Preprocessor
Post Reply
dzcadii
Posts: 94
Joined: Sun Mar 01, 2015 4:36 am

Browser type and OS of Server ::CLASS:

Post by dzcadii » Sun Mar 01, 2015 6:37 pm

Need to find out what browser is hitting your page? Here is a simple class in PHP for that
The OS portion will tell you what OS the server is on. Use this for development servers if you use different variables for each like ::Localhost:: instead of dzsoundnirvana.com

**Update**
Ok, I have changed the code to take everything out of the $_SESSION. All information is in the Client_Info array.

Code: Select all

<?
class client{
    var $Errors;
    var $Client_Info;
    
    function GetBrowser(){
        //Browser Info
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        //Just the IP
        $clientip=$_SERVER['REMOTE_ADDR'];
        //IP or client name
        $clientipn=gethostbyaddr($clientip);
        
        if (preg_match("|MSIE ([0-9].[0-9]{1,2})|", $user_agent, $matched)) {
            $browser_version=$matched[1];
            $browser = "IE";
        }
        elseif(preg_match( "|Opera ([0-9].[0-9]{1,2})|", $user_agent, $matched)) {
            $browser_version=$matched[1];
                $browser = "Opera";
        }
        elseif(preg_match( "|AOL ([0-9].[0-9]{1,2})|", $user_agent, $matched)) {
            $browser_version=$matched[1];
                $browser = "AOL";
        }
        elseif(preg_match( "|Navigator/ ([0-9].[0-9]{1,2})|", $user_agent, $matched)) {
            $browser_version=$matched[1];
                $browser = "Netscape";
        }
        elseif(preg_match( "|Fluid/ ([0-9].[0-9]{1,3})|", $user_agent, $matched)) {
            $browser_version=$matched[1];
                $browser = "Fluid";
        }
        elseif(preg_match( "|Pogo/ ([0-9].[0-9]{1,2})|", $user_agent, $matched)) {
            $browser_version=$matched[1];
                $browser = "Pogo";
        }
        elseif(preg_match("|Firefox/([0-9\.]+)|", $user_agent, $matched)) {
            $browser_version=$matched[1];
            $browser = "Firefox";
        }
        elseif(preg_match("|Chrome/([0-9\.]+)|", $user_agent, $matched)) {
                $browser_version=$matched[1];
              $browser = "Chrome";
        }
        elseif(preg_match("|Safari/([0-9\.]+)|", $user_agent, $matched)) {
                $browser_version=$matched[1];
              $browser = "Safari";
        }
        else {
               // browser not recognized!
                $browser_version = 0;
                $browser= "Unknown";
        }
        $this->Client_Info['Browser_Name']=$browser;
        $this->Client_Info['Browser_Version']=$browser_version;
        $this->Client_Info['IP'] = $clientip;
        $this->Client_Info['Host_Name'] = $clientipn;
        
    }
}
?>
and this is how you would use it (simple way)

Code: Select all

<?
$client = new client();
var_dump($client->Client_Info);
?>

//Quick and easy

Post Reply