- Beta Tester
- Bought between 10 and 49 items
- Contributed a Tutorial to a Tuts+ Site
- Exclusive Author
- Grew a moustache for the Envato Movember competition
- Has been a member for 3-4 years
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Sold between 1 000 and 5 000 dollars
- United States
Hey, this might seem like an easy question but everything I try doesn’t seem to work. I have an image, but I want the link on the image to change on refresh. I want the picture to stay the same but every time the page is refreshed I want the link to change back and forth from one link to another. Kinda like a referral link so you can split it between two people. Thanks for any help!
- Author had a File in an Envato Bundle
- Author had a Free File of the Month
- Author was Featured
- Bought between 10 and 49 items
- Exclusive Author
- Has been a member for 4-5 years
- Item was Featured
- Referred between 50 and 99 users
- Sold between 10 000 and 50 000 dollars
Could you explain in more detail what you want? From what I can gather, you want to output a random link each time the page refreshes? Try using this PHP :
<?php $rand = rand(0, 1);
$links[0] = "http://google.com";
$links[1] = "http://yahoo.com";
echo '<a href="' . $links[$rand] . '?>Random Link';
?>
It generates a random number between 0 and 1. You create an array containing two values assigned to the 0 and 1 keys.
Depending on what random number is created, the appropriate link is output.
But I’m not too sure if that’s what you’re asking for.
- Beta Tester
- Bought between 10 and 49 items
- Contributed a Tutorial to a Tuts+ Site
- Exclusive Author
- Grew a moustache for the Envato Movember competition
- Has been a member for 3-4 years
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Sold between 1 000 and 5 000 dollars
- United States
Okay, i’ll give an example. I have an image, let’s say a ThemeForest ad, and I want to put a referral link on this image, but let’s say I have 2 different ThemeForest accounts, and I want both referral links on one image, but I want them to switch back and forth every time the page is refreshed. This is just an example. Hope this makes more since.
- Author had a File in an Envato Bundle
- Author had a Free File of the Month
- Author was Featured
- Bought between 10 and 49 items
- Exclusive Author
- Has been a member for 4-5 years
- Item was Featured
- Referred between 50 and 99 users
- Sold between 10 000 and 50 000 dollars
In which case the example PHP code I gave will work fine. In your case, it’ll look more like:
<?php $rand = rand(0, 1);
$links[0] = "THEMEFOREST-LINK-1";
$links[1] = "THEMEFOREST-LINK-2";
echo '<a href="' . $links[$rand] . '?><img src="IMAGE" />';
?>
Just fill in the gaps.
- Beta Tester
- Bought between 10 and 49 items
- Contributed a Tutorial to a Tuts+ Site
- Exclusive Author
- Grew a moustache for the Envato Movember competition
- Has been a member for 3-4 years
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Sold between 1 000 and 5 000 dollars
- United States
Great, it worked. Thanks!
Alex, you coding PHP !? 
- Community Moderator
- United States
- Was featured in a podcast
- Attended a Community Meetup
- Author had a Free File of the Month
- Bought between 50 and 99 items
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Has been a member for 4-5 years
- Contributed a Tutorial to a Tuts+ Site
Technically, that won’t alter every page refresh to the next one, it will just be random. Not to say that your solution doesn’t work just fine Dan 
If you truly wanted it to alter every page refresh, just use sessions and increment a variable every refresh, if the variable hits the highest number you want (1 or 2 depending on the links) reset the counter on the session variable.
Like I said, the solution above probably works fine for what you need, just advising to use sessions if you truly want them to alternate in order 
- Author had a File in an Envato Bundle
- Author had a Free File of the Month
- Author was Featured
- Bought between 10 and 49 items
- Exclusive Author
- Has been a member for 4-5 years
- Item was Featured
- Referred between 50 and 99 users
- Sold between 10 000 and 50 000 dollars
Well, yeah, but it seemed too small a thing to store in a session imo. But yeah, if you really needed exactly 50:50, using sessions would be best.
- Beta Tester
- Bought between 10 and 49 items
- Contributed a Tutorial to a Tuts+ Site
- Exclusive Author
- Grew a moustache for the Envato Movember competition
- Has been a member for 3-4 years
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Sold between 1 000 and 5 000 dollars
- United States
Alex, you coding PHP !?![]()
Yea
- Community Moderator
- United States
- Was featured in a podcast
- Attended a Community Meetup
- Author had a Free File of the Month
- Bought between 50 and 99 items
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Has been a member for 4-5 years
- Contributed a Tutorial to a Tuts+ Site
Well, yeah, but it seemed too small a thing to store in a session imo. But yeah, if you really needed exactly 50:50, using sessions would be best.Yup
I hope it didn’t seem like I was correcting you, your code works just perfect for this, just wanted to pitch that in there 
