WEB blocking/redirecting useragent? PHP

Discussion in 'OT Technology' started by MSTRMOO, Mar 1, 2009.

  1. MSTRMOO

    MSTRMOO Active Member

    Joined:
    Jul 19, 2005
    Messages:
    36,312
    Likes Received:
    0
    hey, i dont really know where to start with this... how would i go about redirecting a user based on useragent? in php...
     
  2. MSTRMOO

    MSTRMOO Active Member

    Joined:
    Jul 19, 2005
    Messages:
    36,312
    Likes Received:
    0
    like.. wat do i tell it to search for in the useragent string?

    cause like.. it could be somehting like this

    Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)

    how do i make it look for "googlebot" in the string and redirecting that user, instead of redirecting everyone with mozilla 5.0?
     
  3. hank85

    hank85 sudo shred /dev/sda -f -v -z --iterations=6

    Joined:
    Jul 23, 2008
    Messages:
    4,360
    Likes Received:
    0
    Catch the UserAgent then look for it with the below code.

    Code:
    if(strstr($UserAgent,"googlebot"){
     Redirect code here.
    }
    strstr checks to see if a string exists within a string, if it does it returns whatever comes after. If not, it returns false.

    This is a method of cloaking and usually instead of a redirect, people tend to just show google what they'd prefer the crawler to see. So:

    Code:
    if(strstr($UserAgent,"googlebot") || strstr($UserAgent, "bot")){
      Stuff google bot sees.
    }else{
     Stuff  your users see.
    }
    
     
  4. MSTRMOO

    MSTRMOO Active Member

    Joined:
    Jul 19, 2005
    Messages:
    36,312
    Likes Received:
    0
    thank you!
     
  5. MSTRMOO

    MSTRMOO Active Member

    Joined:
    Jul 19, 2005
    Messages:
    36,312
    Likes Received:
    0
    i just used that as an example of a unique useragent.. im not blocking/redirecting google
     
  6. MSTRMOO

    MSTRMOO Active Member

    Joined:
    Jul 19, 2005
    Messages:
    36,312
    Likes Received:
    0
    what about blocking using .htaccess?

    using something like this

    will this look for "BadBot" anywhere in the useragent string or just the first word or wat?

    #get rid of the bad bot
    RewriteEngine on
    RewriteCond %{HTTP_USER_AGENT} ^BadBot
    RewriteRule ^(.*)$ http://go.away/


    i think this only looks at the first word of the string.. anyway to make it look for a word anywhere in the string?
     

Share This Page