How to determine the user’s browser and ip? [duplicate]

What code can be used to determine the user’s ip and browser?


Answer 1, authority 100%

$_SERVER['REMOTE_ADDR']
$_SERVER['HTTP_USER_AGENT']

IP and client, respectively


Answer 2, authority 89%

I saw the answer to your question on the Internet … I still use this method

function get_ip()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))
    {
        $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
    {
        $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
        $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

Answer 3, authority 72%

There is only one way to determine the IP address of the client from which the request came: $ip = $_SERVER['REMOTE_ADDR'];Everything else has nothing to do with the IP address.

All values ​​of the $_SERVERarray beginning with 'HTTP_'are request headers, which the client forms entirely on its own.


Answer 4, authority 50%

class Browser {
  private $_agent = '';
  private $_browser_name = '';
  private $_version = '';
  private $_platform = '';
  private $_os = '';
  private $_is_aol = false;
  private $_is_mobile = false;
  private $_is_robot = false;
  private $_aol_version = '';
  const BROWSER_UNKNOWN = 'unknown';
  const VERSION_UNKNOWN = 'unknown';
  const BROWSER_OPERA = 'Opera'; // http://www.opera.com/
  const BROWSER_OPERA_MINI = 'Opera Mini'; // http://www.opera.com/mini/
  const BROWSER_WEBTV = 'WebTV'; // http://www.webtv.net/pc/
  const BROWSER_IE = 'Internet Explorer'; // http://www.microsoft.com/ie/
  const BROWSER_POCKET_IE = 'Pocket Internet Explorer'; // http://en.wikipedia.org/wiki/Internet_Explorer_Mobile
  const BROWSER_KONQUEROR = 'Konqueror'; // http://www.konqueror.org/
  const BROWSER_ICAB = 'iCab'; // http://www.icab.de/
  const BROWSER_OMNIWEB = 'OmniWeb'; // http://www.omnigroup.com/applications/omniweb/
  const BROWSER_FIREBIRD = 'Firebird'; // http://www.ibphoenix.com/
  const BROWSER_FIREFOX = 'Firefox'; // http://www.mozilla.com/en-US/firefox/firefox.html
  const BROWSER_ICEWEASEL = 'Iceweasel'; // http://www.geticeweasel.org/
  const BROWSER_SHIRETOKO = 'Shiretoko'; // http://wiki.mozilla.org/Projects/shiretoko
  const BROWSER_MOZILLA = 'Mozilla'; // http://www.mozilla.com/en-US/
  const BROWSER_AMAYA = 'Amaya'; // http://www.w3.org/Amaya/
  const BROWSER_LYNX = 'Lynx'; // http://en.wikipedia.org/wiki/Lynx
  const BROWSER_SAFARI = 'Safari'; // http://apple.com
  const BROWSER_IPHONE = 'iPhone'; // http://apple.com
  const BROWSER_IPOD = 'iPod'; // http://apple.com
  const BROWSER_IPAD = 'iPad'; // http://apple.com
  const BROWSER_CHROME = 'Chrome'; // http://www.google.com/chrome
  const BROWSER_ANDROID = 'Android'; // http://www.android.com/
  const BROWSER_GOOGLEBOT = 'GoogleBot'; // http://en.wikipedia.org/wiki/Googlebot
  const BROWSER_SLURP = 'Yahoo! Slurp'; // http://en.wikipedia.org/wiki/Yahoo!_Slurp
  const BROWSER_W3CVALIDATOR = 'W3C Validator'; // http://validator.w3.org/
  const BROWSER_BLACKBERRY = 'BlackBerry'; //http://www.blackberry.com/
  const BROWSER_ICECAT = 'IceCat'; // http://en.wikipedia.org/wiki/GNU_IceCat 
const BROWSER_NOKIA_S60 = 'Nokia S60 OSS Browser'; // http://en.wikipedia.org/wiki/Web_Browser_for_S60
  const BROWSER_NOKIA = 'Nokia Browser'; // * all other WAP-based browsers on the Nokia Platform
  const BROWSER_MSN = 'MSN Browser'; // http://explorer.msn.com/
  const BROWSER_MSNBOT = 'MSN Bot'; // http://search.msn.com/msnbot.htm
                               // http://en.wikipedia.org/wiki/Msnbot (used for Bing as well)
  const BROWSER_NETSCAPE_NAVIGATOR = 'Netscape Navigator'; // http://browser.netscape.com/ (DEPRECATED)
  const BROWSER_GALEON = 'Galeon'; // http://galeon.sourceforge.net/ (DEPRECATED)
  const BROWSER_NETPOSITIVE = 'NetPositive'; // http://en.wikipedia.org/wiki/NetPositive (DEPRECATED)
  const BROWSER_PHOENIX = 'Phoenix'; // http://en.wikipedia.org/wiki/History_of_Mozilla_Firefox (DEPRECATED)
  const PLATFORM_UNKNOWN = 'unknown';
  const PLATFORM_WINDOWS = 'Windows';
  const PLATFORM_WINDOWS_CE = 'Windows CE';
  const PLATFORM_APPLE = 'Apple';
  const PLATFORM_LINUX = 'Linux';
  const PLATFORM_OS2 = 'OS/2';
  const PLATFORM_BEOS = 'BeOS';
  const PLATFORM_IPHONE = 'iPhone';
  const PLATFORM_IPOD = 'iPod';
  const PLATFORM_IPAD = 'iPad';
  const PLATFORM_BLACKBERRY = 'BlackBerry';
  const PLATFORM_NOKIA = 'Nokia';
  const PLATFORM_FREEBSD = 'FreeBSD';
  const PLATFORM_OPENBSD = 'OpenBSD';
  const PLATFORM_NETBSD = 'NetBSD';
  const PLATFORM_SUNOS = 'SunOS';
  const PLATFORM_OPENSOLARIS = 'OpenSolaris';
  const PLATFORM_ANDROID = 'Android';
  const OPERATING_SYSTEM_UNKNOWN = 'unknown';
  public function Browser($useragent="") {
    $this->reset();
    if( $useragent != "" ) {
      $this->setUserAgent($useragent);
    }
    else {
      $this->determine();
    }
  }
  /**
  * Reset all properties
  */
  public function reset() {
    $this->_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "";
    $this->_browser_name = self::BROWSER_UNKNOWN;
    $this->_version = self::VERSION_UNKNOWN;
    $this->_platform = self::PLATFORM_UNKNOWN;
    $this->_os = self::OPERATING_SYSTEM_UNKNOWN;
    $this->_is_aol = false;
    $this->_is_mobile = false;
    $this->_is_robot = false;
    $this->_aol_version = self::VERSION_UNKNOWN;
  }
  /**
  * Check to see if the specific browser is valid
  * @param string $browserName
  * @return True if the browser is the specified browser
  */
  function isBrowser($browserName) { return( 0 == strcasecmp($this->_browser_name, trim($browserName))); }
  /**
  * The name of the browser. All return types are from the class constants
  * @return string Name of the browser
  */
  public function getBrowser() { return $this->_browser_name; }
  /**
  * Set the name of the browser
  * @param $browser The name of the Browser
  */
  public function setBrowser($browser) { return $this->_browser_name = $browser; }
  /**
  * The name of the platform. All return types are from the class constants
  * @return string Name of the browser
  */
  public function getPlatform() { return $this->_platform; }
  /**
  * Set the name of the platform
  * @param $platform The name of the Platform
  */
  public function setPlatform($platform) { return $this->_platform = $platform; }
  /**
  * The version of the browser.
  * @return string Version of the browser (will only contain alpha-numeric characters and a period)
  */
  public function getVersion() { return $this->_version; }
  /**
  * Set the version of the browser
  * @param $version The version of the Browser
  */
  public function setVersion($version) { $this->_version = preg_replace('/[^0-9,.,a-z,A-Z-]/','',$version); }
  /**
  * The version of AOL.
  * @return string Version of AOL (will only contain alpha-numeric characters and a period)
  */ 
public function getAolVersion() { return $this->_aol_version; }
  /**
  * Set the version of AOL
  * @param $version The version of AOL
  */
  public function setAolVersion($version) { $this->_aol_version = preg_replace('/[^0-9,.,a-z,A-Z]/','',$version); }
  /**
  * Is the browser from AOL?
  * @return boolean True if the browser is from AOL otherwise false
  */
  public function isAol() { return $this->_is_aol; }
  /**
  * Is the browser from a mobile device?
  * @return boolean True if the browser is from a mobile device otherwise false
  */
  public function isMobile() { return $this->_is_mobile; }
  /**
  * Is the browser from a robot (ex Slurp,GoogleBot)?
  * @return boolean True if the browser is from a robot otherwise false
  */
  public function isRobot() { return $this->_is_robot; }
  /**
  * Set the browser to be from AOL
  * @param $isAol
  */
  public function setAol($isAol) { $this->_is_aol = $isAol; }
  /**
   * Set the Browser to be mobile
   * @param boolean $value is the browser a mobile browser or not
   */
  protected function setMobile($value=true) { $this->_is_mobile = $value; }
  /**
   * Set the Browser to be a robot
   * @param boolean $value is the browser a robot or not
   */
  protected function setRobot($value=true) { $this->_is_robot = $value; }
  /**
  * Get the user agent value in use to determine the browser
  * @return string The user agent from the HTTP header
  */
  public function getUserAgent() { return $this->_agent; }
  /**
  * Set the user agent value (the construction will use the HTTP header value - this will overwrite it)
  * @param $agent_string The value for the User Agent
  */
  public function setUserAgent($agent_string) {
    $this->reset();
    $this->_agent = $agent_string;
    $this->determine();
  }
  /**
   * Used to determine if the browser is actually "chromeframe"
   * @since 1.7
   * @return boolean True if the browser is using chromeframe
   */
  public function isChromeFrame() {
    return( strpos($this->_agent,"chromeframe") !== false );
  }
  /**
  * Returns a formatted string with a summary of the details of the browser.
  * @return string formatted string with a summary of the browser
  */
  public function __toString() {
    return "<strong>Browser Name:</strong>{$this->getBrowser()}<br/>\n" .
        "<strong>Browser Version:</strong>{$this->getVersion()}<br/>\n" .
        "<strong>Browser User Agent String:</strong>{$this->getUserAgent()}<br/>\n" .
        "<strong>Platform:</strong>{$this->getPlatform()}<br/>";
  }
  /**
   * Protected routine to calculate and determine what the browser is in use (including platform)
   */
  protected function determine() {
    $this->checkPlatform();
    $this->checkBrowsers();
    $this->checkForAol();
  }
  /**
   * Protected routine to determine the browser type
   * @return boolean True if the browser was detected otherwise false
   */
   protected function checkBrowsers() {
    return (
      // well-known, well-used
      // Special Notes:
      // (1) Opera must be checked before FireFox due to the odd
      // user agents used in some older versions of Opera
      // (2) WebTV is strapped onto Internet Explorer so we must
      // check for WebTV before IE
      // (3) (deprecated) Galeon is based on Firefox and needs to be
      // tested before Firefox is tested
      // (4) OmniWeb is based on Safari so OmniWeb check must occur
      // before Safari
      // (5) Netscape 9+ is based on Firefox so Netscape checks
      // before FireFox are necessary
      $this->checkBrowserWebTv() ||
      $this->checkBrowserInternetExplorer() ||
      $this->checkBrowserOpera() ||
      $this->checkBrowserGaleon() ||
      $this->checkBrowserNetscapeNavigator9Plus() || 
$this->checkBrowserFirefox() ||
      $this->checkBrowserChrome() ||
      $this->checkBrowserOmniWeb() ||
      // common mobile
      $this->checkBrowserAndroid() ||
      $this->checkBrowserPad() ||
      $this->checkBrowseriPod() ||
      $this->checkBrowserPhone() ||
      $this->checkBrowserBlackBerry() ||
      $this->checkBrowserNokia() ||
      // common bots
      $this->checkBrowserGoogleBot() ||
      $this->checkBrowserMSNBot() ||
      $this->checkBrowserSlurp() ||
      // WebKit base check (post mobile and others)
      $this->checkBrowserSafari() ||
      // everyone else
      $this->checkBrowserNetPositive() ||
      $this->checkBrowserFirebird() ||
      $this->checkBrowserKonqueror() ||
      $this->checkBrowserIcab() ||
      $this->checkBrowserPhoenix() ||
      $this->checkBrowserAmaya() ||
      $this->checkBrowserLynx() ||
      $this->checkBrowserShiretoko() ||
      $this->checkBrowserIceCat() ||
      $this->checkBrowserW3CValidator() ||
      $this->checkBrowserMozilla() /* Mozilla is such an open standard that you must check it last */
    );
  }
  /**
   * Determine if the user is using a BlackBerry (last updated 1.7)
   * @return boolean True if the browser is the BlackBerry browser otherwise false
   */
  protected function checkBrowserBlackBerry() {
    if( stripos($this->_agent,'blackberry') !== false ) {
      $aresult = explode("/",stristr($this->_agent,"BlackBerry"));
      $aversion = explode(' ',$aresult[1]);
      $this->setVersion($aversion[0]);
      $this->_browser_name = self::BROWSER_BLACKBERRY;
      $this->setMobile(true);
      return true;
    }
    return false;
  }
  /**
   * Determine if the user is using an AOL User Agent (last updated 1.7)
   * @return boolean True if the browser is from AOL otherwise false
   */
  protected function checkForAol() {
    $this->setAol(false);
    $this->setAolVersion(self::VERSION_UNKNOWN);
    if( stripos($this->_agent,'aol') !== false ) {
      $aversion = explode(' ',stristr($this->_agent, 'AOL'));
      $this->setAol(true);
      $this->setAolVersion(preg_replace('/[^0-9\.a-z]/i', '', $aversion[1]));
      return true;
    }
    return false;
  }
  /**
   * Determine if the browser is the GoogleBot or not (last updated 1.7)
   * @return boolean True if the browser is the GoogletBot otherwise false
   */
  protected function checkBrowserGoogleBot() {
    if( stripos($this->_agent,'googlebot') !== false ) {
      $aresult = explode('/',stristr($this->_agent,'googlebot'));
      $aversion = explode(' ',$aresult[1]);
      $this->setVersion(str_replace(';','',$aversion[0]));
      $this->_browser_name = self::BROWSER_GOOGLEBOT;
      $this->setRobot(true);
      return true;
    }
    return false;
  }
  /**
   * Determine if the browser is the MSNBot or not (last updated 1.9)
   * @return boolean True if the browser is the MSNBot otherwise false
   */
  protected function checkBrowserMSNBot() {
    if( stripos($this->_agent,"msnbot") !== false ) {
      $aresult = explode("/",stristr($this->_agent,"msnbot"));
      $aversion = explode(" ",$aresult[1]);
      $this->setVersion(str_replace(";","",$aversion[0]));
      $this->_browser_name = self::BROWSER_MSNBOT;
      $this->setRobot(true);
      return true;
    }
    return false;
  }
  /**
   * Determine if the browser is the W3C Validator or not (last updated 1.7)
   * @return boolean True if the browser is the W3C Validator otherwise false
   */
  protected function checkBrowserW3CValidator() {
    if( stripos($this->_agent,'W3C-checklink') !== false ) { 
$ ARESULT = EXPLODE ('/', STRISTR ($ this- & gt; _Agent, 'W3C-Checklink'));
      $ AVersion = Explode ('', $ ARESULT [1]);
      $ this- & gt; setVersion ($ AVERSion [0]);
      $ this- & gt; _browser_name = self :: browser_w3cvalidator;
      RETURN TRUE;
    }
    ELSE if (stripos ($ this- & gt; _agent, 'w3c_validator')! == FALSE) {
      // Some of the Validator Versions Do Not Delineate W / a Slash - Add it Back in
      $ UA = STR_REPLACE ("W3C_VALIDATOR", "W3C_VALIDATOR /", $ THIS- & GT; _Agent);
      $ ARESULT = Explode ('/', stristr ($ UA, 'W3C_VALIDATOR'));
      $ AVersion = Explode ('', $ ARESULT [1]);
      $ this- & gt; setVersion ($ AVERSion [0]);
      $ this- & gt; _browser_name = self :: browser_w3cvalidator;
      RETURN TRUE;
    }
    RETURN FALSE;
  }
  / **
   * Determine IF The Browser Is The Yahoo! SLURP ROBOT OR NOT (LAST UPDATED 1.7)
   * @return Boolean True If The Browser Is The Yahoo! SLURP ROBOT OTHERWISE FALSE
   * /
  PROTECTED FUNCTION CHECKBROWSLURP () {
    If (stripos ($ this- & gt; _Agent, 'Slurp')! == FALSE) {
      $ ARESULT = EXPLODE ('/', STRISTR ($ this- & gt; _Agent, 'Slurp'));
      $ AVersion = Explode ('', $ ARESULT [1]);
      $ this- & gt; setVersion ($ AVERSion [0]);
      $ this- & gt; _browser_name = self :: browser_slurp;
      $ this- & gt; setrobot (true);
      $ this- & gt; setMobile (false);
      RETURN TRUE;
    }
    RETURN FALSE;
  }
  / **
   * Determine If The Browser IS Internet Explorer or Not (Last Updated 1.7)
   * @return Boolean True If The Browser IS Internet Explorer Otherwise False
   * /
  PROTECTED FUNCTION CHECKBROWSERINTERETEXPLORERER () {
    // Test for V1 - V1.5 IE
    If (Stripos ($ this- & gt; _Agent, 'Microsoft Internet Explorer')! == False) {
      $ this- & gt; setBrowser (Self :: browser_ie);
      $ this- & gt; setVersion ('1.0');
      $ ARESULT = STRISTR ($ this- & gt; _agent, '/');
      if (preg_match ('/ 308 | 425 | 426 | 474 | 0b1 / i', $ arsult)) {
        $ this- & gt; setVersion ('1.5');
      }
      RETURN TRUE;
    }
    // Test for Versions & GT; 1.5
    ELSE If (stripos ($ this- & gt; _Agent, 'Msie')! == False & amp; & amp; stripos ($ this- & gt; _Agent, 'Opera') === FALSE) {
      // See If the Browser Is The Odd MSN Explorer
      if (stripos ($ this- & gt; _agent, 'msnb')! == FALSE) {
        $ ARESULT = EXPLODE ('', STRISTR (STR_REPLACE (';', ';', $ this- & gt; _agent), 'msn'));
        $ this- & gt; setBrowser (Self :: browser_msn);
        $ this- & gt; setVersion (STR_REPLACE (array ('(', ')', ';'), '', $ arsult [1]));
        RETURN TRUE;
      }
      $ ARESULT = EXPLODE ('', STRISTR (STR_REPLACE (';', ';', $ this- & gt; _Agent), 'MSIE'));
      $ this- & gt; setBrowser (Self :: browser_ie);
      $ this- & gt; setVersion (STR_REPLACE (array ('(', ')', ';'), '', $ arsult [1]));
      RETURN TRUE;
    }
    // Test for Pocket IE
    ELSE If (stripos ($ this- & gt; _Agent, 'mspie')! == False || Stripos ($ this- & gt; _Agent, 'Pocket')! == FALSE) {
      $ ARESULT = EXPLODE ('', STRISTR ($ this- & gt; _agent, 'mspie'));
      $ this- & gt; setplatform (Self :: Platform_Windows_ce);
      $ this- & gt; setBrowser (Self :: browser_pocket_ie);
      $ this- & gt; setMobile (TRUE);
      If (stripos ($ this- & gt; _Agent, 'mspie')! == FALSE) {
        $ this- & gt; setVersion ($ ARESULT [1]);
      }
      ELSE {
        $ AVersion = Explode ('/', $ this- & gt; _agent);
        $ this- & gt; setVersion ($ AVERSion [1]);
      }
      RETURN TRUE;
    }
    RETURN FALSE;
  }
  / **
   * Determine IF The Browser Is Opera or Not (Last Updated 1.7)
   * @return Boolean True If The Browser Is Opera OtherWise False
   * /
  PROTECTED FUNCTION CHECKBROWSEROPERA () {
    If (Stripos ($ this- & gt; _Agent, 'Opera mini')! == false) {
      $ resultant = stristr ($ this- & gt; _Agent, 'Opera Mini');
      If (preg_match ('/ //', $ resultant)) {
        $ ARESULT = Explode ('/', $ Resultant);
        $ AVersion = Explode ('', $ ARESULT [1]);
        $ this- & gt; setVersion ($ AVERSion [0]); 
}
      {
        $=$);
        $&$;
      }
      $&_= :
      $&;
      
    }
     $&_ !== ) {
      $= $&_ ;
       (10.*)$/$$ ) {
        $&$;
      }
       /$ ) {
        $= "("," ",$);
        $=$;
        $&$;
      }
      {
        $=$);
        $&$?$"");
      }
      $&_= :
      
    }
    
  }
  /**
   * (1.7)
   *@*/
  ) {
     $&_ !== ) {
      $=$&_);
      $=$;
      $&$;
      $&:;
      
    }
    
  }
  /**
   * (1.7)
   *@*/
  () {
     $&_ !== ) {
      $=$&_);
      $=$;
      $&$;
      $&:;
      
    }
    
  }
  /**
   * (1.7)
   *@*/
  ) {
     $&_ !== ) {
      $=$&_);
      $=$;
      $&,$);
      $&:;
      
    }
     
  }
  /**
   * (1.7)
   *@*/
  ) {
     $&_ !== ) {
      $=$&_);
      $=$;
      $&$;
      $&:;
      
    }
    
  }
  /**
   * (1.7)
   *@*/
  ) {
     $&_ !== ) {
      $=$&_);
      $=$;
      $&$;
      $&:;
      
    }
    
  }
  /**
   * (1.7)
   *@*/
  ) {
     $&_ !== ) {