1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to create Simple PHP Link Rotator

Discussion in 'PHP' started by Nik555, Sep 30, 2008.

  1. #1
    I wanna have a banner on my site that will direct to different advertiser each time a person clicks on it. How to do that.

    Example, i have 5 links, that i want rotate under that banner
     
    Nik555, Sep 30, 2008 IP
  2. live-cms_com

    live-cms_com Notable Member

    Messages:
    3,128
    Likes Received:
    112
    Best Answers:
    0
    Trophy Points:
    205
    Digital Goods:
    1
    #2
    live-cms_com, Sep 30, 2008 IP
  3. Nik555

    Nik555 Peon

    Messages:
    427
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yeah but how do i change thiese companies ads with my own links
     
    Nik555, Sep 30, 2008 IP
  4. ads2help

    ads2help Peon

    Messages:
    2,148
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #4
    ads2help, Sep 30, 2008 IP
  5. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Try this, its simple:

    
    <?php
    //Add as many links you want
    $mylink[1] = '<a href="http://www.aioshared.com">Free Rapidshare Search Engine</a>';
    $mylink[2] = '<a href="http://www.hotmail.com">Free 5gb Mailbox</a>';
    $mylink[3] = '<a href="http://www.mozilla.org">Fastest and Secure Web Browser</a>';
    //....
    //....
    
    //Change 3 to as many links you have added above
    $id = rand(1,3);
    
    echo $mylink[$id];
    ?>
    
    PHP:
    Hope its what you are looking for...
     
    mehdi, Oct 1, 2008 IP
  6. Xabber

    Xabber Active Member

    Messages:
    437
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Yea, use the random function. It's not hard, really... just google it up :)
     
    Xabber, Oct 1, 2008 IP
  7. nastynappy

    nastynappy Banned

    Messages:
    499
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #7
    for better solution :
    
    <?php
    //Add as many links you want
    $mylink[1] = '<a href="http://www.pegor.com">Free PHP Tutorials</a>';
    $mylink[2] = '<a href="http://www.lifestinks.info">Fast Facebook Proxy</a>';
    $mylink[3] = '<a href="http://www.mozilla.org">Fastest and Secure Web Browser</a>';
    //$mylink[4] = .....
    //$mylink[5] = ....
    
    // this will count your links itself and select a random one
    $id = rand(1,count($mylink));
    
    // this will display the random link
    echo $mylink[$id];
    ?>
    
    PHP:
     
    nastynappy, Oct 1, 2008 IP
  8. Nik555

    Nik555 Peon

    Messages:
    427
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8


    so i just copy and paste it in my html file? and where the banner goes? I wanna have same banner but each time you someone clicks on it it goes to one of the URLs
     
    Nik555, Oct 1, 2008 IP
  9. spc

    spc Well-Known Member

    Messages:
    136
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    125
    #9
    @Nik555:

    You need to use php page, not html page. i.e. page with extension .php
    See the bold part in the below code...that's how you use banners in link.

    
    $mylink[1] = '<a href="http://www.pegor.com">
    [B]<img src="images/banner.jpg" width="468" height="60" alt="some_tex_there">[/B]</a>';
    
    Code (markup):
    Modify code of nastynappy to see image banners in all links using this method.
     
    spc, Oct 1, 2008 IP
  10. Nik555

    Nik555 Peon

    Messages:
    427
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    so i make php file. then i put a banner on my html page, and under a banner i put a link to that php file , correct?
     
    Nik555, Oct 1, 2008 IP
  11. spc

    spc Well-Known Member

    Messages:
    136
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    125
    #11
    No, just rename your html page with php.

    say index.html will be index.php

    then in the body where you want to put the banner modify the code as

    
    <body>
    <div id=banner>
    <?php
    //Add as many links you want
    $mylink[1] = '<a href="http://www.pegor.com">
    <img src="images/banner1.jpg" width="468" height="60" alt="some_tex_here">
    </a>';
    $mylink[2] = '<a href="http://www.lifestinks.info">
    <img src="images/banner2.jpg" width="468" height="60" alt="some_tex_here"></a>';
    $mylink[3] = '<a href="http://www.mozilla.org"><img src="images/banner3.jpg" width="468" height="60" alt="some_tex_here"></a>';
    //$mylink[4] = .....
    //$mylink[5] = ....
    
    // this will count your links itself and select a random one
    $id = rand(1,count($mylink));
    
    // this will display the random link
    echo $mylink[$id];
    ?>
    </div>
    
    Code (markup):
    don't forget to keep the php codes inside <? and ?>

    Hope it's perfect to understand now.
     
    spc, Oct 1, 2008 IP
  12. Nik555

    Nik555 Peon

    Messages:
    427
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    i tried it with two links, but it only directs to the same no matter how often i click on it.

     
    Nik555, Oct 1, 2008 IP
  13. Nik555

    Nik555 Peon

    Messages:
    427
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    actually it works now, but it works like that: it goes 5 times to 1 link and then 5 times to another, is it possible for it to work like
    1link,2nd link,3rd link and then all over, instead of just taking random link
     
    Nik555, Oct 1, 2008 IP
  14. happpy

    happpy Well-Known Member

    Messages:
    926
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    120
    #14
    keep it simple guys

    <?
    $links="http://google.com,http://yahoo.com,http://msn.com";
    $linkx=explode(",",$links);
    shuffle($linkx);
    echo"$linkx[0]";
    ?>
    Code (markup):
     
    happpy, Oct 1, 2008 IP
  15. Nik555

    Nik555 Peon

    Messages:
    427
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    thanks, but where in that code i should put in the banner?

     
    Nik555, Oct 1, 2008 IP
  16. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #16
    If permissions are setup on your server in a way that PHP can read/write files safely, this way will make it easier to change the URLs later.

    Create a file named "items.txt" and in that file put each of the items to be rotated on their own line.
    For your 5 links the file might look like

    <a href="http://www.one.com/?ref=me">One</a>
    <a href="http://www.two.com/?ref=me">two</a>
    <a href="http://www.three.com/?ref=me">three</a>
    <a href="http://www.four.com/?ref=me">four</a>
    <a href="http://www.five.com/?ref=me">five</a>
    Code (markup):
    Then in your page, assuming it's a PHP page or HTML is setup to be parsed as PHP, you place this anywhere you want the rotation to show.

    <?php
    $items_file = 'items.txt';
    $urls = file($items_file); array_push($urls, array_shift($urls)); echo trim($urls[0]);
    file_put_contents($items_file, implode('', $urls));
    ?>
    Code (markup):
    That way you can use the same PHP everywhere you want it to happen, and changes made to "items.txt" are reflected in all of those places. That, and a plain text file is usually easier to deal with.
     
    joebert, Oct 1, 2008 IP
  17. Nik555

    Nik555 Peon

    Messages:
    427
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    what is ?Ref=me for?
    is it required?
     
    Nik555, Oct 1, 2008 IP
  18. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #18
    Nah, the contents of "items.txt" can be anything you want it to be, just remember that each line is a new item.

    Also remember, if you put an empty newline at the end of the file you will end up with an empty item in the rotation.
     
    joebert, Oct 1, 2008 IP
  19. Nik555

    Nik555 Peon

    Messages:
    427
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    can you also specify where should i put image src ......
     
    Nik555, Oct 1, 2008 IP
  20. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #20
    You put anything you want rotated on its' own line.

    If you have something like this in your webpages now

    <a href="http://domai.com/"><img src="banner.png" alt="visit *"/></a>
    Code (markup):
    Then you will replace that with the PHP I posted earlier, and your "items.txt" might look like this to alternate between two banners.

    <a href="http://domai.com/"><img src="banner.png" alt="visit *"/></a>
    <a href="http://example.com/"><img src="banner-2.png" alt="visit *"/></a>
    Code (markup):
     
    joebert, Oct 1, 2008 IP