Sticky rotation with PHP nodes

How to route to the same node every time using a PHP node

Zeno avatar
Written by Zeno
Updated over a week ago

Want to send users to multiple offers (or nodes) but make the same visitor always go to the same node (i.e. sticky sessions)?

If so, the easiest way is with a PHP node and cookies.

Here is sample code you can use for the node:


$cookie = 'sticky-rotator-X';
$number_of_routes = 3;

if (isset($_COOKIE[$cookie]) && is_numeric($_COOKIE[$cookie])) {
return $_COOKIE[$cookie];
else {
$route = mt_rand(1, $number_of_routes);
$month = 2592000 + time();
setcookie($cookie, $route, $month);
return $route;

//Default route just in case
return 1;


This code expects you to have THREE connections coming out from the PHP node, which would be on done 1, 2, and 3.

You can change number_of_routes to match the number of connections you have.

This code is essentially a "sticky rotator" and you can also chain them together.

For example, you might want a PHP node that splits traffic 50/50 between two routes, but then on one of the routes you have another node which 50/50 splits again to two offers.

The end result would be 50 - 25 - 25 rotation to the end offers.

You can also change number of routes and create multiple connections to control rotation.

For example, you could set number_of_routes to 10, where now each route would get 10% of average traffic.

Then, you could connect on done 1-2 to a certain node, 3-5 to another node and 6-10 to a final node, accomplishing a 20-30-50% split in traffic.

Just make sure the number of routes variable always matches the number of outgoing connections you have. Failure to do this may result in errors and lost traffic.

Did this answer your question?