writing first function: browser detection. erroneous =\

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
visitor-Q
Forum Commoner
Posts: 72
Joined: Mon Feb 05, 2007 1:40 am

writing first function: browser detection. erroneous =\

Post 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?
Last edited by visitor-Q on Wed Feb 07, 2007 4:41 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

There is a function called get_browser somewhere else in your code.
visitor-Q
Forum Commoner
Posts: 72
Joined: Mon Feb 05, 2007 1:40 am

Post 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?
Last edited by visitor-Q on Wed Feb 07, 2007 4:47 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

get_browser() is a native function.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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;
}
?>
visitor-Q
Forum Commoner
Posts: 72
Joined: Mon Feb 05, 2007 1:40 am

Post 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???
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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
}
?>
visitor-Q
Forum Commoner
Posts: 72
Joined: Mon Feb 05, 2007 1:40 am

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

viewtopic.php?t=43988 the following may be of interest.
Post Reply