davery Posted August 10, 2011 Share Posted August 10, 2011 Greetings, I'm using the get_browser function that can determine what browser the user is using. The following code will display the name, version and platform of the browser: <?php function getBrowser() { $u_agent = $_SERVER['HTTP_USER_AGENT']; $bname = 'Unknown'; $platform = 'Unknown'; $version= ""; //First get the platform? if (preg_match('/linux/i', $u_agent)) { $platform = 'linux'; } elseif (preg_match('/macintosh|mac os x/i', $u_agent)) { $platform = 'mac'; } elseif (preg_match('/windows|win32/i', $u_agent)) { $platform = 'windows'; } // Next get the name of the useragent yes seperately and for good reason if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) { $bname = 'Internet Explorer'; $ub = "MSIE"; } elseif(preg_match('/Firefox/i',$u_agent)) { $bname = 'Mozilla Firefox'; $ub = "Firefox"; } elseif(preg_match('/Chrome/i',$u_agent)) { $bname = 'Google Chrome'; $ub = "Chrome"; } elseif(preg_match('/Safari/i',$u_agent)) { $bname = 'Apple Safari'; $ub = "Safari"; } elseif(preg_match('/Opera/i',$u_agent)) { $bname = 'Opera'; $ub = "Opera"; } elseif(preg_match('/Netscape/i',$u_agent)) { $bname = 'Netscape'; $ub = "Netscape"; } // finally get the correct version number $known = array('Version', $ub, 'other'); $pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#'; if (!preg_match_all($pattern, $u_agent, $matches)) { // we have no matching number just continue } // see how many we have $i = count($matches['browser']); if ($i != 1) { //we will have two since we are not using 'other' argument yet //see if version is before or after the name if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){ $version= $matches['version'][0]; } else { $version= $matches['version'][1]; } } else { $version= $matches['version'][0]; } // check if we have a number if ($version==null || $version=="") {$version="?";} return array( 'userAgent' => $u_agent, 'name' => $bname, 'version' => $version, 'platform' => $platform, 'pattern' => $pattern ); } // now try it $ua=getBrowser(); $yourbrowser= "Your browser: " . $ua['name'] . " " . $ua['version'] . " on " .$ua['platform'] . " reports: <br >" . $ua['userAgent']; print_r($yourbrowser); ?> I'm trying to add an "if" conditional to say that when the user is using the Mozilla Firefox browser, print the text "Hello World". I just recently tried adding this before the ?> but no luck: if ($yourbrowser == "Mozilla Firefox") { print("Hello World"); } Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/244423-get_broswer-conditional/ Share on other sites More sharing options...
TeNDoLLA Posted August 10, 2011 Share Posted August 10, 2011 Should not it be if ($ua['name'] == "Mozilla Firefox") { print("Hello World"); } Quote Link to comment https://forums.phpfreaks.com/topic/244423-get_broswer-conditional/#findComment-1255410 Share on other sites More sharing options...
davery Posted August 10, 2011 Author Share Posted August 10, 2011 Yes!! It worked! Thank you......Now instead of the "hello world", I wanna display a stylesheet that will show in Firefox. Quote Link to comment https://forums.phpfreaks.com/topic/244423-get_broswer-conditional/#findComment-1255417 Share on other sites More sharing options...
TeNDoLLA Posted August 10, 2011 Share Posted August 10, 2011 To do that you would just echo out the code for linking the stylesheet in your page like you would echo out any HTML instead of the hello world. Quote Link to comment https://forums.phpfreaks.com/topic/244423-get_broswer-conditional/#findComment-1255425 Share on other sites More sharing options...
davery Posted August 10, 2011 Author Share Posted August 10, 2011 What I need is actually not a stylesheet but a php file. How would I link to a php file inside of php? Quote Link to comment https://forums.phpfreaks.com/topic/244423-get_broswer-conditional/#findComment-1255482 Share on other sites More sharing options...
TeNDoLLA Posted August 10, 2011 Share Posted August 10, 2011 Using require_once() or include_once(). http://fi.php.net/manual/en/function.include-once.php Quote Link to comment https://forums.phpfreaks.com/topic/244423-get_broswer-conditional/#findComment-1255483 Share on other sites More sharing options...
davery Posted August 10, 2011 Author Share Posted August 10, 2011 The problem lies within Firefox. Theres a form on the website that is aligned completely off the page in Firefox. In Chrome, its aligned within the space that it should be. Its the same code, just different browsers. Im using Firefox 5.0.1 btw. Quote Link to comment https://forums.phpfreaks.com/topic/244423-get_broswer-conditional/#findComment-1255507 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.