Show a module once per session

(5 votes, average 5.00 out of 5)

Use this rules to show a module only the first time that a user views a certain page. On subsequent page views you can either display an alternative module, or no module at all. (See also: Show a module just once for any user [with cookies])

session_start();
/* set a different "instance name" for each module that you want to show only once */
$instance_name = "shown_module_1";
if (!isset($_SESSION[$instance_name]) ||
 $_SESSION[$instance_name] == false) {
 $_SESSION[$instance_name] = true;
 /* customise "101" to your own module number.
  * This is the one that gets shown just once per session.
  */
 return 101; 
}
/* customise "102" to the module you want to display on the 2nd, 3rd... showing.
 * You can leave out the following line entirely if you don't want to display
 * a module after the first showing.
 */
return 102;

Advanced version: Cycle through different modules on consecutive page views

session_start();
/* set a different "instance name" for each module that you want to show only once */
$instance_name = "module_counter1";
/* use as many or as few as you like, or "0" for no module */
$modules = array(101,0,54,0,62,0,84,33,34);
if (!isset($_SESSION[$instance_name]) ||
 $_SESSION[$instance_name] == "") {
 $_SESSION[$instance_name] = 0;
}
$index = $_SESSION[$instance_name] % count($modules);
$_SESSION[$instance_name]++;
$module_no = $modules[$index];
if ($module_no > 0) return $module_no;