November, 2008

Easy to use PHP browser detection script

I’m always on the lookout for easy to use browser detection scripts and googled my way across this one by Harald Hope a minute ago:

< ?php
/*
Script Name: Simple 'if' PHP Browser detection
Author: Harald Hope, Website: http://TechPatterns.com/
Script Source URI: http://TechPatterns.com/downloads/php_browser_detection.php
Version 2.0.2
Copyright (C) 29 June 2007

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Get the full text of the GPL here: http://www.gnu.org/licenses/gpl.txt

Coding conventions:

http://cvs.sourceforge.net/viewcvs.py/phpbb/phpBB2/docs/codingstandards.htm?rev=1.3

*/

/*
the order is important, because opera must be tested first, and ie4 tested for before ie general
same for konqueror, then safari, then gecko, since safari navigator user agent id's with 'gecko' in string.
note that $dom_browser is set for all  modern dom browsers, this gives you a default to use, unfortunately we
haven't figured out a way to do this with actual method testing, which would be much better and reliable.

Please note: you have to call the function in order to get access to the variables, you call it by this:

browser_detection('browser');

then put you code that you want to use the variables with after that.

*/

function browser_detection( $which_test ) {

	// initialize the variables
	$browser = '';
	$dom_browser = '';

	// set to lower case to avoid errors, check to see if http_user_agent is set
	$navigator_user_agent = ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) ? strtolower( $_SERVER['HTTP_USER_AGENT'] ) : '';

	// run through the main browser possibilities, assign them to the main $browser variable
	if (stristr($navigator_user_agent, "opera"))
	{
		$browser = 'opera';
		$dom_browser = true;
	}

	elseif (stristr($navigator_user_agent, "msie 4"))
	{
		$browser = 'msie4';
		$dom_browser = false;
	}

	elseif (stristr($navigator_user_agent, "msie"))
	{
		$browser = 'msie';
		$dom_browser = true;
	}

	elseif ((stristr($navigator_user_agent, "konqueror")) || (stristr($navigator_user_agent, "safari")))
	{
		$browser = 'safari';
		$dom_browser = true;
	}

	elseif (stristr($navigator_user_agent, "gecko"))
	{
		$browser = 'mozilla';
		$dom_browser = true;
	}

	elseif (stristr($navigator_user_agent, "mozilla/4"))
	{
		$browser = 'ns4';
		$dom_browser = false;
	}

	else
	{
		$dom_browser = false;
		$browser = false;
	}

	// return the test result you want
	if ( $which_test == 'browser' )
	{
		return $browser;
	}
	elseif ( $which_test == 'dom' )
	{
		return $dom_browser;
		//  note: $dom_browser is a boolean value, true/false, so you can just test if
		// it's true or not.
	}
}

/*
you would call it like this:

$user_browser = browser_detection('browser');

if ( $user_browser == 'opera' )
{
	do something;
}

or like this:

if ( browser_detection('dom') )
{
	execute the code for dom browsers
}
else
{
	execute the code for non DOM browsers
}

and so on.......

*/
? >

Needs some updating on the browser versions but all in all it’s a real good one and easy to understand for us PHP noobs. Adding it to the archives! :D

Clearing an input-field of it´s value – and then put it back.

After about 12 years of webdesigning I finally figured out a simple way of having an input-field cleared of it’s initial text value when focused and then reset when unfocused…

<input type="text" name="searchbox" id="searchbox" value="Inital text" onclick="this.value='';" onmouseout="this.value='Inital text';" />

Resulting in this:

That only took me a decade to figure out… Me stoopid. But hey, Rome wasn’t built in a day guys!! ;)

Update!

Got a tip from a reader about a better (read: less annoying…) way of doing it:

<input type="text" name="searchbox" id="searchbox" value="Inital text" onfocus="this.value='';" onblur="this.value='Inital text';" />

This clears the input at the same way, only it’s not reset until you click outside the field. Much better. :)

"It took you twelve years?!" I really think that last exclamation mark was uncalled for... *lol*

A rather nice morning


Not a bad way to start off the day. Not bad at all...

Avoid Internet Explorer compability issues with IETester

IETester Just discovered a very useful application for testing the IE compability of webpages across different versions of the browser, IETester.

That’s going to save me a lot of time waiting in line for browsershots… :) (Though browsershots.org is great for checking the compability across platforms, like PC <-> Mac and so on.)

Clever 404-page

How clever is this?? I’m feeling inspired!

A Dear John

I’m so sorry TTFtitles… We’ve had some great times together but… I’ve found something better. *insert dramatic squirrel here*

The Facelift script (also available as a WP plugin) achieves image text replacement but without stripping the text tags (h1, h2 and so on) from your code, which helps improve your search engine ranking.

Also, the text becomes editable via css, which I find just lovely because makes it so much easier to manage and edit. When creating a WP theme I won’t have to rely on an external plugin, instead I simply put the script along with the theme-files. Much better, especially if you’re releasing the theme for download.

User friendly? Let’s say it like this: my knowledge in javascript doesn’t even qualify me as a noob and it took me about 30 minutes to completely figure out how everything worked and starting to modify it to suit the theme I’m working on. And I’m not using the plugin either, just the script. So yeah, it’s easy to use;)

ps. A “Dear John”-letter refers to a letter written by a woman to her husband or boyfriend to inform him their relationship is over, usually because she has found another man… (wikipedia) lol

I help you and you help the next guy

YodaThe thing I appreciate most with the internet is the exchange of knowledge that is made possible by it.

Learning to design web pages is just one example – thanks to thousands of sites, forums, tutorials and the open source community you can pretty much start out from scratch, not knowing a single line of code, and end up a decent web designer.

I would like to contribute to this and that’s why I’ve set up a contact form in the sidebar of this blog here – my idea is that if you have a question (relating in some way to webdesign) that you can’t find the answer for; write it down and send it to me.

I’m especially turning to you beginners (noobs! :D ) out there; if you haven’t quite mastered CSS yet or the W3C validation screen just leaves you looking like a question mark, then maybe I can help out.

I’m a noob myself when it comes to PHP for example and I know how it sometimes can be difficult to find the answers your looking for because you don’t even know which question to ask… And having to much information is sometimes as confusing as having none at all!

So, if I’m able to answer you question, I’ll do so in a blog post and that way make it accessible for the next person that has the same question as you did.

And if I can’t answer your question, maybe I can figure it out and learn something new myself.

And in the event that I don’t know the answer and can’t find out – better luck next time, ey… ;)

An ode to Fallout 3

Fall Out 3 About two weeks ago my ten year wait was finally over – Fallout 3 was released.

Fallout isn’t just a game; it’s a way of life! Foul language, big guns and bloody messes… What’s not to love?

I was happily surprised that Bethesda actually has done a very good job in keeping the original feel of the game, but without simply replicating it’s predecessor. And as I said; we are quite a few out there that have been waiting eagerly for a decade and our expectations haven’t been small!

Actually, the only thing that bugs me with Fallout 3 is the levelcap. At level 20 I’m just getting warmed up, dammit!!