1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Simple text link rotator?

Discussion in 'PHP & Perl' started by Erik, Mar 19, 2009.

  1. Erik

    Erik Regular Member

    Joined:
    Dec 14, 2008
    Messages:
    302
    Likes Received:
    411
    Occupation:
    Monitoring my affiliate accounts.
    Location:
    Planet Earth
    Home Page:
    O.K. I looked around and I can't seem to find this.

    Basically I have a 'Click Here' Link attached to a Url. What I'd like to do is find a way to make that link rotate.

    In other words Person A clicks on a link and goes to URL 1 on click. Person B goes to URL 2 on click, etc.

    Random would also be better.

    Can anyone help me out with this?
     
  2. 00CivicEX

    00CivicEX Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 3, 2009
    Messages:
    293
    Likes Received:
    214
    Here is Javascript that will allow you to do it, just replace the href with your link and the text. You can also make it rotate to images or whatever...if you need to know howto do that just let me know. Oh yeh, it goes in your HEAD tags

    Code:
    <script language="JavaScript1.2">
    var howOften = 2; //number often in seconds to rotate
    var current = 0; //start the counter at 0
    var ns6 = document.getElementById&&!document.all; //detect netscape 6
    // place your images, text, etc in the array elements here
    var items = new Array();
        items[0]="<a href='yourlink'>Text with a link</a>"; //each link
        items[1]="<a href='yourlink'>Text with a link</a>"; //link 2 and so on
        items[2]="<a href='yourlink'>Text with a link</a>";
        items[3]="<a href='yourlink'>Text with a link</a>";
        items[4]="<a href='yourlink'>Text with a link</a>";
    function rotater() {
        if(document.layers) {
            document.placeholderlayer.document.write(items[current]);
            document.placeholderlayer.document.close();
        }
        if(ns6)document.getElementById("placeholderdiv").innerHTML=items[current] 
            if(document.all)
                placeholderdiv.innerHTML=items[current];
        current = (current==items.length) ? 0 : current + 1; //increment or reset
        setTimeout("rotater()",howOften*1000);
    }
    window.onload=rotater;
    //->
    </script>
    
     
    • Thanks Thanks x 1
    Last edited: Mar 19, 2009
  3. Erik

    Erik Regular Member

    Joined:
    Dec 14, 2008
    Messages:
    302
    Likes Received:
    411
    Occupation:
    Monitoring my affiliate accounts.
    Location:
    Planet Earth
    Home Page:
    dupe post- remove
     
    Last edited: Mar 19, 2009
  4. Erik

    Erik Regular Member

    Joined:
    Dec 14, 2008
    Messages:
    302
    Likes Received:
    411
    Occupation:
    Monitoring my affiliate accounts.
    Location:
    Planet Earth
    Home Page:
    hey thanks for the help, If i put this code in my header and fill in the urls there, what do I need to place in the body to display those rotating links?
     
  5. 00CivicEX

    00CivicEX Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 3, 2009
    Messages:
    293
    Likes Received:
    214
    Here use this, cause to much to explain changing the placeholder
    Code:
    <script language="JavaScript1.2">
    /*
    Rotating image or text(You can use for changing banners)
    Author: Narayan Chand Thakur
    Source: 
    This may be used freely as long as this message is intact.
    */
    <!--
    //you may add your image file or text below
    var item=new Array()
    item[0]="<a href='#'><img src='ballon2.gif' border='0'></a>"
    item[1]="<a href='#'><img src='ballon3.gif' border='0'></a>"
    item[2]="<a href='#'><img src='ballon4.gif' border='0'></a>"
    item[3]="<strong><font face='arial' size='4' color='red'>Text without a Link!</font></strong>"
    item[4]="<a href='#'><font face='arial' size='4' color='darkgreen'><B>This text has Link</B></font></a>"
    item[5]="<a href='#'><font face='arial' size='4' color='darkgreen'><B>How do you like that?</B></font></a>"
    var current=0
    var ns6=document.getElementById&&!document.all
    function changeItem(){
    if(document.layers){
    document.layer1.document.write(item[current])
    document.layer1.document.close()
    }
    if(ns6)document.getElementById("div1").innerHTML=item[current]
    {
    if(document.all){
    div1.innerHTML=item[current]
    }
    }
    if (current==5) current=0
    else current++
    setTimeout("changeItem()",2000)
    }
    window.onload=changeItem
    //-->
    </script>
    
    and then put this in your body, remember to change the left and right so its inthe position you want.
    Code:
    <layer id="layer1" left="210" top="250" ></layer>
    <div id="div1" style="position:absolute;left:210;top:250">
    </div>
    
     
  6. Erik

    Erik Regular Member

    Joined:
    Dec 14, 2008
    Messages:
    302
    Likes Received:
    411
    Occupation:
    Monitoring my affiliate accounts.
    Location:
    Planet Earth
    Home Page:
    Thank you sir. this should work perfectly
     
  7. ukescuba

    ukescuba Jr. VIP Jr. VIP Premium Member

    Joined:
    Feb 24, 2008
    Messages:
    994
    Likes Received:
    634
    Occupation:
    Mobile Marketer & QR Code Junkie
    Location:
    San Antonio, TX
    Home Page:
    i can write up a quick php version if you need it...
     
  8. 00CivicEX

    00CivicEX Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 3, 2009
    Messages:
    293
    Likes Received:
    214
    Here is php one if you need it.

    Code:
    <?php
    //Add as many links you want
    $mylink[1] = '<a href="YOURLINK">YOUTEXT</a>';
    $mylink[2] = '<a href="YOURLINK">YOURTEXT</a>';
    $mylink[3] = '<a href="YOURLINK">YOURTEXT</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];
    ?>
    
     
    • Thanks Thanks x 6
  9. Erik

    Erik Regular Member

    Joined:
    Dec 14, 2008
    Messages:
    302
    Likes Received:
    411
    Occupation:
    Monitoring my affiliate accounts.
    Location:
    Planet Earth
    Home Page:
    dude the second option worked wonders for me! Thank you very very very much!
     
  10. 00CivicEX

    00CivicEX Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 3, 2009
    Messages:
    293
    Likes Received:
    214
    No problem, just hit me up if you need any other kind of code....if i dont know i can make it...if i cant make it...i can find it ;)
     
  11. charlietree

    charlietree Registered Member

    Joined:
    Feb 25, 2008
    Messages:
    88
    Likes Received:
    32
    Occupation:
    Website Design, Online Reputation Management, Bran
    Location:
    Dunedin, New Zealand
    Home Page:
    I am looking for something similiar but I would need it to just show random webpages...as in it would just be a redirect i guess to a list of sites and chosen at random...

    Does that make sense? Like it would go to yoursite.com/links.html but its really showing other sites chosen from a list of your choosing...

    I guess its called a link rotator or something...
     
  12. 00CivicEX

    00CivicEX Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 3, 2009
    Messages:
    293
    Likes Received:
    214
    Not sure if this is what you mean but here it is, it will load a random page upon opening the page that is code is in.

    Code:
    <head>
    <script type="text/javascript" language="javascript">
    var randomlinks=new Array("[URL]http://DOMAIN.com[/URL]", "[URL]http://DOMAIN.com[/URL]", "[URL]http://DOMAIN.com[/URL]", "[URL]http://DOMAIN.com[/URL]", "[URL]http://www.DOMAIN.com[/URL]");
    location.href = randomlinks[Math.floor(Math.random()*randomlinks.length)];
    </script>
    </head>
     
    • Thanks Thanks x 2
  13. egood

    egood Guest

    is there a way to change this from random to just a regular rotate? example site 1 site 2 site 3 site 1 site 2 site 3 etc. Does anybody have a regular url rotator script?

    thanks



     
  14. 10kay

    10kay Registered Member

    Joined:
    Oct 13, 2007
    Messages:
    87
    Likes Received:
    38
    yes, like egood, looking for a sequential script ... 1 then 2, then 3 ...4, 5 .... then loops back to 1 ... in php would be great.
    thnx guys!


     
  15. egood

    egood Guest

    does anybody have a sequential link rotator script? i'm only finding ones with random link rotation!!
     
  16. denight

    denight Registered Member

    Joined:
    Apr 7, 2009
    Messages:
    58
    Likes Received:
    26
    That would require SQL access or write access to the file system to store the previous link in the database...

    I assume you have one or the other, right?

    dN
     
  17. egood

    egood Guest

    Let me rephrase that. I'm looking for a url rotator script. example....If a person types in or clicks whatever.com they'll be directed to a different website each time in sequential order. I need all my sites to get an equal number of clicks
     
  18. 00CivicEX

    00CivicEX Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 3, 2009
    Messages:
    293
    Likes Received:
    214
    SQL is not needed you can use one of the following:

    Code:
    <?php
    $linksfile ="urlrotator.txt";
     
    $urls = file("$linksfile");
    $urls=str_replace('\n','',$urls);
    $goto=array_shift($urls);
    array_push($urls,$goto);
    $strurls=implode('\n',$urls);
     
    $handle=fopen($linksfile,"w"); 
     
    fwrite($handle,$strurls); 
     
    fclose($handle);
     
    header("Location: $goto");
     
    ?>
    There are two issues here, a. performance hit of doing a large amount of disk writes and b. risk of getting your data messed up by two people writing to it at the same time. Two sollutions are needed. To avoid the excess disk writes you could have a seperate file with a number in, each time the page is hit this number is incremented untill it reaches the number of links and then goes back to 0. To avoid the possiblity of data ****up you can use php's file locking flock. Here's the modded code.

    Code:
    <?php
    $linksfile ="urlrotator.txt";
    $posfile = "pos.txt";
     
    $links = file($linksfile);
    $numlinks = count($linksfile);
     
    $fp = fopen($posfile, 'r+') or die("Failed to open posfile");
    flock($fp, LOCK_EX);
    $num = fread($fp, 1024);
    if($num<$numlinks-1) {
    fwrite($fp, $num+1);
    } else {
    fwrite($fp, 0);
    }
    flock($fp, LOCK_UN);
    fclose($fp);
     
    header("Location: {$links[$num]}");
    ?>
    You will need to create the urlrotator.txt and postxt file and upload it to the directory the php file is located. Also make sure the folder it is in, is writeable, just change the permissions in your FTP Program. Hope this helps.
     
    • Thanks Thanks x 2
    Last edited: Apr 22, 2009
  19. egood

    egood Guest

    thanks civic,but i'm a newbie when it comes to this coding stuff! Is there a way that you can modify this script you posted to be sequential? i was just looking for something i can paste on a blogger or wordpress blog.

    thanks


     
  20. 00CivicEX

    00CivicEX Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 3, 2009
    Messages:
    293
    Likes Received:
    214
    The php is basically a copy and paste....just make the rotatorurl.txt and upload then use the php code i gave ya...and thats it.