Web Analytics Made Easy -
StatCounter PHP Links Randomizer or Rotator - CodingForum

Announcement

Collapse
No announcement yet.

PHP Links Randomizer or Rotator

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP Links Randomizer or Rotator

    Hello!

    I have this php script on my page that does geotargeting, redirecting the visitors to another page based on their IP address:

    Code:
    <?php
    
    include("geoip.inc");
    $ip=$_SERVER['REMOTE_ADDR'];
    $gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
    
    $country_code = geoip_country_code_by_addr($gi, "$ip");
    
    // Country name is not used so commented
    // Get Country Name based on source IP
    //$country = geoip_country_name_by_addr($gi, "$ip");
    
    geoip_close($gi);
    
    switch($country_code)
    
        {
            case "US": header("Location: http://www.aaa.com"); break;
            case "CA": header("Location: http://www.bbb.com"); break;
            case "UK": header("Location: http://www.ccc.com"); break;
            case "AU": header("Location: http://www.ddd.coml"); break;
            default: header("Location: http://www.eee.com");
    }
    ?>
    I want to add a feature that allows to send the visitor to a random site. e.g. let's say a visitor from US comes to the site, normally he will get redirected to www.aaa.com. Instead of this i'd like him to be redirected to a random site out of 3 or 4 different sites i choose. So each time a visitor enters the page he will be redirected to any of the sites i choose.

    In other words i'd like to add something like a rotator of links that allows me to send the visitor not always to the same site like my current setup. Instead of this sending him to any of the 3 or 4 sites i choose.

    Any help would be greatly appreciated.

  • #2
    i wrote something a like to someone here..

    <?php
    $min = 1;
    $max = 4;
    $lucky_visitor = rand($min, $max); // makes a random num
    switch($lucky_visitor)
    {
    case 1: header("Location: http://www.aaa.com"); break;
    case 2: header("Location: http://www.bbb.com"); break;
    case 3: header("Location: http://www.ccc.com"); break;
    case 4: header("Location: http://www.ddd.coml"); break;
    }

    ?>
    :. -= | Eran | =- .:
    __________________

    Comment


    • #3
      i wrote something a like to someone here..

      <?php
      $min = 1;
      $max = 4;
      $lucky_visitor = rand($min, $max); // makes a random num
      switch($lucky_visitor)
      {
      case 1: header("Location: http://www.aaa.com"); break;
      case 2: header("Location: http://www.bbb.com"); break;
      case 3: header("Location: http://www.ccc.com"); break;
      case 4: header("Location: http://www.ddd.coml"); break;
      }

      ?>
      :. -= | Eran | =- .:
      __________________

      Comment


      • #4
        Erans example would work, and if you don't want to use SWITCH you can just use IF, ELSEIF, and ELSE statements for it, with the RAND code.
        Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

        Comment


        • #5
          Thank you guys for the responses

          I'm kind of a noob. What i need to be done is to have a visitor from each of the locations (US, UK, AU, etc..) redirected to a random website from a list of 3 or 4 different websites i choose.

          Where do i put the list of websites to be chosen by the script so it can redirect my visitor?


          I will be needing a set of about 4 sites for each location. Each set of websites needs to be different, it can't be the same 4 sites for all locations. So, do i have to repeat this code for each location? i'm kinda lost in here, would you please explain how do i include this on my current code.

          e.g if a have a UK visitor, i want him to be redirected to any of the sites i will be choosing or putting on a list, sites that are intended for a UK vistor.
          Let's say i have another visitor, this time from US, i want him redirected to any of the 4 sites intended for a US visitor.

          In other words:
          My current setup as you can see has only 1 website availabe for each location. I want to have about 4 different websites available for each location that will be shown to a visitor randomly, i don't want the same site showing all the time like it's coded right now on the script i showed.

          Comment


          • #6
            Trying to explain better (not so confusing) it will be something like this:


            My script Right now works like this:
            • A visitor from US goes to my website,
            • the script redirects this visitor to another website (US related content website), let's say this site is called www.us-site1.com



            I want the script to work like this:
            • A visitor from US comes to my website


            Right now i just have 1 redirect option available for each location. And what i need is to have 4 different redirect options for each location (4 for US, 4 for UK, 4 for CA, 4 for AU and 4 for the rest of the world)

            Comment


            • #7
              The code below will randomly select a website from the 4 in the list. You would need one block of code for US, UK, CA, AU and others.

              PHP Code:
              <?php
                $us_websites 
              = array('http://www.yahoo.com',
                                     
              'http://www.google.com',
                                     
              'http://www.ebay.com',
                                     
              'http://www.facebook.com');

                
              shuffle($us_websites);

                foreach (
              $us_websites as $us_website ) {
                
              $us_website $us_website;
                }

              ?>
              PHP Code:
              case "US"header("Location: $us_website"); break; 
              I'm not sure about the the syntax in the header function.


              ---------
              Last edited by Len Whistler; Apr 16, 2009, 03:37 AM.
              Leonard Whistler

              Comment


              • #8
                Originally posted by Len Whistler View Post
                The code below will randomly select a website from the 4 in the list. You would need one block of code for US, UK, CA, AU and others.

                PHP Code:
                <?php
                  $us_websites 
                = array('http://www.yahoo.com',
                                       
                'http://www.google.com',
                                       
                'http://www.ebay.com',
                                       
                'http://www.facebook.com');

                  
                shuffle($us_websites);

                  foreach (
                $us_websites as $us_website ) {
                  
                $us_website $us_website;
                  }

                ?>
                PHP Code:
                case "US"header("Location: $us_website"); break; 
                I'm not sure about the the syntax in the header function.


                ---------
                hey Len, this is exactly what i meant!
                Thanks a lot!!!
                I really really appreciate it!

                I'm gonna try it right now.

                Comment

                Working...
                X