Test IP, User Agent and OS to change link.
Results 1 to 4 of 4

Thread: Test IP, User Agent and OS to change link.

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    2

    Test IP, User Agent and OS to change link.

    Hello, my name is alkarnur. I just started learning PHP a few months back and I'm really excited about it

    I normally solve my own PHP problems, but most of the time, they're very basic, seeing as how I'm a beginner really.

    I'm working on a personal site and there's a link that should be different when I visit my site using a specific OS, or a specific UserAgent, or a specific IP address.
    So normally, the link points to site A, but if any of the above three conditions is true, it points to site B.
    As an example: Normally, the link points to site A, but if I visit my site from a Mac (OS test), or an iPhone or iPad (UserAgent test), or from my home (IP test), I want it the link to be changed to point to site B.

    I already did a somewhat extensive search but all I got was fragments and I'm not sure how to put them together.

    Can anyone please help me?

    If you'd like I can post the pieces of code I found in my search

  2. #2
    Junior Member
    Join Date
    Jan 2013
    Location
    Stockholm
    Posts
    3
    I would do it like this.

    PHP Code:
    # fetch data
    $agent $_SERVER['HTTP_USER_AGENT'];
    $ip       $_SERVER['REMOTE_ADDR'];


    if(
    $ip == "your ip") {
      
    // link
    }
    else if(
    preg_match("/windows/i"$agent) == 1) {
      
    // win link
    }
    else if(
    preg_match("/linux/i"$agent) == 1) {
      
    // linux link
    }


    # etc........ 

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thank you proudfist.
    I have integrated your suggestion into a couple of other scripts. And I got this:

    Code:
    <html>
    <head>
     <title>Titley Title</title>
    </head>
    <body>
    
    <?php
    
    $blacklist = array("127.0.0.1", "10.16.2.12", "172.16.85.1");
    
    $user_ip = $_SERVER["REMOTE_ADDR"];
    
    $mobile_agents = '!(ipod|ios)!i';
    
    $agent = $_SERVER['HTTP_USER_AGENT'];
    
    $found = array_search($user_ip, $blacklist);
    
    
    if ($found !== false) { 
    		echo "B";
    
    	}
    	else if(preg_match("/macintosh|mac os x/i", $agent) == 1) {
    		echo "B";
    
    	}
    	else if(preg_match($mobile_agents, $agent) == 1) {
    		echo "B";
    
    	}
    	else {
    		echo "A";
    	}
    ?>
    </body>
    </html>
    I usually try to test for whether the positive conditions are true (because they may change later on) rather than try to work with negative conditions (i.e. "if it's not a mac or an ios it should be a windows or a linux"). That always somehow gets me in trouble.

    I'm still open to suggestions, however, if anybody thinks there's something that can be improved with the script.

  4. #4
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,885
    Quote Originally Posted by alkarnur
    I'm still open to suggestions, however, if anybody thinks there's something that can be improved with the script.
    Personally I wouldn't just drop it into the script like that. I'd have separate functions to do address- and user-agent-sniffing, and put them in a separate file so that the same code can be reused in different situations.

    Also, unless you want the actual content of the page to be different depending on what the user agent claims to be (which is often frowned upon - Google for example will count it against you if you serve their spiders something different from what you serve everyone else), then there are often alternatives to UA-sniffing where the client itself can decide what is most appropriate (such as feature testing in JavaScript and media selectors in CSS).
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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