picxelplay
picxelplay

Reputation: 298

Outbound affiliate links: Nth click goes to house

PHP or JQuery way of having outbound links change every Nth click.

For instance:

Visitor1 clicks on LinkA - They get taken to http://google.com
Visitor2 clicks on LinkA - They get taken to http://google.com
Visitor3 clicks on LinkA - They get taken to http://google.com
Visitor4 clicks on LinkA - They get taken to http://yahoo.com
Visitor5 clicks on LinkA - They get taken to http://google.com
Visitor6 clicks on LinkA - They get taken to http://google.com
Visitor7 clicks on LinkA - They get taken to http://google.com
Visitor8 clicks on LinkA - They get taken to http://yahoo.com
Visitor9 clicks on LinkA - They get taken to http://google.com
Visitor10 clicks on LinkA - They get taken to http://google.com
Visitor11 clicks on LinkA - They get taken to http://google.com
Visitor12 clicks on LinkA - They get taken to http://yahoo.com

What are some good ways to go about this?

Upvotes: 0

Views: 129

Answers (2)

Hogan
Hogan

Reputation: 70523

There is no easy way to do this on the client. Remember that jQuery runs on the client not on the server. Each visitor is getting the file from the server. Clients can not communicate with each other.

There are two ways to solve this, the non-client way is easier. For the link put a url to your server then on your server check to see if where you want to redirect this client to and send a redirect back to the browser to go to the target.

The hard client way using jQuery would work like this: When the user clicks on the link do an ajax call to see where to redirect and then load that page from the local client.

Both ways involve having the server track how many clicks have been requested.

Upvotes: 0

user14038
user14038

Reputation:

The sanest approach is to do this in PHP - because you need to make sure you keep a global count of the clicks, and so you cannot do this in the client.

So your original page would have something like:

<a href="/forward.php?id=1">site 1</a>
<a href="/forward.php?id=2">site 2</a>
<a href="/forward.php?id=3">site 3</a>
<a href="/forward.php?id=4">site 4</a>

Have your PHP file record the number of clicks, and return a location based on that. Something like this in pseudo-code:

// connect to database

// find the count of clicks for site with id=X, and increment it

// if clicks % N == 0 
//    redirect to http://example.com/
// otherwise
//    redirect to http://example.org/

The redirection should be simple :

 <?php
   header( 'Location: http://example.com/' ) ;
 ?>

Your database would have a table like:

[link id][click count]

You could also have the link targets there, but that might be more complex than you need..

Upvotes: 3

Related Questions