Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Redirect mac and PC

    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

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New Coder
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    5
    Thanked 0 Times in 0 Posts
    this just echoes the users details, how would i use this for a redirect?

    thanks

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    this works, I just tested on pc & mac.

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


  • Users who have thanked angst for this post:

    ojsimon (02-08-2010)

  • #5
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    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.

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



    ---------
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    ojsimon (02-08-2010)

  • #6
    New Coder
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thank you both very much, incredibly useful

  • #7
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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.

  • #8
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Quote Originally Posted by ojsimon View Post
    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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #9
    New Coder
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I did, sorry i'm newish to php and don't understand useragents at all. Thanks again for your help.

  • #10
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    The use of strpos/stripos would be better suited than preg_match.

  • #11
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    why is that?

  • #12
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by angst View Post
    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.

  • #13
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    thats not true,

    I just googled it and found several sites saying different,

    here is a straight benchmark:

    http://dreamfall.blogspot.com/2008/0...gmatchall.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

  • #14
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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.

  • #15
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by angst View Post
    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •