...

View Full Version : Redirect mac and PC



ojsimon
02-08-2010, 06:17 PM
Hi
Bassically what i want to do is check the user agent and if it is a mac redirect to /mac if it is a pc redirect to /pc if it is another or unknown to /unknown does anyone know how to do this?

Thanks

_Aerospace_Eng_
02-08-2010, 06:19 PM
This may be useful to you.

http://techpatterns.com/downloads/php_browser_detection.php#php_full

ojsimon
02-08-2010, 06:25 PM
this just echoes the users details, how would i use this for a redirect?

thanks

angst
02-08-2010, 06:39 PM
this works, I just tested on pc & mac.



$Agent = $_SERVER['HTTP_USER_AGENT'];
if(preg_match('/NT/',$Agent)){
header("location: /win/");
}elseif(preg_match('/Mac/',$Agent)){
header("location: /mac/");
}

Len Whistler
02-08-2010, 06:45 PM
FYI ..... I get the following output.

Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.3) Gecko/20091020 Linux Mint/8 (Helena) Firefox/3.5.3

So you could add Linux for linux users.


if(preg_match('/Linux/',$Agent)){
header("location: /linux/");




---------

ojsimon
02-08-2010, 06:47 PM
Thank you both very much, incredibly useful

angst
02-08-2010, 06:48 PM
odd, I just tested on my linux box and this is what i got:

Lynx/2.8.7dev.11 libwww-FM/2.14 SSL-MM/1.4.1

which is why I didn't add another one for linux.

_Aerospace_Eng_
02-08-2010, 06:52 PM
this just echoes the users details, how would i use this for a redirect?

thanks

You clearly didn't look at the example. You can get the user's OS and redirect based off of the value.

ojsimon
02-08-2010, 06:57 PM
I did, sorry i'm newish to php and don't understand useragents at all. Thanks again for your help.

MattF
02-08-2010, 07:36 PM
The use of strpos/stripos would be better suited than preg_match.

angst
02-08-2010, 07:43 PM
why is that?

MattF
02-08-2010, 08:15 PM
why is that?

You're doing a straight string match hence strpos would be sufficient and is faster then the preg_ functions. There may be no real world difference, but better the lesser of the two.

angst
02-08-2010, 08:48 PM
thats not true,

I just googled it and found several sites saying different,

here is a straight benchmark:

http://dreamfall.blogspot.com/2008/02/php-benchmarks-strpos-vs-pregmatchall.html


preg_match_all: 10.6 seconds
preg_match: 9.0 seconds
str(i)pos (4 of them for prefix and 4 for state): 19.7 seconds


State prefix only

preg_match_all: 4.5 seconds
preg_match: 3.5 seconds
strpos (4 of them for prefix and 4 for state): 4.4 seconds


State only

preg_match_all: 6.6 seconds
preg_match: 5.8 seconds
stripos (4 of them for prefix and 4 for state): 16.0 seconds

angst
02-08-2010, 08:51 PM
ok, well here is anything site that says different again:

http://lzone.de/articles/php-string-search.htm

either way, for this performance really wouldn't be an issue regardless.

MattF
02-08-2010, 08:55 PM
either way, for this performance really wouldn't be an issue regardless.

That's why I mentioned there may be no real world difference. :) I'm merely working from the recommendations on the php site. For straight string matching, they suggest using the str functions over the preg ones. I've never done any benchmarks personally though.

angst
02-08-2010, 08:58 PM
yah me either, I just like to know which is best is all.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum