Page 1 of 1

writing first function: browser detection. erroneous =\

Posted: Wed Feb 07, 2007 4:37 pm
by visitor-Q
i'm trying to write a very very simple version of a browser detector. i've never written a function before, this is my first. here it is:

Code: Select all

<?php
        function get_browser(){   // <--- line 22

                $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
                $browsers = array(
                                array("msie", "internet explorer"),
                                array("firefox")
                                 );

                for($i = 0; $i < 2; $i++){
                        foreach($browsers[$i] as $key => $val){
                                if($i == 0){
                                        if(eregi($val, $browsers)){
                                                $browser = "ie";
                                        }
                                }elseif($i == 1){
                                        if(eregi($val, $browsers)){
                                                $browser = "ff";
                                        }
                                }else{
                                        $browser = "ie";
                                }
                        }
                }
                return $browser;
        }
?>
but i get this error:
Fatal error: Cannot redeclare get_browser() On line: 22

line 22 is noted above... what's going on here?

Posted: Wed Feb 07, 2007 4:41 pm
by RobertGonzalez
There is a function called get_browser somewhere else in your code.

Posted: Wed Feb 07, 2007 4:46 pm
by visitor-Q
wow, i was not aware of that... i'm working with somebody else's code... that's weird... i renamed it to get_browser_type() and it didn't bring me any errors, but then again it didn't print anything either. i added

Code: Select all

echo get_browser_today();
right after the funciton, but it didn't output anything. what am i missing to print the returned value?

Posted: Wed Feb 07, 2007 4:46 pm
by feyd
get_browser() is a native function.

Posted: Wed Feb 07, 2007 4:53 pm
by RobertGonzalez
Sometimes I love it, sometimes I hate it, when I learn new functions that I had not known of before.

Thanks for teaching me another one feyd.

Posted: Wed Feb 07, 2007 4:59 pm
by RobertGonzalez
I wrote a little something once to check if the browser is IE (because standard browsers can hack it, but IE chokes on standards). It might not be exactly what you want, but it might be helpful in some capacity.

Code: Select all

<?php
/**
 * Checks if the users browser is internet explorer
 * @access public 
 * @return boolean Returns true is the browser is IE, false otherwise
 */
public function check_ie_browser()
{
	// Get the users browser
	$useragent = $_SERVER['HTTP_USER_AGENT'];

	// Look for IEs fingerprint
	if (strpos($useragent, 'MSIE') !== false && strpos($useragent, 'Opera') === false && strpos($useragent, 'Netscape') === false)
	{
		// Read and check the result
		if (preg_match("/MSIE ([0-9]{1}\.[0-9]{1,2})/", $useragent))
		{
			return true;
		}
	}
	
	return false;
}
?>

Posted: Wed Feb 07, 2007 5:06 pm
by visitor-Q
feyd wrote:get_browser() is a native function.
thanks feyd... i wondered why it was highlighted when i typed in that function name in my vim editor, but when i searched php.net/get_browser it didn't bring anything up... i should have typed in php.net/function.get_browser. thank you feyd, and Everah.

everah, nice function. tell me, what is the purpose of adding 'public' before you begin the function. and what would it return if you printed the returned value of that function? how did you use it within your existing code?


also, i get this error when i write

Code: Select all

echo get_browser();
Warning: get_browser(): browscap ini directive not set.
i believe i may just have to simply uncomment something in my php.ini file???

Posted: Wed Feb 07, 2007 5:17 pm
by RobertGonzalez
visitor-Q wrote: tell me, what is the purpose of adding 'public' before you begin the function. and what would it return if you printed the returned value of that function? how did you use it within your existing code?
Public is a class visibility declaration. It is used in PHP5 OOP. Basically it makes the function available to the entire application for use.

Basically all it does is tell you whether your user is using IE or not. So it returns true if the browser is IE, false otherwise. It can be used like...

Code: Select all

<?php
if (check_ie_browser())
{
    // Do something only IE sees
}
else
{
    // Do something standard
}
?>

Posted: Wed Feb 07, 2007 5:22 pm
by visitor-Q
thank's for the info everah. i'm not familiar with OOP, but i'm not too fond of using classes with PHP. it is my understanding that some people swear by it, yet others despise it. is it necessary to evolve to understand/use classes at a high-level of PHP programming?

Posted: Wed Feb 07, 2007 5:55 pm
by RobertGonzalez
Code how you feel most comfortable with. There have been many a heated debate over procedural versus OO programming, and I am not about to open that can of crap again. If you are more of a procedural style coder, keep going. At some point I would venture to say that you will need to learn OOP just so you can keep up with evolving codebases. But for your own projects, you should be alright to code how you want.

Posted: Wed Feb 07, 2007 7:26 pm
by feyd
viewtopic.php?t=43988 the following may be of interest.