CodeCanyon

How do I make a link change in a page

962 posts
  • 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
webdesigndeluxe says

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!

352 posts
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 50 and 99 users
  • Sold between 10 000 and 50 000 dollars
+1 more
danharper says

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.

962 posts
  • 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
webdesigndeluxe says

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.

352 posts
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 50 and 99 users
  • Sold between 10 000 and 50 000 dollars
+1 more
danharper says

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.

962 posts
  • 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
webdesigndeluxe says

Great, it worked. Thanks!

1461 posts
  • Sold between 50 000 and 100 000 dollars
  • Has been a member for 4-5 years
  • Referred between 50 and 99 users
  • Bought between 10 and 49 items
  • Contributed a Tutorial to a Tuts+ Site
  • Exclusive Author
  • United States
  • Microlancer Beta Tester
PixelBin says

Alex, you coding PHP !? :D

2541 posts
  • Community Moderator
  • United States
  • 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
+5 more
CreatingDrew moderator says

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 :)

352 posts
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 50 and 99 users
  • Sold between 10 000 and 50 000 dollars
+1 more
danharper says

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.

962 posts
  • 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
webdesigndeluxe says
Alex, you coding PHP !? :D

Yea

2541 posts
  • Community Moderator
  • United States
  • 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
+5 more
CreatingDrew moderator says
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 :D

by
by
by
by
by